bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

js如何自定義瀑布流布局插件

這篇文章主要介紹js如何自定義瀑布流布局插件,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

駐馬店網站建設公司成都創新互聯公司,駐馬店網站設計制作,有大型網站制作公司豐富經驗。已為駐馬店1000多家提供企業網站建設服務。企業網站搭建\外貿網站建設要多少錢,請找那個售后服務好的駐馬店做網站的公司定做!

瀑布流布局是網頁中經常采用的一種布局方式,其布局有如下特點:

瀑布流布局特點:

(1)圖文元素按列排放
(2)列寬一致,但高度不等
(3)布局過程中將優先向高度最小的列補充數據

以下是自定義的一個jQuery瀑布流插件:jquery.myWaterfull.js

(function ($) {
 $.fn.extend({
  myWaterfull: function () {

   var parentWidth = $(this).width(); //獲取每行的寬度
   var childItems = $(this).children();
   var childWidth = childItems.width(); //獲取每一列的列寬
   var column = 5; //定義每行有多少列
   //計算并設置列與列之間的間隙
   var space = (parentWidth - column * childWidth) / (column - 1);
   //聲明一個數組,用來存放第一行中每一列的高度
   var arrHeight = [];

   //對子元素進行排列布局
   $.each(childItems, function (index, item) {
    if (index < column) { //對第一行的列進行排列布局
     $(item).css({
      top: 0,
      left: index * (childWidth + space)
     });
     arrHeight.push($(item).height() + space); //將第一行中的列的高度添加到數組中
    } else {
     //找尋列高最小的那一列
     var minIndex = 0;
     var minValue = arrHeight[minIndex];
     //循環遍歷找出最小的列高值
     for (var i = 0; i < arrHeight.length; i++) {
      if (minValue > arrHeight[i]) {
       minValue = arrHeight[i];
       minIndex = i;
      }
     }

     //對余下的子元素挨個排列布局
     $(item).css({
      top: minValue + space,
      left: minIndex * (childWidth + space)
     });

     //更新最小列高
     arrHeight[minIndex] += $(item).height() + space;
    }
   });

   //由于這里是利用定位對子元素進行布局,所以要更新父元素的高度
   //當然也可以利用浮動對子元素進行布局
   var maxHeight = 0;
   for (var i = 0; i < arrHeight.length; i++) {
    if (maxHeight < arrHeight[i]) {
     maxHeight = arrHeight[i];
    }
   }

   //設置父元素的高度
   $(this).height(maxHeight);

  }
 });
})(jQuery);

使用示例:

這里假設一個HTML結構:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
 <meta charset="UTF-8">
 <title>瀑布流案例原始</title>
 <style>
* {
 margin: 0;
 padding: 0;
}

body {
 font-family: Microsoft Yahei;
 background-color: #f5f5f5;
}

.container {
 width: 1200px;
 margin: 0 auto;
 padding-top: 50px;
}

.container > .items {
 position: relative;
}

.container > .items > .item {
 width: 232px;
 position: absolute;
 box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
 overflow: hidden;
}

.container > .items > .item > img {
 width: 100%;
 display: block;
 height: 232px;
}

.container > .items > .item:nth-child(3n) > img {
 width: 100%;
 display: block;
 height: 350px;
}

.container > .items > .item > p {
 margin: 0;
 padding: 10px;
 background: #fff;
}

.container > .btn {
 width: 280px;
 height: 40px;
 text-align: center;
 line-height: 40px;
 background-color: #ccc;
 border-radius: 8px;
 font-size: 24px;
 cursor: pointer;
}

.container > .loading {
 background-color: transparent;
}
</style>
</head>
<body>
<div class="container">
 <div class="items">

 </div>
 <div class="btn loading">正在加載...</div>
</div>

書寫腳本文件,這里假設從后臺獲取子元素的數據,并用artTemplate模板引擎將數據渲染到頁面:

<script src="JS/jquery.min.js"></script>
<script src="JS/jquery.myWaterfull.js"></script>
<script src="JS/template.js"></script>

//定義引擎模板
<script id="template" type="text/html">
 {{ each items as item index}}
 <div class="item">
  <img src="{{item.path}}" alt="">
  <p>{{item.text}}</p>
 </div>
 {{/each}}
</script>

//書寫腳本
$(function () {
 //根據接口文檔,向服務器請求數據
 var page = 1, pageSize = 20;
 //當DOM結構加載完畢,就調用一次render函數
 render();
 function render() {
  $.ajax({
   type: "get",
   url: "php/data.php",
   data: {
    page: page,
    pageSize: pageSize
   },
   beforeSend: function () { //在發送請求前改變按鈕的內容
    $(".btn").html("正在加載...").addClass("loading");
   },
   success: function (data) {
    //2.借助模板引擎,渲染數據
    var tplStr = template("template", data);
    $(".items").append(tplStr);
    $(".items").myWaterfull();
    //當加載完成后,改變按鈕內容和樣式
    $(".btn").html("加載更多").removeClass("loading");
    //當后臺數據展示完畢時,向用戶提示
    if (data.items.length < pageSize) {
     $(".btn").html("沒有更多內容了").addClass("loading");
    }
    //每次響應成功后,將從后臺返回的page保存起來
    page = data.page;
   }
  });
 }

 //當點擊按鈕時,加載下一頁數據
 $(".btn").on('click',function () {
  if($(".btn").hasClass("loading")) return false;
  render();
 });

 //當頁面滾動到數據底部的時候加載數據
 $(window).on('scroll',function () {
  //判斷是否滾動到底部
  var isBottom = ($('.items').height()-($(window).height()-$('.items').offset().top))-$(window).scrollTop();
  if (isBottom <= 200 && !$('.btn').hasClass("loading")) render();
 });


});

以上是“js如何自定義瀑布流布局插件”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注創新互聯行業資訊頻道!

當前題目:js如何自定義瀑布流布局插件
網頁路徑:http://vcdvsql.cn/article18/jhgddp.html

成都網站建設公司_創新互聯,為您提供電子商務軟件開發微信公眾號域名注冊關鍵詞優化面包屑導航

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都app開發公司