Trigger event when user scroll to specific element - with jQuery

后端 未结 12 1495
攒了一身酷
攒了一身酷 2020-11-22 11:52

I have an h1 that is far down a page..

TRIGGER EVENT WHEN SCROLLED TO.

and I want to trigger an alert

12条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 12:31

    I use the same code doing that all the time, so added a simple jquery plugin doing it. 480 bytes long, and fast. Only bound elements analyzed in runtime.

    https://www.npmjs.com/package/jquery-on-scrolled-to

    It will be $('#scroll-to').onScrolledTo(0, function() { alert('you have scrolled to the h1!'); });

    or use 0.5 instead of 0 if need to alert when half of the h1 shown.

提交回复
热议问题