<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>瀑布流-javascript</title>
<style>
*{margin:0;padding:0;}
#content{position: relative;margin:0 auto;}
.box{padding:10px;float: left;}/*首行浮动,第二行开始绝对定位*/
.box img{width: 180px;height:auto;display: block;}
</style>
<script>
window.onload=function(){
waterfall('content','box');
//改变窗口大小时,重新排列
window.onresize = function(){
waterfall('content','box');
}
//如果数据不够,没出现滚动条,自动加载数据
var time=setInterval(function(){
if(checkscrollside()){
addDate();//插入数据
waterfall('content','box');//加载完数据从新排列
}else{
clearInterval(time);
window.onscroll=function(){
if(checkscrollside()){
addDate();
waterfall('content','box');
};
}
}
},1000)
}
// 数据插入
function addDate(){
var dataInt=['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg'];//模拟数据,也可以是对象
var oParent = document.getElementById('content');
for(var i=0;i<dataInt.length;i++){//循环插入数据
var oBox=document.createElement('div');
oBox.className='box';
oParent.appendChild(oBox);
var oImg=document.createElement('img');
oImg.src='./img/'+dataInt[i];
oBox.appendChild(oImg);
}
}
//主函数
function waterfall(parentID,childClass){
var oParent=document.getElementById(parentID);
var arrBox=getClassObj(parentID,childClass);// getClassObj()获取子class的数组
var iBoxW=arrBox[0].offsetWidth;// 获取瀑布流块的宽度
var num=Math.floor(document.documentElement.clientWidth/iBoxW);//计算窗口能容纳几列
oParent.style.width=iBoxW*num+'px';//设置父级宽度
var arrBoxH=[];//数组,用于存储每列中的所有块框相加的高度
for(var i=0;i<arrBox.length;i++){//遍历数组瀑布流 块
var boxH=arrBox[i].offsetHeight;//获取当前块的高度
if(i<num){
arrBox[i].style.cssText="";//防止用户改变窗口大小,到时样式出错
arrBoxH[i]=boxH; //第一行中的num个块box 先添加进数组arrBoxH
}else{
var minH=Math.min.apply(null,arrBoxH);//获取数组arrBoxH中的最小值minH
var minHIndex=getminHIndex(arrBoxH,minH);//遍历数组获取最小值minH的索引
arrBox[i].style.position='absolute';//设置绝对位移
arrBox[i].style.top=minH+'px';
arrBox[i].style.left=minHIndex*iBoxW+'px';//也可以直接获取arrBox[minHIndex].offsetLeft
arrBoxH[minHIndex]+=arrBox[i].offsetHeight;//添加后,更新最小列高
}
}
}
//获取子class的数组
function getClassObj(parentID,childClass){
var oParent=document.getElementById(parentID);
var allChildObj=oParent.getElementsByTagName('*');//获取父级下的所有子集
var childObj=[];//创建一个数组 用于收集子元素
for (var i=0;i<allChildObj.length;i++) {//遍历子元素、判断类别、压入数组
if (allChildObj[i].className==childClass){
childObj.push(allChildObj[i]);
}
};
return childObj;
}
//获取数组最小值的索引
function getminHIndex(arr,minH){
for(var i in arr){
if(arr[i]==minH){
return i;
}
}
}
// 判断滚动条是否到底部
function checkscrollside(){
var arrBox=getClassObj("content",'box');
//获取最后一个瀑布流块的高度:距离网页顶部(实现未滚到底就开始加载)
var lastBoxH=arrBox[arrBox.length-1].offsetTop;
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//获取滚动条卷走的高度
var documentH=document.documentElement.clientHeight;//显示页面文档的高
return (lastBoxH<scrollTop+documentH)?true:false;//到达指定高度后 返回true,触发waterfall()函数
}
</script>
</head>
<body>
<div id="content">
<div class="box"><img src="img/0.jpg" alt=""></div>
<div class="box"><img src="img/1.jpg" alt=""></div>
<div class="box"><img src="img/2.jpg" alt=""></div>
<div class="box"><img src="img/3.jpg" alt=""></div>
<div class="box"><img src="img/4.jpg" alt=""></div>
<div class="box"><img src="img/5.jpg" alt=""></div>
<div class="box"><img src="img/6.jpg" alt=""></div>
<div class="box"><img src="img/7.jpg" alt=""></div>
<div class="box"><img src="img/8.jpg" alt=""></div>
<div class="box"><img src="img/9.jpg" alt=""></div>
<div class="box"><img src="img/10.jpg" alt=""></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>瀑布流-jquery</title>
<style>
*{margin:0;padding:0;}
#content{position: relative;margin:0 auto;}
.box{padding:10px;float: left;}
.box img{width: 180px;height:auto;display: block;}
</style>
<script src="js/jquery-1.11.1.min.js"></script>
<script>
$(function(){
waterfall();
//改变窗口大小时,重新排列
$(window).resize(function(){
waterfall();
})
//如果数据不够,没出现滚动条,自动加载数据
var time=setInterval(function(){
if(checkscrollside()){
addDate();//插入数据
waterfall();//加载完数据从新排列
}else{
clearInterval(time);
$(window).scroll(function(){
if(checkscrollside()){
addDate();
waterfall();
};
})
}
},1000)
})
// 数据插入
function addDate(){
var dataInt=['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg'];//模拟数据,也可以是对象
var oParent = $('#content');
for(var i=0;i<dataInt.length;i++){//循环插入数据
oParent.append('<div class="box"><img src="./img/'+dataInt[i]+'" alt=""></div>');
}
}
//主函数
function waterfall(){
var arrBox=$('#content').children('.box');// box对象
var iBoxW=arrBox.eq(0).innerWidth();// 获取瀑布流块的宽度,注意width(),跟innerWidth()的区别
var num=Math.floor($(window).width()/iBoxW);//计算窗口能容纳几列
$('#content').css('width',iBoxW*num);//设置父级宽度
var arrBoxH=[];//数组,用于存储每列中的所有块框相加的高度
for(var i=0;i<arrBox.length;i++){//遍历数组瀑布流 块
var boxH=arrBox.eq(i).innerHeight();//获取当前块的高度
if(i<num){
arrBox.eq(i).attr('style','');//防止用户改变窗口大小,到时样式出错
arrBoxH[i]=boxH; //第一行中的num个块box 先添加进数组arrBoxH
}else{
var minH=Math.min.apply(null,arrBoxH);//获取数组arrBoxH中的最小值minH
var minHIndex=$.inArray(minH,arrBoxH);//使用jquery提供的工具
arrBox.eq(i).css({'position':'absolute','top':minH,'left':minHIndex*iBoxW});//设置定位
arrBoxH[minHIndex]+=arrBox.eq(i).innerHeight();//添加后,更新最小列高
}
}
}
// 判断滚动条是否到底部
function checkscrollside(){
var arrBox=$('#content').children('.box');
//获取最后一个瀑布流块的高度:距离网页顶部(实现未滚到底就开始加载)
var lastBoxH=arrBox.eq(arrBox.length-1).offset().top;
var scrollTop=$(window).scrollTop()//获取滚动条卷走的高度
var documentH=$(window).height();;//显示页面文档的高
return (lastBoxH<scrollTop+documentH)?true:false;//到达指定高度后 返回true,触发waterfall()函数
}
</script>
</head>
<body>
<div id="content">
<div class="box"><img src="img/0.jpg" alt=""></div>
<div class="box"><img src="img/1.jpg" alt=""></div>
<div class="box"><img src="img/2.jpg" alt=""></div>
<div class="box"><img src="img/3.jpg" alt=""></div>
<div class="box"><img src="img/4.jpg" alt=""></div>
<div class="box"><img src="img/5.jpg" alt=""></div>
<div class="box"><img src="img/6.jpg" alt=""></div>
<div class="box"><img src="img/7.jpg" alt=""></div>
<div class="box"><img src="img/8.jpg" alt=""></div>
<div class="box"><img src="img/9.jpg" alt=""></div>
<div class="box"><img src="img/10.jpg" alt=""></div>
</div>
</body>
</html>
var arrBox=getClassObj(parentID,childClass);// getClassObj()获取子class的数组 var iBoxW=arrBox[0].offsetWidth;// 获取瀑布流块的宽度 var num=Math.floor(document.documentElement.clientWidth/iBoxW);//计算窗口能容纳几列 oParent.style.width=iBoxW*num+'px';//设置父级宽度
arrBox[i].style.position='absolute';//设置绝对位移 arrBox[i].style.top=minH+'px'; arrBox[i].style.left=minHIndex*iBoxW+'px';//也可以直接获取arrBox[minHIndex].offsetLeft arrBoxH[minHIndex]+=arrBox[i].offsetHeight;//添加后,更新最小列高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>瀑布流-css3</title>
<style>
*{margin:0;padding:0;}
#content{margin:0 auto;position: relative;width:1200px;column-count:6;-moz-column-count:6;-webkit-column-count:6;}
.box{padding:10px;width: 180px;}
.box img{width: 180px;height:auto;display: block;}
</style>
<script>
window.onload=function(){
//如果数据不够,没出现滚动条,自动加载数据
var time=setInterval(function(){
if(checkscrollside()){
addDate();//插入数据
}else{
clearInterval(time);
window.onscroll=function(){
if(checkscrollside()){
addDate();
};
}
}
},1000)
}
// 数据插入
function addDate(){
var dataInt=['1.jpg','2.jpg','3.jpg','4.jpg','5.jpg','6.jpg','7.jpg','8.jpg'];//模拟数据,也可以是对象
var oParent = document.getElementById('content');
for(var i=0;i<dataInt.length;i++){//循环插入数据
var oBox=document.createElement('div');
oBox.className='box';
oParent.appendChild(oBox);
var oImg=document.createElement('img');
oImg.src='./img/'+dataInt[i];
oBox.appendChild(oImg);
}
}
//获取子class的数组
function getClassObj(parentID,childClass){
var oParent=document.getElementById(parentID);
var allChildObj=oParent.getElementsByTagName('*');//获取父级下的所有子集
var childObj=[];//创建一个数组 用于收集子元素
for (var i=0;i<allChildObj.length;i++) {//遍历子元素、判断类别、压入数组
if (allChildObj[i].className==childClass){
childObj.push(allChildObj[i]);
}
};
return childObj;
}
// 判断滚动条是否到底部
function checkscrollside(){
var arrBox=getClassObj("content",'box');
//获取最后一个瀑布流块的高度:距离网页顶部(实现未滚到底就开始加载)
var lastBoxH=arrBox[arrBox.length-1].offsetTop;
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//获取滚动条卷走的高度
var documentH=document.documentElement.clientHeight;//显示页面文档的高
return (lastBoxH<scrollTop+documentH)?true:false;//到达指定高度后 返回true,触发waterfall()函数
}
</script>
</head>
<body>
<div id="content">
<div class="box"><img src="img/0.jpg" alt=""></div>
<div class="box"><img src="img/1.jpg" alt=""></div>
<div class="box"><img src="img/2.jpg" alt=""></div>
<div class="box"><img src="img/3.jpg" alt=""></div>
<div class="box"><img src="img/4.jpg" alt=""></div>
<div class="box"><img src="img/5.jpg" alt=""></div>
<div class="box"><img src="img/6.jpg" alt=""></div>
<div class="box"><img src="img/7.jpg" alt=""></div>
<div class="box"><img src="img/8.jpg" alt=""></div>
<div class="box"><img src="img/9.jpg" alt=""></div>
<div class="box"><img src="img/10.jpg" alt=""></div>
</div>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有