Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger $(window).scroll();

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.

like image 637
Sawny Avatar asked Oct 27 '12 09:10

Sawny


People also ask

What is window scroll?

The Window. scroll() method scrolls the window to a particular place in the document.

What is $( window scrollTop ()?

$(selector).scrollTop(position) Parameter. Description. position. Specifies the vertical scrollbar position in pixels.

How do I add an event listener to scroll?

forEach(element => { window. addEventListener( "scroll", () => runOnScroll(element), { passive: true } ); }); Or alternatively, bind a single scroll listener, with evt => runOnScroll(evt) as handler and then figure out what to do with everything in elements inside the runOnScroll function instead.


3 Answers

Just use:

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

Source:

  • http://www.w3schools.com/jquery/event_scroll.asp
  • https://api.jquery.com/scroll/
like image 138
Vinícius Hipólito Avatar answered Nov 04 '22 19:11

Vinícius Hipólito


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/

like image 40
Gabriele Petrioli Avatar answered Nov 04 '22 20:11

Gabriele Petrioli


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');
like image 20
user3059993 Avatar answered Nov 04 '22 21:11

user3059993