Trigger event when user scroll to specific element - with jQuery

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

    If you are doing a lot of functionality based on scroll position, Scroll magic (http://scrollmagic.io/) is built entirely for this purpose.

    It makes it easy to trigger JS based on when the user reaches certain elements when scrolling. It also integrates with the GSAP animation engine (https://greensock.com/) which is great for parallax scrolling websites

    0 讨论(0)
  • 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.

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

    This should be what you need.

    Javascript:

    $(window).scroll(function() {
        var hT = $('#circle').offset().top,
            hH = $('#circle').outerHeight(),
            wH = $(window).height(),
            wS = $(this).scrollTop();
        console.log((hT - wH), wS);
        if (wS > (hT + hH - wH)) {
            $('.count').each(function() {
                $(this).prop('Counter', 0).animate({
                    Counter: $(this).text()
                }, {
                    duration: 900,
                    easing: 'swing',
                    step: function(now) {
                        $(this).text(Math.ceil(now));
                    }
                });
            }); {
                $('.count').removeClass('count').addClass('counted');
            };
        }
    });
    

    CSS:

    #circle
    {
        width: 100px;
        height: 100px;
        background: blue;
        -moz-border-radius: 50px;
        -webkit-border-radius: 50px;
        border-radius: 50px;
        float:left;
        margin:5px;
    }
    .count, .counted
    {
      line-height: 100px;
      color:white;
      margin-left:30px;
      font-size:25px;
    }
    #talkbubble {
       width: 120px;
       height: 80px;
       background: green;
       position: relative;
       -moz-border-radius:    10px;
       -webkit-border-radius: 10px;
       border-radius:         10px;
       float:left;
       margin:20px;
    }
    #talkbubble:before {
       content:"";
       position: absolute;
       right: 100%;
       top: 15px;
       width: 0;
       height: 0;
       border-top: 13px solid transparent;
       border-right: 20px solid green;
       border-bottom: 13px solid transparent;
    }
    

    HTML:

    <div id="talkbubble"><span class="count">145</span></div>
    <div style="clear:both"></div>
    <div id="talkbubble"><span class="count">145</span></div>
    <div style="clear:both"></div>
    <div id="circle"><span class="count">1234</span></div>
    

    Check this bootply: http://www.bootply.com/atin_agarwal2/cJBywxX5Qp

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

    You can calculate the offset of the element and then compare that with the scroll value like:

    $(window).scroll(function() {
       var hT = $('#scroll-to').offset().top,
           hH = $('#scroll-to').outerHeight(),
           wH = $(window).height(),
           wS = $(this).scrollTop();
       if (wS > (hT+hH-wH)){
           console.log('H1 on the view!');
       }
    });
    

    Check this Demo Fiddle


    Updated Demo Fiddle no alert -- instead FadeIn() the element


    Updated code to check if the element is inside the viewport or not. Thus this works whether you are scrolling up or down adding some rules to the if statement:

       if (wS > (hT+hH-wH) && (hT > wS) && (wS+wH > hT+hH)){
           //Do something
       }
    

    Demo Fiddle

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

    You could use this for all devices,

    $(document).on('scroll', function() {
        if( $(this).scrollTop() >= $('#target_element').position().top ){
            do_something();
        }
    });
    
    0 讨论(0)
  • 2020-11-22 12:44

    Combining this question with the best answer from jQuery trigger action when a user scrolls past a certain part of the page

    var element_position = $('#scroll-to').offset().top;
    
    $(window).on('scroll', function() {
        var y_scroll_pos = window.pageYOffset;
        var scroll_pos_test = element_position;
    
        if(y_scroll_pos > scroll_pos_test) {
            //do stuff
        }
    });
    

    UPDATE

    I've improved the code so that it will trigger when the element is half way up the screen rather than at the very top. It will also trigger the code if the user hits the bottom of the screen and the function hasn't fired yet.

    var element_position = $('#scroll-to').offset().top;
    var screen_height = $(window).height();
    var activation_offset = 0.5;//determines how far up the the page the element needs to be before triggering the function
    var activation_point = element_position - (screen_height * activation_offset);
    var max_scroll_height = $('body').height() - screen_height - 5;//-5 for a little bit of buffer
    
    //Does something when user scrolls to it OR
    //Does it when user has reached the bottom of the page and hasn't triggered the function yet
    $(window).on('scroll', function() {
        var y_scroll_pos = window.pageYOffset;
    
        var element_in_view = y_scroll_pos > activation_point;
        var has_reached_bottom_of_page = max_scroll_height <= y_scroll_pos && !element_in_view;
    
        if(element_in_view || has_reached_bottom_of_page) {
            //Do something
        }
    });
    
    0 讨论(0)
提交回复
热议问题