瀑布流就是像瀑布一样的网站——丰富的网站内容,特别是绚美的图片会让你流连忘返。你在浏览网站的时候只需要轻轻滑动一下鼠标滚轮,一切的美妙的图片精彩便可呈现在你面前。瀑布流网站是新兴的一种网站模式——她的典型代表是pinterest、美丽说、蘑菇街这类型的网站。
<div id="container">
<div class="box">
<div class="content">
<img src="../imgs/Girls/01.jpg">
</div>
</div>
...
</div>
.box { position: relative; float: left; }
.content { padding: 2px; border: 1px solid #ccc; border-radius: 2px; }
.content img { width: 234px; height: auto; }
#container { background: #fff none repeat scroll 0 0; margin: 0 auto; width: auto; }
function imgLocation() {
var box = $(".box");
var boxWidth = box.eq(0).width(); //获取第一张图片的宽度
var num = Math.floor($(window).width()/boxWidth); //确定一排能放多少张图片
var container = num * boxWidth+"px";
$("#container").css("max-width",container);
var boxArr = []; //获取盒子高度
box.each(function (index, value) {
var boxHeight = box.eq(index).height();
if(index < num){
boxArr[index] = boxHeight;
}else {
var minboxHeight = Math.min.apply(null,boxArr); //获取最小高度
var minboxIndex = $.inArray(minboxHeight,boxArr);
//通过位置进行摆放
$(value).css({
"position":"absolute",
"top":minboxHeight,
"left":box.eq(minboxIndex).position().left
});
//重新计算高度
boxArr[minboxIndex] += box.eq(index).height();
}
});
}
function scrollSide() {
var box = $(".box");
var lastBoxHeight = box.last().get(0).offsetTop + Math.floor( box.last().height()/2);
// 当前页面的高度
var documentHeight = $(window).height();
// 鼠标滚动的高度
var scrollHeight = $(window).scrollTop();
return (lastBoxHeight < (scrollHeight + documentHeight))?true:false; //是否允许滚动
}
//监听鼠标监听事件
window.onscroll = function () {
//最后一张图片出现一半时加载
if(scrollSide()){
$.each(dataImg.data,function (index, value) {
var box = $("<div>").addClass("box").appendTo($("#container"));
var content = $("<div>").addClass("content").appendTo(box);
$("<img>").attr("src",$(value).attr("src")).appendTo(content);
});
imgLocation();
}
};
//初始化图片
function initializeImgs() {
$.ajax({
type:'GET',
url:url4girls,
dateType:'xml',
success:function (data) {
addImgBox(data);
}
});
};
function addImgBox(data) {
var arr = $(data).find('string');
$.each(arr,function (index, value) {
var box = $("<div>").addClass("box").appendTo($("#container"));
var content = $("<div>").addClass("content").appendTo(box);
$("<img>").attr("src",$(value).text()).appendTo(content);
});
imgLocation();
}
index_by_jQuery.html
index_by_jQuery.js
index_by_jQuery_Ajax.html
index_by_jQuery_Ajax.js
waterfall.css
jquery-3.1.1.min.js