最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable scrolling until animation is complete? - Stack Overflow

programmeradmin2浏览0评论

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

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

It works, but there is one issue with it: When the user scrolls down while the script scrolls up, there is some juddering because there are two scroll commands at the same time in different directions - sounds logical to me.

I checked some other sites with such scroll functionality, there is no juddering. So what's the trick to prevent this?

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

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

It works, but there is one issue with it: When the user scrolls down while the script scrolls up, there is some juddering because there are two scroll commands at the same time in different directions - sounds logical to me.

I checked some other sites with such scroll functionality, there is no juddering. So what's the trick to prevent this?

Share Improve this question edited Oct 3, 2015 at 10:39 CommunityBot 11 silver badge asked Jul 25, 2012 at 12:14 SvenSven 13.3k29 gold badges97 silver badges156 bronze badges 4
  • on which event are you using this: $("html, body").animate({scrollTop: $(".myDiv").offset().top}, 300); can you explain more please. – Barlas Apaydin Commented Jul 25, 2012 at 12:32
  • are you using this with hover or click or somethingelse? – Barlas Apaydin Commented Jul 25, 2012 at 12:37
  • I use it on click, like $("somehting").click(function() { ... }); – Sven Commented Jul 25, 2012 at 12:40
  • come plz chat.stackoverflow.com/rooms/14079/fast-solution – Barlas Apaydin Commented Jul 25, 2012 at 12:41
Add a comment  | 

2 Answers 2

Reset to default 14

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.

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)

  1. 暂无评论