jQuery/JavaScript 實現的非同步載入圖片效果

來源:互聯網
上載者:User

在上代碼之前先說一下簡單的原理,我們知道在 img 標籤中的 src 屬性是指向圖片地址,要實現非同步載入,就先暫時不給 src 屬性賦值,而是先找個中間變數的屬性 data-src,把圖片地址寫在 data-src 屬性裡,然後當觸發 onclick 事件的時候,把 data-src 的值複製給 src,也就實現了非同步載入。

代碼時間到,因為無論是 jQuery 還是 JavaScript 的方法,HTML 的結構是相同的,如下:

 代碼如下 複製代碼
<div id="box">
    <input type="submit" value="非同步載入圖片" id="submitBtn" />
    <div class="picBox">
        <img data-src="logo.png" alt="" class="logo" />
    </div>
</div>

先上 jQuery 的方法:

 代碼如下 複製代碼
$(document).ready(function(){
    $('#submitBtn').click(function() {
        var imgSrc = $(this).next().find('.logo').attr('data-src');
        $(this).next().find('.logo').attr('src', imgSrc);
    });
});

再比較一下 JavaScript 的方法:

 代碼如下 複製代碼

window.onload = function(){
    document.getElementById('submitBtn').onclick = submitBtn;
}
function submitBtn(){
    var imgTag = document.getElementById('box').getElementsByTagName('img');
    var imgSrc = imgTag[0].getAttribute('data-src');
    imgTag[0].setAttribute('src',imgSrc);
}

再來玩一下這個 DEMO,按照這個原理,發現自己寫一個 lazyload 的 jQuery 外掛程式也並不是很難嘛,哈哈~

 代碼如下 複製代碼

<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
 <meta charset="UTF-8" />
 <script type="text/javascript" src="jquery.min.1.3.1.js"></script>
 <style type="text/css">
  .box{
   padding:10px;
  }
  .picBox{
   width:265px;
   height:137px;
   border:1px dashed #999;
  }
  .picBox img{
   width:100%;
   height:100%;
   display:block;
  }
 </style>
 <title>jQuery/JavaScript 實現的非同步載入圖片</title>
</head>
<body>
 <!-- S jqAjax -->
 <div id="jqAjax" class="box">
  <input type="submit" value="jQuery 非同步載入圖片" id="jqAjaxBtn" />
  <div class="picBox">
   <img data-src="logo.png" src="blank.gif" alt="" class="logo" />
  </div>
  <script type="text/javascript">
   $(document).ready(function(){
    $('#jqAjaxBtn').toggle(
     function(){
      var imgSrc = $(this).next().find('.logo').attr('data-src');
      $(this).next().find('.logo').attr('src', imgSrc);
      $(this).attr('value','再玩一次 O(∩_∩)O~');
     },
     function(){
      $(this).next().find('.logo').attr('src', 'blank.gif');
      $(this).attr('value','jQuery 非同步載入圖片');
     }
    );
   });
  </script>
 </div>
 <!-- E jqAjax -->
 
 <!-- S jsAjax -->
 <div id="jsAjax" class="box">
  <input type="submit" value="JavaScript 非同步載入圖片" id="jsAjaxBtn" />
  <div class="picBox">
   <img data-src="logo.png" src="blank.gif" alt="" class="logo" />
  </div>
  <script type="text/javascript">
   window.onload = function(){
    document.getElementById('jsAjaxBtn').onclick = jsToggle;
   }
   function jsToggle(){
    var jsImgTag = document.getElementById('jsAjax').getElementsByTagName('img')[0];
    var jsImgDataSrc = jsImgTag.getAttribute('data-src');
    var jsImgSrc = jsImgTag.getAttribute('src',2);
    if ( jsImgSrc == 'blank.gif' ){
     jsImgTag.setAttribute('src',jsImgDataSrc);
     document.getElementById('jsAjaxBtn').setAttribute('value','再玩一次 O(∩_∩)O~');
    } else {
     jsImgTag.setAttribute('src','blank.gif');
     document.getElementById('jsAjaxBtn').setAttribute('value','JavaScript 非同步載入圖片');
    }
   }
  </script>
 </div>
 <!-- E jsAjax -->
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.