24

When I call $("body").animate({scrollTop: someValue}); I want $(window).scroll(function() { }); to be called too. How can I achieve that?

I have tried with $(window).trigger("scroll") and $(window).triggerHandler("scroll") without success.

The code

EDIT: Problem solved. There was an if in my $(window).scroll(function() { }); that caused the problem.

Sawny
  • 1,404
  • 2
  • 14
  • 31

3 Answers3

50

Just use:

// Trigger the scroll event
$(window).scroll();

Source:

DBS
  • 9,110
  • 4
  • 35
  • 53
9

Apply it to both body and html as it is not consistent.. (for example, FF uses the html for scrolling while chrome uses the body)

$("html, body").animate({scrollTop: someValue});

demo at http://jsfiddle.net/vzyVh/

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 1) Doesn't solve my problem. 2) Then the callback get called twice. – Sawny Oct 27 '12 at 09:57
  • @sawny, only if you have specifically made your CSS to allow both `html` and `body` to scroll. By default only one of those scrolls.. Additionally: the `scroll` method applied to `window` is not called only once. It gets called for each scrolling step.. – Gabriele Petrioli Oct 27 '12 at 10:00
  • 1
    Mixed this answer with http://stackoverflow.com/a/12792245/1504300 for graceful scrolling to the bottom. – reallynice Dec 24 '14 at 13:55
2

You can try below code - here i am scrolling to top of my div tag which has id "one".

$('html,body').animate({ scrollTop: $('#one').offset().top }, 'slow');
user3059993
  • 324
  • 1
  • 4
  • 9