0

我在制作动画时遇到了一些问题,它在我的 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/

4

1 回答 1

0

在我看来,您似乎为 swipeForward 和 swipeBackward 安装了事件处理程序,但随后您只卸载了触发的事件处理程序。下一次,您再次安装这两个事件处理程序,因此其中一些会堆积起来并安装不止一次,当一个安装不止一次的触发时,这肯定会弄乱您的动画。如果您要在其中一个触发时卸载,那么您需要同时卸载两个,而不仅仅是触发的那个。点击处理程序也有同样的问题。

此外,在 中calcLeft(),该变量left应声明为局部变量。另外,什么jQuery.when(promise)时候promise是整数?您应该将延迟对象传递给jQuery.when().

于 2013-02-25T01:36:15.283 回答