Trigger events when the window is scrolled to certain positions

后端 未结 4 1186
别那么骄傲
别那么骄傲 2020-12-01 10:11

I would like to call functions when the browser window goes beyond a certain point
(e.g the user scrolled the window down beyond 200px from the top

Is there an e

4条回答
  •  醉梦人生
    2020-12-01 10:25

    A popular plugin for what you're describing is jQuery Waypoints.


    If you don't wish to use a plugin, the mechanisms are:

    $(window).scrollTop(); // returns pixel value
    $(window).scroll(function () { /* code here */ });
    

    However, because the scroll event fires very quickly, you must be careful to put only code that executes quickly inside the handler. A common technique is to "throttle" the rate at which you handle the event by checking if a certain amount of time has passed.

提交回复
热议问题