アニメーションを機能させるのに問題があり、iPad でランダムに停止します
だから私はこのhtmlを持っています
<body>
<div class="touchslider-prev sliderButtons" style="display: block;">back</div>
<div class="touchslider-next sliderButtons" style="display: block;">next</div>
<section id="mainContent" style="width: 950px; overflow:hidden">
<div id="pageHolder" style="float: left; left: 0; position: relative; width: 6000px;">
<div class="pageSize activeSlide" id="page1" style="width: 950px; float: left; position: relative; height:500px; background-color:#F00">
blah
</div>
<div class="pageSize " id="page" style="width: 950px; float: left; position: relative; height:500px; background-color:#FF0">
blah 2
</div>
</div>
</section>
</body>
そしてこのJavaScript
<script type="text/javascript">
function clickHandlerPrev(event) {
// If event isn't already marked as handled, handle it
if(event.handled !== true) {
// Kill event handler, preventing any more clicks
jQuery(window).off("swipeBackward", clickHandlerPrev);
jQuery(window).off("swipeForward", clickHandlerNext);
// Do your stuff here
pageSize = jQuery(".mainHeader").width();
slide("back", pageSize);
// Mark event as handled
event.handled = true;
}
return false;
}
function clickHandlerNext(event) {
// If event isn't already marked as handled, handle it
if(event.handled !== true) {
// Kill event handler, preventing any more clicks
jQuery(window).off("swipeBackward", clickHandlerPrev);
jQuery(window).off("swipeForward", clickHandlerNext);
// Do your stuff here
pageSize = jQuery(".mainHeader").width();
slide("forward", pageSize);
// Mark event as handled
event.handled = true;
}
return false;
}
function slide(data, pageSize) {
if (!pageSize) {
pageSize = 950;
}
var left = calcLeft(data, pageSize);
jQuery('#pageHolder').animate({
left: left
}, 400, function() {
console.log("animation started");
jQuery(window).on("swipeBackward", clickHandlerPrev);
jQuery(window).on("swipeForward", clickHandlerNext);
console.log("animation complete");
});
}
function calcLeft(action, pageSize){
var left;
var currentPos = currentPosition();
if (action == "back") {
console.log(pageSize);
if (currentPos == "0") {
left = -pageSize * 1;
} else {
left = currentPos + pageSize ;
}
} else if (action == "forward") {
if (currentPos == -pageSize * 1) {
left = 0;
} else {
left = currentPos - pageSize ;
}
}
return left;
}
function currentPosition() {
var currentPosition = parseInt(jQuery("#pageHolder").css("left"), 10);
return currentPosition;
}
jQuery(document).ready(function () {
jQuery(".touchslider-prev").click( function() {
jQuery(window).trigger('swipeForward');
});
jQuery(".touchslider-next").click( function() {
jQuery(window).trigger('swipeBackward');
});
jQuery(window).on('swipeForward', clickHandlerNext );
jQuery(window).on('swipeBackward', clickHandlerPrev );
console.log("here");
var maxTime = 1000,
// allow movement if < 1000 ms (1 sec)
maxDistance = 50,
// swipe movement of 50 pixels triggers the swipe
target = jQuery('.pageSize'),
startX = 0,
startTime = 0,
touch = "ontouchend" in document,
startEvent = (touch) ? 'touchstart' : 'mousedown',
moveEvent = (touch) ? 'touchmove' : 'mousemove',
endEvent = (touch) ? 'touchend' : 'mouseup';
target.bind(startEvent, function(e) {
// prevent image drag (Firefox)
// e.preventDefault();
startTime = e.timeStamp;
startX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX;
}).bind(endEvent, function(e) {
startTime = 0;
startX = 0;
}).bind(moveEvent, function(e) {
// e.preventDefault();
var currentX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX,
currentDistance = (startX === 0) ? 0 : Math.abs(currentX - startX),
// allow if movement < 1 sec
currentTime = e.timeStamp;
if (startTime !== 0 && currentTime - startTime < maxTime && currentDistance > maxDistance) {
if (currentX < startX) {
// swipe left code here
console.log("page forward trigger");
jQuery(window).trigger('swipeForward');
}
if (currentX > startX) {
// swipe right code here
console.log("page back trigger");
//slide("back", pageSize);
jQuery(window).trigger('swipeBackward');
}
startTime = 0;
startX = 0;
}
});
});
</script>
これを iPad などで開くと、メインのカラー パネルをスワイプできるようになります。これはうまくいくこともあればうまくいかないこともあり、その理由を見つけるのに本当に苦労しています
誰でも助けることができますか?
スライドは定期的に停止しますが、ユーザーが戻る/次へのボタンを使用するだけで常に機能する場合、イベントをトリガーするだけなので、スライド コードに問題はないようです。
ここにコードを配置しました
http://demo.stg.brightonconsulting.net.au/templates/tests/ipadSlider/