Blogger Information
Blog 4
fans 0
comment 0
visits 2628
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS懒加载
遇上你是我的缘的博客
Original
780 people have browsed it
<script>
    var container=document.createElement('div');
    var frag=document.createDocumentFragment();

    for(var i=1 ;i<13;i++){
        var imgUrl='images/'+i+'.jpg';
        var img=document.createElement('img');
        img.setAttribute('data-index',imgUrl);
        img.setAttribute('src','images/loading.gif');
        img.setAttribute('style','width:600px;height:350px;margin: 5px;');
        frag.appendChild(img);
        // console.log(img)
    }
    container.appendChild(frag);
    document.body.insertBefore(container,document.body.firstElementChild)
    //——————————————————————————————————————————以上动态生成占位图片——
    window.addEventListener('scroll',lazyLoaded);
    function lazyLoaded(){
        //滚动条高度
        var scrollTop= document.documentElement.scrollTop;
        //可视区高度
        var clientHeight=document.documentElement.clientHeight;

        var imgArr=Array.prototype.slice.call(document.images);
        imgArr.forEach(function(ev){
           console.log('滚动条高度:'+scrollTop+' 可视区高度:'+clientHeight);

          if(ev.offsetTop<=(scrollTop+clientHeight)){
              ev.src=ev.dataset.index;
          }
       })


    }

    window.addEventListener('load',lazyLoaded);

</script>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post