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é)構:
<!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é)構加載完畢,就調(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();
});
});
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
微信小程序?qū)崿F(xiàn)搜索功能并跳轉(zhuǎn)搜索結(jié)果頁面
本文主要介紹了微信小程序?qū)崿F(xiàn)搜索功能并跳轉(zhuǎn)搜索結(jié)果頁面,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
javascript中的 object 和 function小結(jié)
JavaScript的面向?qū)ο笫腔谠蔚?,所有對象都有一條屬于自己的原型鏈。Object與Function可能很多看Object instanceof Function , Function instanceof Object都為true而迷惑,所以首先看下對象的實例。2016-08-08
JS+CSS實現(xiàn)鼠標經(jīng)過彈出一個DIV框完整實例(帶緩沖動畫漸變效果)
這篇文章主要介紹了JS+CSS實現(xiàn)鼠標經(jīng)過彈出一個DIV框的實現(xiàn)方法,帶緩沖漸變動畫效果,涉及鼠標事件的響應及結(jié)合時間函數(shù)定時觸發(fā)形成動畫漸變效果的相關技巧,需要的朋友可以參考下2016-03-03
javascript中強制執(zhí)行toString()具體實現(xiàn)
Javascript通常會根據(jù)方法或運算符的需要而自動把值轉(zhuǎn)成所需的類型,這可能導致各種錯誤,接下來為大家介紹下javascript如何強制執(zhí)行toString(),感興趣的朋友可以參考下哈2013-04-04
Javascript下的urlencode編碼解碼方法附decodeURIComponent
而本文,就大概說說如何在js中通過系統(tǒng)自帶的函數(shù)去解決這個問題。2010-04-04
js 禁止選擇功能實現(xiàn)代碼(兼容IE/Firefox)
有時候出于某種需要,不希望用戶可以選擇某個區(qū)域,進行下面的操作,這里給出簡單的代碼。2010-04-04

