attach mousewheel event listener don't reset scroll inertia

I'm using the jquery-mousewheel plugin to trigger a function.

When I call moveit I detach the listener and wait until the animation is completed, then I re-attach the listener.

The problem is that when I re-attach it, the mousewheel plugin is still listening to the inertia of some mouses/trackpads, and call moveit repeatedly.

I guess debouncing or throttling the function call are not good solutions in my specific case, because the inertia is still there, and I also want the listener to be attached immediately for other possible moveit calls.

Is there a way to "kill the inertia" by completely resetting the mousewheel event, instead of only detaching it?

$(document).ready(function () {

    var tween;
    var slide = $('#slide');

    function bodyListen () {
        $('body').on('mousewheel.bodyscroll',
        function (e, delta, deltaX, deltaY) {
            e.preventDefault();
            $('body').off('mousewheel.bodyscroll');
            moveit();
        });
    }

    function moveit () {
        tween = TweenMax.to(slide, 0.8, {
            marginLeft: 300,
            onComplete: bodyListen
        });
    }

    bodyListen();
});

处理事件(或与DOM相关的任何操作)时使用标志,因为事件监听器通常可能表现得像异步函数一样。

$(document).ready(function () {

    var tween;
    var slide = $('#slide');
    var flag = true;

    function bodyListen () {
        $('body').on('mousewheel.bodyscroll',
        function (e, delta, deltaX, deltaY) {
            if(flag){
                e.preventDefault();
                flag = false;
                moveit();
            }
        });
    }

    function moveit () {
        tween = TweenMax.to(slide, 0.8, {
            marginLeft: 300,
            onComplete: function(){
                flag = true;
            }
        });
    }

    bodyListen();
});

Although I have not tried this myself, TweenMax has Kill Tweens of and KillAll that allow you to kill a tween and optionally complete them before you do. maybe not what you want, because it would force the animation to finish but it gets you back to a state you want and the code insert is simple. At the beginning of function bodyListen

function bodyListen() {
    if(tween) tween.killAll(true)
    .....
链接地址: http://www.djcxy.com/p/74474.html

上一篇: 外部“C”变量名称为“虚拟”

下一篇: 附加鼠标滚轮事件监听器不重置滚动惯量