原生js實現移動端瀑布流式程式碼範例_javascript技巧

來源:互聯網
上載者:User

瀑布流布局已成為當今非常普遍的圖片展示方式,無論是PC還是手機等行動裝置上。最近使用到了“懶載入”,現在更新一般,因為平時主要使移動端的開發所以庫檔案使用的是zepto.js 。當然也可以和jQuery 通用。
代碼如下:

function loadImgLazy(node) { var lazyNode = $('[node-type=imglazy]', node),  mobileHeight, lazyOffSetHeight, tempHeight, currentNodeTop, imgObject,  imgDataSrc, localUrl; localUrl = location.href; // 擷取當前瀏覽器可視地區的高度 mobileHeight = $(window).height(); return function(co) {  var conf = {   'loadfirst': true,   'loadimg': true  };  for (var item in conf) {   if (item in co) {    conf.item = co.item;   }  }  var that = {};  var _this = {};  /**   * [replaceImgSrc 動態替換節點中的src]   * @param {[type]} tempObject [description]   * @return {[type]}   [description]   */  _this.replaceImgSrc = function(tempObject) {   var srcValue;   $.each(tempObject, function(i, item) {    imgObject = $(item).find('img[data-lazysrc]');    imgObject.each(function(i) {     imgDataSrc = $(this).attr('data-lazysrc');     srcValue = $(this).attr('src');     if (srcValue == '#') {      if (imgDataSrc) {       $(this).attr('src', imgDataSrc);       $(this).removeAttr('data-lazysrc');      }     }    });   });  };  /**   * 首屏判斷螢幕上是否出現imglazy節點,有的話就載入圖片   * @param {[type]} i) {     currentNodeTop [description]   * @return {[type]} [description]   */  _this.loadFirstScreen = function() {   if (conf.loadfirst) {    lazyNode.each(function(i) {     currentNodeTop = $(this).offset().top;     if (currentNodeTop < mobileHeight + 800) {      _this.replaceImgSrc($(this));     }    });   }  };  //當載入過首屏以後按照隊列載入圖片  _this.loadImg = function() {   if (conf.loadimg) {    $(window).on('scroll', function() {     var imgLazyList = $('[node-type=imglazy]', node);     for (var i = 0; i < 5; i++) {      _this.replaceImgSrc(imgLazyList.eq(i));     }    });   }  };  that = {   replaceImgSrc: _this.replaceImgSrc(),   mobileHeight: mobileHeight,   objIsEmpty: function(obj) {    for (var item in obj) {     return false;    }    return true;   },   destory: function() {    if (_this) {     $.each(_this, function(i, item) {      if (item && item.destory) {       item.destory();      }     });     _this = null;    }    $(window).off('scroll');   }  };  return that; };}

以上所述就是本文給大家分享的全部內容了,希望能夠對大家熟練使用javascript有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.