原生JS實(shí)現(xiàn)美圖瀑布流布局賞析
自pinterest網(wǎng)站爆紅以來,國內(nèi)一度掀起“仿PIN”狂潮,諸如花瓣、蘑菇街等等。正是如此,“瀑布流”式布局受到廣大網(wǎng)民的青睞。眾多知名JS庫,也相繼出現(xiàn)“瀑布流”布局插件,譬如jQuery的Masonry插件、KISSY的waterfall插件等。今天閑來無聊,我也自己動(dòng)手弄了段原生JS代碼,實(shí)現(xiàn)了簡(jiǎn)單的“瀑布流”布局效果,當(dāng)然肯定不能和以上那些優(yōu)秀插件相提并論,有興趣的朋友,可以去看看,希望能帶給你或多或少的收獲。
1. js代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-">
<title>Waterfall代碼</title>
</head>
<body>
<style type="text/css">
.wf-main{
position: relative;
margin: auto;
width: px;
overflow: hidden;
}
.wf-main .wf-cld{
position: absolute;
margin-bottom: px;
padding:px px;
width: px;
left: -px;
top: -px;
line-height:px;
border: px solid #;
border-radius: px;
background-color: #ccc;
overflow: hidden;
}
.wf-cld .inner{
position: absolute;
left: -px;
top: -px;
margin-bottom: px;
width: px;
overflow: hidden;
border: px solid #f;
border-radius: px;
}
.wf-cld .title{
margin: px;
padding: px;
width: px;
color: #f;
font-size: px;
}
</style>
<div class="wf-main" id="wf-main">
<div class="wf-cld"><h style="color:#f">、瀑布流</h></div>
<div class="wf-cld"><br></div>
<div class="wf-cld"><br><br></div>
<div class="wf-cld"><br><br><br></div>
<div class="wf-cld"><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld" id="wf-inner">
<h class="inner title">、內(nèi)部瀑布流</h>
<div class="inner">-<br></div>
<div class="inner">-</div>
<div class="inner">-</div>
<div class="inner">-</div>
<div class="inner">-<br></div>
<div class="inner">-</div>
<div class="inner">-</div>
<div class="inner">-</div>
<div class="inner">-</div>
</div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
<div class="wf-cld"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>
</div>
<script type="text/javascript">
function Waterfall(param){
this.id = typeof param.container == 'string' ? document.getElementById(param.container) : param.container;
this.colWidth = param.colWidth;
this.colCount = param.colCount || ;
this.cls = param.cls && param.cls != '' ? param.cls : 'wf-cld';
this.init();
}
Waterfall.prototype = {
getByClass:function(cls,p){
var arr = [],reg = new RegExp("(^|\\s+)" + cls + "(\\s+|$)","g");
var nodes = p.getElementsByTagName("*"),len = nodes.length;
for(var i = ; i < len; i++){
if(reg.test(nodes[i].className)){
arr.push(nodes[i]);
reg.lastIndex = ;
}
}
return arr;
},
maxArr:function(arr){
var len = arr.length,temp = arr[];
for(var ii= ; ii < len; ii++){
if(temp < arr[ii]){
temp = arr[ii];
}
}
return temp;
},
getMar:function(node){
var dis = ;
if(node.currentStyle){
dis = parseInt(node.currentStyle.marginBottom);
}else if(document.defaultView){
dis = parseInt(document.defaultView.getComputedStyle(node,null).marginBottom);
}
return dis;
},
getMinCol:function(arr){
var ca = arr,cl = ca.length,temp = ca[],minc = ;
for(var ci = ; ci < cl; ci++){
if(temp > ca[ci]){
temp = ca[ci];
minc = ci;
}
}
return minc;
},
init:function(){
var _this = this;
var col = [],//列高
iArr = [];//索引
var nodes = _this.getByClass(_this.cls,_this.id),len = nodes.length;
for(var i = ; i < _this.colCount; i++){
col[i] = ;
}
for(var i = ; i < len; i++){
nodes[i].h = nodes[i].offsetHeight + _this.getMar(nodes[i]);
iArr[i] = i;
}
for(var i = ; i < len; i++){
var ming = _this.getMinCol(col);
nodes[i].style.left = ming * _this.colWidth + "px";
nodes[i].style.top = col[ming] + "px";
col[ming] += nodes[i].h;
}
_this.id.style.height = _this.maxArr(col) + "px";
}
};
new Waterfall({
"container":"wf-inner",
"colWidth":,
"colCount":,
"cls":"inner"
});
new Waterfall({
"container":"wf-main",
"colWidth":,
"colCount":
});
</script>
</body>
</html>
2. [圖片] 瀑布流.jpg

- 原生JS實(shí)現(xiàn)響應(yīng)式瀑布流布局
- 解析瀑布流布局:JS+絕對(duì)定位的實(shí)現(xiàn)
- 微信小程序通過js實(shí)現(xiàn)瀑布流布局詳解
- 純js實(shí)現(xiàn)瀑布流布局及ajax動(dòng)態(tài)新增數(shù)據(jù)
- JavaScript瀑布流布局實(shí)現(xiàn)代碼
- 詳解javascript實(shí)現(xiàn)瀑布流絕對(duì)式布局
- 原生js實(shí)現(xiàn)瀑布流布局
- JS實(shí)現(xiàn)瀑布流布局
- javascript瀑布流布局實(shí)現(xiàn)方法詳解
- js實(shí)現(xiàn)瀑布流布局(無限加載)
相關(guān)文章
uni-app如何用JS動(dòng)態(tài)修改scss樣式變量
對(duì)于uni-app中的組件,可以通過修改它們的樣式來自定義它們的外觀,下面這篇文章主要給大家介紹了關(guān)于uni-app如何用JS動(dòng)態(tài)修改scss樣式變量的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
javascript中undefined與null的區(qū)別
在JavaScript中存在這樣兩種原始類型:Null與Undefined。這兩種類型常常會(huì)使JavaScript的開發(fā)人員產(chǎn)生疑惑,在什么時(shí)候是Null,什么時(shí)候又是Undefined?2015-08-08
JavaScript筆記之?dāng)?shù)據(jù)屬性和存儲(chǔ)器屬性
本文給大家介紹js數(shù)據(jù)屬性和存儲(chǔ)器屬性,及兩種屬性的區(qū)別,對(duì)js數(shù)據(jù)屬性存儲(chǔ)器屬性相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)2016-03-03
JavaScript自定義錯(cuò)誤與擴(kuò)展Error的示例詳解
在任何應(yīng)用程序中,正確處理錯(cuò)誤都是至關(guān)重要的,JavaScript 提供了一個(gè)標(biāo)準(zhǔn)的 Error 對(duì)象來管理錯(cuò)誤,但在實(shí)際開發(fā)中,通常需要針對(duì)特定場(chǎng)景定制錯(cuò)誤處理,這時(shí),自定義錯(cuò)誤和擴(kuò)展 Error 類就顯得尤為重要,本文給大家介紹了JavaScript自定義錯(cuò)誤與擴(kuò)展Error的示例2025-01-01
Egg.js構(gòu)建一個(gè)stream流式接口服務(wù)實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Egg.js構(gòu)建一個(gè)stream流式接口服務(wù)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
教你如何自定義百度分享插件以及bshare分享插件的分享按鈕
在項(xiàng)目中我們常用到百度分享插件或者bshare分享插件,雖然官方都有自定義按鈕的功能,但是畢竟還是只有少數(shù)幾種,我們?nèi)绾蝸碇谱饔凶约禾厣姆窒戆粹o呢?2014-06-06
JS中比Switch...Case更優(yōu)雅的多條件判斷寫法
這篇文章主要給大家介紹了關(guān)于JS中比Switch...Case更優(yōu)雅的多條件判斷寫法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
window.open()實(shí)現(xiàn)post傳遞參數(shù)
本文主要向大家介紹了如何使用window.open()實(shí)現(xiàn)post傳遞參數(shù)的方法,思路是參考的一位網(wǎng)友的,然后根據(jù)自己的項(xiàng)目需求做了些調(diào)整,這里同樣分享給大家,希望對(duì)大家能夠有所幫助。2015-03-03

