search

Home  >  Q&A  >  body text

javascript - How to determine whether an element has scrolled to the bottom of the visible area of ​​the screen?

An element was originally located above the bottom of the visible area of ​​the screen. When the scroll bar scrolled up, the element touched the bottom of the screen. I want to trigger a function when the element touches the bottom of the screen. So how can I capture the element? What about touching the bottom of the screen?

给我你的怀抱给我你的怀抱2779 days ago766

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-19 10:28:18

    <p class="element" style="height: 100px;width:100px; border:1px solid;margin-top:500px"></p>

    The element turns red when it touches the bottom, otherwise it turns white

        <script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
        <script>
            var element = $('.element');
            var win = $(window);
            win.scroll(function() {
                if (element.offset().top + element.height() <= win.height() + win.scrollTop()) {
                    element.css('backgroundColor', '#f33');
                } else {
                    element.css('backgroundColor', '#fff');
                }
            })
        </script>

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-19 10:28:18

    You can get the current scrollTop and the clientTop of the element through the js box model, and then make a judgment when the height of the box is known.

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-19 10:28:18

    document.addEventListener('scroll', function () {
        var p = document.getElementById('p')
        if (document.body.scrollTop === p.offsetTop) {
            //...
        }
    })

    reply
    0
  • Cancelreply