Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

请大佬给愚弟看一个方法,如何检测元素离开可视范围

//进入可视范围已经可以了,但是怎么检测当前元素离开可视范围,愚弟卡主了,谢谢大佬

        var lastItem = 0;
        $(document).ready(function() {
            detectionVisualRange();
            window.addEventListener("scroll", function(e) {
                detectionVisualRange();
            });
        });

        function detectionVisualRange() {
            var lis = $('.index-mode-2').find(".item");
            //swHeight=滚动的高度+窗体的高度;当item的offset高度<=swHeight,那么说明当前.item显示在可视区域了
            var swHeight = $(window).scrollTop() + $(window).height();
            $.each(lis, function(index, item) {
                mTop = item.offsetTop;
                var dItem = index + 1;
                if ((mTop + 800) < swHeight && dItem > lastItem) {
                    console.log(index + 1 + "进入可视范围");
                    lastItem += 1;
                }
            });
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

既然获取到了’进入可视范围‘,那么相反的条件,不就是’离开可视范围‘


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...