溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-07-27 14:58:45 來源:億速云 閱讀:141 作者:小新 欄目:web開發(fā)

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

瀑布流布局是網(wǎng)頁中經(jīng)常采用的一種布局方式,其布局有如下特點:

瀑布流布局特點:

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

以下是自定義的一個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);
   //聲明一個數(shù)組,用來存放第一行中每一列的高度
   var arrHeight = [];

   //對子元素進行排列布局
   $.each(childItems, function (index, item) {
    if (index < column) { //對第一行的列進行排列布局
     $(item).css({
      top: 0,
      left: index * (childWidth + space)
     });
     arrHeight.push($(item).height() + space); //將第一行中的列的高度添加到數(shù)組中
    } else {
     //找尋列高最小的那一列
     var minIndex = 0;
     var minValue = arrHeight[minIndex];
     //循環(huán)遍歷找出最小的列高值
     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結(jié)構(gòu):

<!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>

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

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

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

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


});

以上是“js如何自定義瀑布流布局插件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI