原生JS實(shí)現(xiàn)響應(yīng)式瀑布流布局
原生JS實(shí)現(xiàn)的瀑布流布局,代碼及demo代碼地址:https://github.com/leozdgao/responsive_waterfall
Demo:http://leozdgao.github.io/demo/responsive_waterfall/
演示圖:

核心代碼:
responsive_waterfall.js
(function() {
var Waterfall = function(opts) {
var minBoxWidth;
Object.defineProperty(this, 'minBoxWidth', {
get: function() { return minBoxWidth; },
set: function(value) {
if(value < 100) value = 100;
if(value > 1000) value = 1000;
minBoxWidth = value;
}
});
opts = opts || {};
var containerSelector = opts.containerSelector || '.wf-container';
var boxSelector = opts.boxSelector || '.wf-box';
// init properties
this.minBoxWidth = opts.minBoxWidth || 250;
this.columns = [];
this.container = document.querySelector(containerSelector);
this.boxes = this.container ?
Array.prototype.slice.call(this.container.querySelectorAll(boxSelector)) : [];
// compose once in constructor
this.compose();
// handle the image or something which might change size after loaded
var images = this.container.querySelectorAll('img'), that = this;
var clr;
for (var i = 0; i < images.length; i++) {
var img = images[i];
img.onload = function() {
if(clr) clearTimeout(clr);
clr = setTimeout(function() {
that.compose(true);
}, 500);
}
}
window.addEventListener('resize', function() {
that.compose();
});
}
// compute the number of columns under current setting
Waterfall.prototype.computeNumberOfColumns = function() {
var num = Math.floor(this.container.clientWidth / this.minBoxWidth);
num = num || 1; // at least one column
return num;
}
// init enough columns and set the width
Waterfall.prototype.initColumns = function(num) {
if(num > 0) {
// create column div
this.columns = [];
var width = (100 / num) + '%';
while(num--) {
var column = document.createElement('div');
column.className = 'wf-column';
column.style.width = width;
this.columns.push(column);
this.container.appendChild(column);
}
}
}
// get the index of shortest column
Waterfall.prototype.getMinHeightIndex = function() {
if(this.columns && this.columns.length > 0) {
var min = this.columns[0].clientHeight, index = 0;
for (var i = 1; i < this.columns.length; i++) {
var columnElem = this.columns[i];
if(columnElem.clientHeight < min) {
min = columnElem.clientHeight;
index = i;
}
}
return index;
}
else return -1;
}
// compose core
Waterfall.prototype.compose = function(force) {
var num = this.computeNumberOfColumns();
var cols = this.columns.length;
if(force || num != cols) {
// remove old column
for (var i = 0; i < this.columns.length; i++) {
var columnElem = this.columns[i];
columnElem.remove();
}
// init new column
this.initColumns(num);
// compose
for (var i = 0, l = this.boxes.length; i < l; i++) {
var box = this.boxes[i];
this.addBox(box);
}
}
}
// add a new box to grid
Waterfall.prototype.addBox = function(elem) {
// push if new box
if(this.boxes.indexOf(elem) < 0) this.boxes.push(elem);
var columnIndex = this.getMinHeightIndex();
if(columnIndex > -1) {
var column = this.columns[columnIndex];
column.appendChild(elem);
}
}
window.Waterfall = Waterfall;
})()
以上所述就是本文給大家分享的全部內(nèi)容了,希望能夠?qū)Υ蠹沂炀毷褂胘avascript有所幫助。
相關(guān)文章
js格式化貨幣數(shù)據(jù)實(shí)現(xiàn)代碼
貨幣數(shù)據(jù)想要一某種形式在頁面中顯示的話,首先是必須要格式化的,下面為大家介紹下具體的格式化代碼,感興趣的朋友可以參考下2013-09-09
javascript轉(zhuǎn)換字符串為dom對象(字符串動態(tài)創(chuàng)建dom)
那么今天的目的就是教大家怎么去實(shí)現(xiàn)一個(gè)這樣的方法用來把字符串直接轉(zhuǎn)換為標(biāo)準(zhǔn)的dom對象2010-05-05
JavaScript中實(shí)現(xiàn)sprintf、printf函數(shù)
這篇文章主要介紹了JavaScript中實(shí)現(xiàn)sprintf、printf函數(shù),這兩個(gè)函數(shù)在大多數(shù)編程語言中都有,但JS中卻沒有,本文介紹在js中實(shí)現(xiàn)這兩個(gè)函數(shù)功能,需要的朋友可以參考下2015-01-01
JavaScript類型系統(tǒng)之布爾Boolean類型詳解
這篇文章主要介紹了JavaScript類型系統(tǒng)之布爾Boolean類型詳解的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
JavaScript基礎(chǔ)知識及常用方法總結(jié)
JAVASCRIPT是AJAX技術(shù)中不可或缺的一部分,所以想學(xué)好AJAX以及現(xiàn)在流行的AJAX框架,學(xué)好JAVASCRIPT是最重要的,通過本篇文章給大家介紹javascript基礎(chǔ)知識及常用方法總結(jié),對js基礎(chǔ)知識及常用方法相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01
當(dāng)前流行的JavaScript代碼風(fēng)格指南
這篇文章主要介紹了當(dāng)前流行的JavaScript代碼風(fēng)格指南,同時(shí)推薦了一款風(fēng)格檢驗(yàn)工具jshint,可以配合大多數(shù)的編輯器統(tǒng)一團(tuán)隊(duì)代碼風(fēng)格,需要的朋友可以參考下2014-09-09

