码迷,mamicode.com
首页 > Windows程序 > 详细

IntersectionObserver API js懒加载实现方法

时间:2020-06-29 13:27:50      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:info   ret   pos   tin   trie   script   new   server   fas   

<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <title></title>
    <style>
        #content {
            width: 100vw;
            height: 100vh;
        }

        #info {
            position: fixed;
            top: 0;
        }

        #target {
            width: 100px;
            height: 100px;
            background: red;
        }
    </style>

</head>

<body>
    <div id="info">我藏在页面底部,请向下滚动</div>
    <div id="content"></div>
    <div class="test">adsfasfd</div>
    <div id="target"></div>
    <div id="h" style="height:1000px;"></div>
    <div class="test">adsfasfd</div>
</body>
<!--  -->
<script type="text/javascript">
    function query(selector) {
        return Array.from(document.querySelectorAll(selector));
    }
    let observer = new IntersectionObserver(function (entries) {
        entries.forEach(function (element, index) {
            // console.log(observer.takeRecords());
            if (element.isIntersecting) {
                info.textContent = "我出来了";
            } else {
                info.textContent = "我藏在页面底部,请向下滚动"
            }
        });

    }, {
        root: null,
        threshold: [0, 1]
    })
    query(document.querySelectorAll(‘.test‘).forEach((item) => {
        observer.observe(item);
    }))
    // observer.observe(document.querySelectorAll(‘.test‘)[0])
</script>

</html>

 

IntersectionObserver API js懒加载实现方法

标签:info   ret   pos   tin   trie   script   new   server   fas   

原文地址:https://www.cnblogs.com/jwzhang/p/13207279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!