简体   繁体   English

JQuery - 禁用滚轮直到动画完成

[英]JQuery - Disable scroll wheel until animation is completed

I am trying to disable mousewheel on mousewheel event and only enable it after the action is completed.我试图在鼠标滚轮事件上禁用鼠标滚轮,并且仅在操作完成后才启用它。

$(window).on('DOMMouseScroll mousewheel', function (event) {

    //disable mousewhell until the following animation is complete

    $('.box').animate({
        margin: div_offset+'px 0 0 0'
    }, 500);

    //enable mousewhell

    return false;
});

I would also like the code to ignore every scroll after the first one, until the animation is complete.我还希望代码在第一个滚动之后忽略每个滚动,直到动画完成。 In this particular case, I want the animation to complete only once, even if the user scrolls for more than one tick on mouse wheel.在这种特殊情况下,我希望动画只完成一次,即使用户在鼠标滚轮上滚动超过一个刻度也是如此。 Basically in this case, I want the mouse wheel to be disabled for 500 milliseconds after a single mouse scroll "tick", and not register the remaining "ticks" ever (if the user scrolls for 10 ticks during these 500 milliseconds, I only want one animation, while the rest are ignored).基本上在这种情况下,我希望鼠标滚轮在单个鼠标滚动“滴答”后禁用 500 毫秒,并且永远不注册剩余的“滴答声”(如果用户在这 500 毫秒内滚动 10 个滴答声,我只想要一个动画,而其余的则被忽略)。 After 500 milliseconds, I want the mousewheel to be enabled once again. 500 毫秒后,我希望再次启用鼠标滚轮。

I thank you in advance.我先谢谢你。

I did originally close this question as a duplicate but was told it only answer half the question.我最初确实关闭了这个问题作为重复,但被告知它只能回答一半的问题。 I re-openend it to add a suggested solution for not processing the event again until the animation is done.我重新打开它以添加建议的解决方案,以便在动画完成之前不再处理事件。

Using animate complete as suggested in this SO post按照此 SO 帖子中的建议使用 animate complete

You should be able to do something similar to this:你应该能够做类似的事情:

var animating = false;

$(window).on('DOMMouseScroll mousewheel', function (event) {
    if(animating){
        return false;
    }

   animating = true;

    $('.box').animate({
        margin: div_offset+'px 0 0 0'
    }, 500, function(){
        animating = false;
    });
});

What you can do is assign the function to the DOM event and then assign an empty function (or 'off' it) in the first line of that code.您可以做的是将函数分配给 DOM 事件,然后在该代码的第一行分配一个空函数(或“关闭”它)。 Then when the animation is complete just re-assign the function to the DOM event.然后当动画完成时,只需将函数重新分配给 DOM 事件。 Like so:像这样:

function test(event) {
    $(window).off('DOMMouseScroll mousewheel');
    //perform your scroll event
    $('.box').animate({
         margin: div_offset+'px 0 0 0'
    }, 500);
    sleep(500);
    $(window).on('DOMMouseScroll mousewheel',test);
}
$(window).on('DOMMouseScroll mousewheel',test);

This will do the trick but it will hold your JS thread busy, you could also do:这可以解决问题,但它会使您的 JS 线程忙碌,您也可以这样做:

function test(event) {
        $(window).off('DOMMouseScroll mousewheel');
        //perform your scroll event
        $('.box').animate({
             margin: div_offset+'px 0 0 0'
        }, 500);
        setTimeout(500,function() {$(window).on('DOMMouseScroll mousewheel',test(event));});

    }
    $(window).on('DOMMouseScroll mousewheel',test);

And based on Frans answer:并基于弗兰斯的回答:

function test(event) {
            $(window).off('DOMMouseScroll mousewheel');
            //perform your scroll event
            $('.box').animate({
                 margin: div_offset+'px 0 0 0'
            }, 500,function() {$(window).on('DOMMouseScroll mousewheel',test);});

        }
    $(window).on('DOMMouseScroll mousewheel',test);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM