亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JS實(shí)現(xiàn)圖片預(yù)加載之無(wú)序預(yù)加載功能代碼

 更新時(shí)間:2017年05月12日 14:22:01   作者:Glunefish (咕嚕魚(yú))  
這篇文章主要介紹了JS實(shí)現(xiàn)圖片預(yù)加載之無(wú)序預(yù)加載功能代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

圖片預(yù)加載之無(wú)序預(yù)加載的效果圖如下所示,如果大家感覺(jué)不錯(cuò),請(qǐng)參考實(shí)現(xiàn)代碼。

具體代碼如下所示:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>PreLoading</title>
  <style>
    *{margin:0; padding:0; border:none; outline:0; text-decoration:none;}
    html,body,.box{width:100%; height:100%;}
    .box{display:none;}
    #img{width:90%; height:90%; margin:2vh auto 0; display:block; box-shadow:0 0 10px gray;}
    .box .btns{width:140px; height:40px; display:block; margin:20px auto;}
    .box .btns .btn{width:60px; height:40px; display:block; border:1px gray solid; background-color:#ccc; text-align:center; line-height:40px; float:left;}
    .box .btns .btn:nth-of-type(2){margin-left:16px;}
    .load{width:100%; height:100%; display:block; font-size:60px; font-family:"微軟雅黑"; color:#ccc; text-align:center; line-height:100vh; position:fixed;}
  </style>
</head>
<body>
  <div class="box">
    <img id="img" src="" alt="pic">
    <p class="btns"><a href="javascript:" rel="external nofollow" rel="external nofollow" class="btn">prev</a><a href="javascript:" rel="external nofollow" rel="external nofollow" class="btn">next</a></p>
  </div>
  <p class="load">0%</p>
  <script type="text/javascript">
    var imgs = ['http://down.699pic.com/photo/50036/7661.jpg?_upt=da51378d1494571758&_upd=500367661.jpg',
          'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/09/0F/ChMkJljskIqIPX9bAAMPyuIn8DcAAbj8QB7XpYAAw_i343.jpg',
          'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/09/0F/ChMkJljskLeIaW-JAAIudN_yqvgAAbj8gDQO5AAAi6M64.jpeg',
          'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/08/ChMkJlauzISIH0uXAARUHuJLVX8AAH8-gHu6zsABFQ2166.jpg',
          'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/08/ChMkJlauzISIIL5TAAObxg4-XeUAAH8-gHzP3EAA5ve000.jpg'];
    // 綁定按鈕事件
    var btns = document.getElementsByClassName('btn'),
      img = document.getElementById('img'),
      index = 0;
    for(var i=0;i<btns.length;i++){
      btns[i].onclick = function(){
        if(this.innerHTML === 'next'){
          index = Math.min(++index , imgs.length-1);
          img.setAttribute('src',imgs[index]);
        }
        if(this.innerHTML === 'prev'){
          index = Math.max(--index , 0);
          img.setAttribute('src',imgs[index]);
        }
      }
    }
    // 計(jì)數(shù)變量
    var count = 0,
      load = document.getElementsByClassName('load')[0],
      box = document.getElementsByClassName('box')[0];
    // 無(wú)序預(yù)加載
    for(var i=0;i<imgs.length;i++){
      (function(i){
        var imgObj = new Image();
      imgObj.onload = function(){
          load.innerHTML = Math.round((count + 1) / imgs.length * 100) + '%';
          count++;
          if(count >= imgs.length-1){
            load.style.display = 'none';
            box.style.display = 'block';
            img.setAttribute('src',imgs[0]);
            document.title = '1/' + imgs.length;
          }
        }
      imgObj.onerror = function(){
          load.innerHTML = Math.round((count + 1) / imgs.length * 100) + '%';
          count++;
          if(count >= imgs.length-1){
            load.style.display = 'none';
            box.style.display = 'block';
            img.setAttribute('src',imgs[0]);
            document.title = '1/' + imgs.length;
          }
        }
        imgObj.src = imgs[i];
      })(i);
    }
  </script>
</body>
</html>

以上所述是小編給大家介紹的JS實(shí)現(xiàn)圖片預(yù)加載之無(wú)序預(yù)加載功能代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論