延时加载图片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css3</title>
<style type="text/css">
*{
    margin:0;
    padding: 0;
}
ul,li{
    list-style: outside none none;
}
.box{
    width: 600px;
    margin: 0 auto;
}
.box li{
    margin: 40px 0;
    width: 100%;
    height: 170px;
    background: #ddd;
}
.box li img{
    width: 100%;
}
</style>
</head>
<body>
<div class="box">
    <ul>
        <li><img x-src="img/1.jpg" alt=""></li>
        <li><img x-src="img/2.jpg" alt=""></li>
        <li><img x-src="img/3.jpg" alt=""></li>
        <li><img x-src="img/4.jpg" alt=""></li>
        <li><img x-src="img/5.jpg" alt=""></li>
        <li><img x-src="img/1.jpg" alt=""></li>
        <li><img x-src="img/2.jpg" alt=""></li>
        <li><img x-src="img/3.jpg" alt=""></li>
        <li><img x-src="img/4.jpg" alt=""></li>
        <li><img x-src="img/5.jpg" alt=""></li>
        <li><img x-src="img/1.jpg" alt=""></li>
        <li><img x-src="img/2.jpg" alt=""></li>
        <li><img x-src="img/3.jpg" alt=""></li>
        <li><img x-src="img/4.jpg" alt=""></li>
        <li><img x-src="img/5.jpg" alt=""></li>
        <li><img x-src="img/1.jpg" alt=""></li>
        <li><img x-src="img/2.jpg" alt=""></li>
        <li><img x-src="img/3.jpg" alt=""></li>
        <li><img x-src="img/4.jpg" alt=""></li>
        <li><img x-src="img/5.jpg" alt=""></li>
        <li><img x-src="img/1.jpg" alt=""></li>
        <li><img x-src="img/2.jpg" alt=""></li>
        <li><img x-src="img/3.jpg" alt=""></li>
        <li><img x-src="img/4.jpg" alt=""></li>
        <li><img x-src="img/5.jpg" alt=""></li>
    </ul>
</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){
        function showImg(){
            $('img').each(function(index,ele){
                if($(this).offset().top<$(window).height()+$(window).scrollTop()){
                    var oSrc = $(this).attr('x-src');
                    $(this).attr('src',oSrc);
                }
            })
        }
        showImg();
        $(window).scroll(function(){
            showImg();
        });
    });
</script>
</body>
</html>

 

posted on 2016-09-10 15:28  jasonduanmu  阅读(176)  评论(0编辑  收藏  举报

导航