美文网首页
使用 Intersection Observer 实现图片延迟加

使用 Intersection Observer 实现图片延迟加

作者: _____西班木有蛀牙 | 来源:发表于2018-06-20 18:16 被阅读17次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>使用 Intersection Observer 实现图片延迟加载</title>
    <link href="https://cdn.bootcss.com/materialize/1.0.0-beta/css/materialize.min.css" rel="stylesheet">
    <style>
        img {
            width: 100%;
            margin: 15px 0;
        }
    </style>
</head>

<body>
    <div class="container">
        <ul>
            <li>
                <img class="js-lazy-image" width="996" height="664" data-src="images/1.jpg" alt="">
            </li>
            <li>
                <img class="js-lazy-image" width="996" height="560" data-src="images/2.jpg" alt="">
            </li>
            <li>
                <img class="js-lazy-image" width="996" height="748" data-src="images/3.jpg" alt="">
            </li>
        </ul>
    </div>
    <script>
        function preloadImage(image) {
            image.setAttribute('src', image.dataset.src)
        }

        // Get all of the images that are marked up to lazy load 
        const images = document.querySelectorAll('.js-lazy-image');
        const config = {
            // If the image gets within 50px in the Y axis, start the download. 
            rootMargin: '50px 0px',
            threshold: 0.01
        };
        // If we don't have support for intersection observer, load the images immediately
        if (!'IntersectionObserver' in window) {
            console.log(123)
            Array.from(images).forEach(image => preloadImage(image));
        } else {
            // The observer for the images on the page 
            let observer = new IntersectionObserver(onIntersection, config);
            
            function onIntersection(entries) {
                // Loop through the entries
                entries.forEach(entry => {
                    // Are we in viewport? 
                    if (entry.intersectionRatio > 0) {
                        // Stop watching and load the image
                        observer.unobserve(entry.target);
                        preloadImage(entry.target);
                    }
                });
            }

            images.forEach(image => {
                observer.observe(image);
            });
        }
    </script>
</body>

</html>

相关文章

  • 使用 Intersection Observer 实现图片延迟加

  • 谈谈IntersectionObserver懒加载

    概念 IntersectionObserver接口(从属于Intersection Observer API)为开...

  • Intersection Observer 简介

    Intersection Observer API提供了一种异步观察目标元素与祖先元素或顶级文档viewport的...

  • Intersection observer will not t

    1、需要在ready等渲染完成之后调用,不能使用attatch 2、在组件中使用应该注意: 创建并返回一个 Int...

  • 图片懒加载

    Intersection Observer 观察器接口,它是是浏览器原生提供的构造函数,使用它能省到大量的循环和判...

  • JS

    JS 懒加载,预加载 概念:懒加载也叫延迟加载:JS图片延迟加载,延迟加载图片或符合某些条件时才加载某些图片。预加...

  • js 实现图片懒加载

    一、懒加载 懒加载也叫延迟加载,Js中图片的延迟加载,延迟图片需要符合某些条件时才加载;对于图片过多的页面,为了加...

  • 手动实现一个MVVM框架(下)

    实现双向绑定 HTML部分 使用方式 详解 构造函数 _init方法的定义 实现_observer函数 接下来实现...

  • 基于Rabbitmq实现延迟队列

    转自 基于Rabbitmq实现延迟队列 基于Rabbitmq实现延迟队列 延迟队列的使用场景 淘宝订单业务:下单后...

  • 2020-05-28-Android-RxJava

    观察者模式 下面写了一个简单例子,实现图片的异步加载。首先实现一个Observer对象,用来接收数据,更新图片内容...

网友评论

      本文标题:使用 Intersection Observer 实现图片延迟加

      本文链接:https://www.haomeiwen.com/subject/fvwxyftx.html