Trigger event when user scroll to specific element - with jQuery

后端 未结 12 1453
攒了一身酷
攒了一身酷 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:49

    I think your best bet would be to leverage an existing library that does that very thing:

    http://imakewebthings.com/waypoints/

    You can add listeners to your elements that will fire off when your element hits the top of the viewport:

    $('#scroll-to').waypoint(function() {
     alert('you have scrolled to the h1!');
    });
    

    For an amazing demo of it in use:

    http://tympanus.net/codrops/2013/07/16/on-scroll-header-effects/

提交回复
热议问题