瀑布流布局是網(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=http://www.3lian.com/edu/2017/05-17/"JS/jquery.min.js"></script> <script src=http://www.3lian.com/edu/2017/05-17/"JS/jquery.myWaterfull.js"></script> <script src=http://www.3lian.com/edu/2017/05-17/"JS/template.js"></script> //定義引擎模板 <script id="template" type="text/html"> {{ each items as item index}} <div class="item"> <img src=http://www.3lian.com/edu/2017/05-17/"{{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(); }); });