国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

js自定義瀑布流布局插件

2019-11-19 16:34:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

瀑布流布局特點(diǎn):

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

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

(function ($) { $.fn.extend({  myWaterfull: function () {   var parentWidth = $(this).width(); //獲取每行的寬度   var childItems = $(this).children();   var childWidth = childItems.width(); //獲取每一列的列寬   var column = 5; //定義每行有多少列   //計(jì)算并設(shè)置列與列之間的間隙   var space = (parentWidth - column * childWidth) / (column - 1);   //聲明一個(gè)數(shù)組,用來(lái)存放第一行中每一列的高度   var arrHeight = [];   //對(duì)子元素進(jìn)行排列布局   $.each(childItems, function (index, item) {    if (index < column) { //對(duì)第一行的列進(jìn)行排列布局     $(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;      }     }     //對(duì)余下的子元素挨個(gè)排列布局     $(item).css({      top: minValue + space,      left: minIndex * (childWidth + space)     });     //更新最小列高     arrHeight[minIndex] += $(item).height() + space;    }   });   //由于這里是利用定位對(duì)子元素進(jìn)行布局,所以要更新父元素的高度   //當(dāng)然也可以利用浮動(dòng)對(duì)子元素進(jìn)行布局   var maxHeight = 0;   for (var i = 0; i < arrHeight.length; i++) {    if (maxHeight < arrHeight[i]) {     maxHeight = arrHeight[i];    }   }   //設(shè)置父元素的高度   $(this).height(maxHeight);  } });})(jQuery);

使用示例:

這里假設(shè)一個(gè)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ū)寫(xiě)腳本文件,這里假設(shè)從后臺(tái)獲取子元素的數(shù)據(jù),并用artTemplate模板引擎將數(shù)據(jù)渲染到頁(yè)面:

<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>//書(shū)寫(xiě)腳本$(function () { //根據(jù)接口文檔,向服務(wù)器請(qǐng)求數(shù)據(jù) var page = 1, pageSize = 20; //當(dāng)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ā)送請(qǐng)求前改變按鈕的內(nèi)容    $(".btn").html("正在加載...").addClass("loading");   },   success: function (data) {    //2.借助模板引擎,渲染數(shù)據(jù)    var tplStr = template("template", data);    $(".items").append(tplStr);    $(".items").myWaterfull();    //當(dāng)加載完成后,改變按鈕內(nèi)容和樣式    $(".btn").html("加載更多").removeClass("loading");    //當(dāng)后臺(tái)數(shù)據(jù)展示完畢時(shí),向用戶(hù)提示    if (data.items.length < pageSize) {     $(".btn").html("沒(méi)有更多內(nèi)容了").addClass("loading");    }    //每次響應(yīng)成功后,將從后臺(tái)返回的page保存起來(lái)    page = data.page;   }  }); } //當(dāng)點(diǎn)擊按鈕時(shí),加載下一頁(yè)數(shù)據(jù) $(".btn").on('click',function () {  if($(".btn").hasClass("loading")) return false;  render(); }); //當(dāng)頁(yè)面滾動(dòng)到數(shù)據(jù)底部的時(shí)候加載數(shù)據(jù) $(window).on('scroll',function () {  //判斷是否滾動(dòng)到底部  var isBottom = ($('.items').height()-($(window).height()-$('.items').offset().top))-$(window).scrollTop();  if (isBottom <= 200 && !$('.btn').hasClass("loading")) render(); });});

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁化县| 平阴县| 永修县| 淄博市| 浦江县| 仙居县| 昌平区| 郎溪县| 宜阳县| 梅州市| 老河口市| 罗田县| 九江市| 溆浦县| 新沂市| 江西省| 长兴县| 寻甸| 丹棱县| 湖南省| 尉犁县| 谢通门县| 普陀区| 威信县| 板桥市| 庄河市| 遂川县| 金门县| 托克逊县| 抚顺市| 黔西县| 涿州市| 弥渡县| 汕尾市| 建宁县| 新安县| 祁门县| 马鞍山市| 龙江县| 克什克腾旗| 蓝田县|