How to disable scrolling until animation is complete?

后端 未结 2 1627
孤独总比滥情好
孤独总比滥情好 2021-01-01 23:39

I am using this code to scroll to a certain element on my page:

$(\"html, body\").animate({scrollTop: $(\".myDiv\").offset().top}, 300);

It

相关标签:
2条回答
  • 2021-01-01 23:59

    Thats a jQuery bug when you use animate with scrolling, good detection.

    I did a research how to turn it off scrolling and find this question : How to disable scrolling temporarily?

    Here is jsFiddle. You will see after click; user cant scroll untill animate complete.

    $('.myDiv').click(function(){
    
        disable_scroll();
    
        $('html, body').stop().animate({ scrollTop: 0 }, 700,function() {
            enable_scroll();
        });
    });
    

    edit: thanks to galambalazs btw.

    0 讨论(0)
  • 2021-01-02 00:00

    an idea - try hooking to the scroll event and use http://api.jquery.com/stop/ to stop your animation .. bad idea..

    same problem with a solution - let user scrolling stop jquery animation of scrolltop?

    0 讨论(0)
提交回复
热议问题