Trigger $(window).scroll();

本秂侑毒 提交于 2019-12-08 15:43:19

问题


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.


回答1:


Just use:

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

Source:

  • http://www.w3schools.com/jquery/event_scroll.asp
  • https://api.jquery.com/scroll/



回答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/




回答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');


来源:https://stackoverflow.com/questions/13098988/trigger-window-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!