php如何实现图片延时加载-PHP问题

资源魔 82 0

php完成图片延时加载的办法:正在抵达指定加载图片地位时,用data-src属性值交换img的src属性值便可,如【imgs[i].src = imgs[i].getAttribute('data-src');】。

正在抵达指定加载图片地位时,用data-src的属性值交换img的src的属性值便可,这个时分img就会去申请资本。

(保举教程:php视频教程)

imgs[i].src = imgs[i].getAttribute('data-src');

代码完成:

<!DOCTYPE html>
<html>
<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>图片懒加载</title>
    <style>
        img {
            display: block;
            width: 100%;
            height: 300px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <img data-src="./images/1.jpg" alt="">
    <img data-src="./images/2.jpg" alt="">
    <img data-src="./images/3.jpg" alt="">
    <img data-src="./images/4.jpg" alt="">
    <img data-src="./images/5.jpg" alt="">
    <img data-src="./images/6.jpg" alt="">
    <img data-src="./images/7.jpg" alt="">
    <img data-src="./images/8.jpg" alt="">
    <img data-src="./images/9.jpg" alt="">
    <img data-src="./images/10.jpg" alt="">
    <img data-src="./images/1.jpg" alt="">
    <img data-src="./images/2.jpg" alt="">
</body>
<script>
        var imgs = document.querySelectorAll('img');

        //offsetTop是元素与offsetParent的间隔,轮回猎取直到页面顶部
        function getTop(e) {
            var T = e.offsetTop;
            while(e = e.offsetParent) {
                T += e.offsetTop;
            }
            return T;
        }

        function lazyLoad(imgs) {
            var H = document.documentElement.clientHeight;//猎取可视区域高度
            var S = document.documentElement.scrollTop || document.body.scrollTop;
            for (var i = 0; i < imgs.length; i++) {
                if (H + S > getTop(imgs[i])) {
                    imgs[i].src = imgs[i].getAttribute('data-src');
                }
            }
        }

        window.onload = window.onscroll = function () { //onscroll()正在滚动条滚动的时分触发
            lazyLoad(imgs);
        }
</script>
</html>

相干保举:php培训

以上就是php若何完成图片延时加载的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题 延时加载

抱歉,评论功能暂时关闭!