Trigger event when user scroll to specific element - with jQuery

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

    Intersection Observer can be the best thing IMO, without any external library it does a really good job.

    const options = {
                root: null,
                threshold: 0.25, // 0 - 1 this work as a trigger. 
                rootMargin: '150px'
            };
    
            const target = document.querySelector('h1#scroll-to');
            const observer = new IntersectionObserver(
               entries => { // each entry checks if the element is the view or not and if yes trigger the function accordingly
                entries.forEach(() => {
                    alert('you have scrolled to the h1!')
                });
            }, options);
            observer.observe(target);
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-22 12:49

    Fire scroll only once after a successful scroll

    The accepted answer worked for me (90%) but I had to tweak it a little to actually fire only once.

    $(window).on('scroll',function() {
                var hT = $('#comment-box-section').offset().top,
                    hH = $('#comment-box-section').outerHeight(),
                    wH = $(window).height(),
                    wS = $(this).scrollTop();
    
                if (wS > ((hT+hH-wH)-500)){
                    console.log('comment box section arrived! eh');
                    // After Stuff
                    $(window).off('scroll');
                    doStuff();
                }
    
            });
    

    Note: By successful scroll I mean when user has scrolled to my element or in other words when my element is in view.

    0 讨论(0)
  • 2020-11-22 12:50

    Inview library triggered event and works well with jquery 1.8 and higher! https://github.com/protonet/jquery.inview

    $('div').on('inview', function (event, visible) {
      if (visible == true) {
        // element is now visible in the viewport
      } else {
        // element has gone out of viewport
      }
    });
    

    Read this https://remysharp.com/2009/01/26/element-in-view-event-plugin

    0 讨论(0)
  • 2020-11-22 12:54

    Just a quick modification to DaniP's answer, for anyone dealing with elements that can sometimes extend beyond the bounds of the device's viewport.

    Added just a slight conditional - In the case of elements that are bigger than the viewport, the element will be revealed once it's top half has completely filled the viewport.

    function elementInView(el) {
      // The vertical distance between the top of the page and the top of the element.
      var elementOffset = $(el).offset().top;
      // The height of the element, including padding and borders.
      var elementOuterHeight = $(el).outerHeight();
      // Height of the window without margins, padding, borders.
      var windowHeight = $(window).height();
      // The vertical distance between the top of the page and the top of the viewport.
      var scrollOffset = $(this).scrollTop();
    
      if (elementOuterHeight < windowHeight) {
        // Element is smaller than viewport.
        if (scrollOffset > (elementOffset + elementOuterHeight - windowHeight)) {
          // Element is completely inside viewport, reveal the element!
          return true;
        }
      } else {
        // Element is larger than the viewport, handle visibility differently.
        // Consider it visible as soon as it's top half has filled the viewport.
        if (scrollOffset > elementOffset) {
          // The top of the viewport has touched the top of the element, reveal the element!
          return true;
        }
      }
      return false;
    }
    
    0 讨论(0)
  • 2020-11-22 12:57

    You can use jQuery plugin with the inview event like this :

    jQuery('.your-class-here').one('inview', function (event, visible) {
        if (visible == true) {
          //Enjoy !
        }
    });
    

    Link : https://remysharp.com/2009/01/26/element-in-view-event-plugin

    0 讨论(0)
提交回复
热议问题