javascript实现瀑布流动态加载图片原理
作者:回忆没了焦点 时间:2024-06-05 09:13:54
本文实例为大家分享了js瀑布流加载效果,动态加载图片,供大家参考,具体内容如下
鼠标滚动事件,当鼠标滚动到下边,动态加载图片。
1. HTML代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js实现瀑布流效果-动态加载图片</title>
<link rel="stylesheet" href="css/waterfallflow.css" type="text/css" />
<script src="js/waterfallflow.js"></script>
</head>
<body>
<div id="container">
<div class="box">
<div class="box_img">
<img src="img/1.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/2.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/3.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/4.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/5.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/6.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/7.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/3.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/1.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/2.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/1.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/2.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/3.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/4.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/5.jpg" />
</div>
</div>
<div class="box">
<div class="box_img">
<img src="img/6.jpg" />
</div>
</div>
</div>
</body>
</html>
2. CSS代码
*{
margin: 0px;
padding: 0px;
}
#container{
position: relative;
}
.box{
padding: 5px;
float: left;
margin: 0px auto;
}
.box_img{
padding: 5px;
border: 1px solid #DCDCDC;
box-shadow: 0 0 5px #ccc;
border-radius: 5px;
}
.box_img img{
width: 230px;
}
3. JavaScript代码
window.onload=function(){
imgLocation("container","box");
var imgData={"data":[{"src":"1.jpg"},{"src":"2.jpg"},{"src":"3.jpg"},{"src":"3.jpg"},{"src":"4.jpg"},{"src":"5.jpg"},{"src":"6.jpg"},{"src":"2.jpg"},{"src":"3.jpg"},{"src":"3.jpg"},{"src":"4.jpg"},{"src":"5.jpg"}]}
window.onscroll=function(){
// console.log(document.documentElement.scrollTop);
if(checkFlag()){
var cparent=document.getElementById("container");
for(var i=0;i<imgData.data.length;i++){
var ccontent=document.createElement("div");
ccontent.className="box";
cparent.appendChild(ccontent);
var boximg=document.createElement("div");
boximg.className="box_img";
ccontent.appendChild(boximg);
var img=document.createElement("img");
img.src="img/"+imgData.data[i].src;
boximg.appendChild(img);
//另外一种方法在div后边追加内容,不覆盖原有内容
// var content="<div class='box'><div class='box_img'><img src='img/"+imgData.data[i].src+"'/></div></div>";
// cparent.innerHTML+=content;
}
imgLocation("container","box");
}
}
}
function checkFlag(){
var cparent=document.getElementById("container");
var ccontent=getChildElement(cparent,"box");//图片的所有box数
var lastContentHeight=ccontent[ccontent.length-1].offsetTop;//最后一张图片距离顶部高度
var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//滚动条距离顶部高度
var pageHeight=document.documentElement.clientHeight||document.body.clientHeight;//屏幕高度
// console.log(lastContentHeight+","+scrollTop+","+pageHeight);
if(lastContentHeight<scrollTop+pageHeight){
return true;
}
}
function imgLocation(parent,content){
var cparent=document.getElementById(parent);
var ccontent=getChildElement(cparent,content);//图片的所有box数
var imgWidth=ccontent[0].offsetWidth;//图片宽度
var num=Math.floor(document.documentElement.clientWidth/imgWidth);//一行放图片个数
cparent.style.cssText="width:"+imgWidth*num+"px;margin:0px auto";//container的宽度
var boxHeightArr=[];//每一列box高度
for(var i=0;i<ccontent.length;i++){
if(i<num){
boxHeightArr[i]=ccontent[i].offsetHeight;
}else{
var minHeight=Math.min.apply(null,boxHeightArr);//最小高度
var minIndex = getMinheightLocation(boxHeightArr,minHeight);//得到最小高度下标
// console.log(minHeight+","+minIndex);
ccontent[i].style.position="absolute";
ccontent[i].style.top=minHeight+"px";//距离顶部高度
ccontent[i].style.left=ccontent[minIndex].offsetLeft+"px";//距离左边长度
boxHeightArr[minIndex]+=ccontent[i].offsetHeight;
// console.log(ccontent[i].offsetHeight+","+ccontent[i].height);//ccontent[i].height=undefined
}
}
}
function getMinheightLocation(boxHeightArr,minHeight){
for(var i in boxHeightArr){
if(boxHeightArr[i]==minHeight){
return i
}
}
}
function getChildElement(parent,content){
//将parent下有content的全部取出
var contentArr=[];
var allContent=parent.getElementsByTagName("*");
for(i=0;i<allContent.length;i++){
if(allContent[i].className=="box"){
contentArr.push(allContent[i]);
}
}
return contentArr;
}
标签:js,瀑布流,动态加载
0
投稿
猜你喜欢
python按照list中字典的某key去重的示例代码
2023-01-20 00:20:00
Git 教程之服务器搭建详解
2022-07-28 06:44:20
详解webpack进阶之loader篇
2024-05-11 09:43:50
Python学习之yaml文件的读取详解
2023-04-20 14:06:42
Python 错误和异常小结
2021-08-19 12:17:58
anaconda jupyter不能导入安装的lightgbm解决方案
2021-09-15 19:24:45
python 简单备份文件脚本v1.0的实例
2022-05-01 01:47:53
python实现俄罗斯方块游戏(改进版)
2022-03-10 07:55:48
python函数的重新定义及练习
2023-10-12 22:47:55
jsp+servlet简单实现上传文件功能(保存目录改进)
2023-07-20 17:57:07
JavaScript中关于base64的一些事
2024-05-02 16:20:25
“尊重”设计师?
2009-03-23 18:14:00
mysql查询时offset过大影响性能的原因和优化详解
2024-01-13 14:17:36
python实现MD5进行文件去重的示例代码
2021-12-13 02:28:23
详解MySQL与Spring的自动提交(autocommit)
2024-01-26 15:19:02
简单介绍Ruby中的CGI编程
2022-09-07 21:38:14
Vue Cli3 创建项目的方法步骤
2024-05-21 10:16:53
利用Python yagmail三行代码实现发送邮件
2021-10-16 08:39:04
一行命令搞定node.js 版本升级
2024-05-13 09:30:14
设计和布局之间的思考
2008-10-09 13:06:00