Trigger $(window).scroll();

74,529

Solution 1

Just use:

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

Source:

Solution 2

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/

Solution 3

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');
Share:
74,529

Related videos on Youtube

Sawny
Author by

Sawny

Hi!

Updated on July 09, 2022

Comments

  • Sawny
    Sawny almost 2 years

    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.

    • Om3ga
      Om3ga over 11 years
      what is animationType? And what if you use animationType inside inverted commas such as 'easeIn'?
    • Sawny
      Sawny over 11 years
      @al0neevenings That is not the problem. The body section scrolls, but the $(window).scrollU(); doesn't get called.
    • Gabriele Petrioli
      Gabriele Petrioli over 11 years
      can you also post what the method bound to scroll is doing ?
    • Sawny
      Sawny over 11 years
      @GabyakaG.Petrioli oh... there was the problem. I had an if that prevented the function to run...
  • Sawny
    Sawny over 11 years
    1) Doesn't solve my problem. 2) Then the callback get called twice.
  • Gabriele Petrioli
    Gabriele Petrioli over 11 years
    @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..
  • reallynice
    reallynice over 9 years
    Mixed this answer with stackoverflow.com/a/12792245/1504300 for graceful scrolling to the bottom.