【HTML+CSS+JavaScript(jQuery)】图片预加载(打开网页等待1秒左右时间显示图片)(懒加载)

需求:图片预加载(打开网页等待1秒左右时间显示图片),也称懒加载

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义数作用  图片懒加载</title>
    <style>
        .imglist img {
            width: 400px;
            height: 300px;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <h1>图片懒加载</h1>
    <hr>

    <div class="imglist">
        <img loadpic='../../dist/images_one/1.jpg'>
        <img loadpic='../../dist/images_one/2.jpg'>
        <img loadpic='../../dist/images_one/3.jpg'>
        <img loadpic='../../dist/images_one/4.jpg'>
    </div>

    <script src="../jquery-3.3.1.js"></script>
    <script>

        //console.log($('.imglist img').attr('loadpic'))
        //单次定时
        setTimeout(function(){
            $('.imglist img').each(function(index, item){
                $(item).attr('src', $(item).attr('loadpic'))
            })
        }, 2000)
    </script>
</body>
</html>

 

posted @ 2018-08-16 15:24  NeymarGoal  阅读(1229)  评论(0编辑  收藏  举报