UMD格式的懒加载(lazyload)

页面的html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <style>
        img {
            display: block;
            margin-bottom: 50px;
            width: 100%;
            height: 900px;
        }
    </style>
</head>
<body>
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p1.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p2.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p3.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p4.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p5.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p6.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p7.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p8.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p9.jpg">
    <img class="soluxLazy" src="https://pic.solux.cn/PC/img-preloading.gif" data-src="https://pic.solux.cn/PC/2017_products/towel/p10.jpg">
<script type="text/javascript" src="soluxLazy.js"></script>
</body>
</html>

soluxLazy.js代码

(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD (Register as an anonymous module)
        define([], factory);
    } else if (typeof exports === 'object') {
        // Node/CommonJS
        module.exports = factory();
    } else {
        // Browser globals
        factory();
    }
}(function () {

    var img = document.querySelectorAll('.soluxLazy');
    var n = 0; //存储图片加载到的位置,避免每次都从第一张图片开始遍历

    function lazyload() { //监听页面滚动事件
        var seeHeight = document.documentElement.clientHeight; //可见区域高度
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; //滚动条距离顶部高度
        for (var i = n; i < img.length; i++) {
            if (img[i].offsetTop < seeHeight + scrollTop + 100) {
                if (img[i].getAttribute("src") === "https://pic.solux.cn/PC/img-preloading.gif") {
                    img[i].src = img[i].getAttribute("data-src");
                }
                n = i + 1;
            }
        }
    }

    // 节流函数
    function throttle(func, wait, options){
        var timeout, context, args, result;
        var previous = 0;
        if (!options) options = {};

        var later = function() {
            previous = options.leading === false ? 0 : Date.now();
            timeout = null;
            result = func.apply(context, args);
            if (!timeout) context = args = null;
        };

        var throttled = function() {
            var now = Date.now();
            if (!previous && options.leading === false) previous = now;
            var remaining = wait - (now - previous);
            context = this;
            args = arguments;
            if (remaining <= 0 || remaining > wait) {
                if (timeout) {
                    clearTimeout(timeout);
                    timeout = null;
                }
                previous = now;
                result = func.apply(context, args);
                if (!timeout) context = args = null;
            } else if (!timeout && options.trailing !== false) {
                timeout = setTimeout(later, remaining);
            }
            return result;
        };

        throttled.cancel = function() {
            clearTimeout(timeout);
            previous = 0;
            timeout = context = args = null;
        };

        return throttled;
    };

    if(img.length){
        lazyload();
        window.addEventListener('scroll',throttle(lazyload,200),false);
    }

}));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值