最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

JS实现瀑布流布局

来源:懂视网 责编:小OO 时间:2020-11-27 22:27:31
文档

JS实现瀑布流布局

本文实例为大家分享了JS实现瀑布流布局展示的具体代码,供大家参考,具体内容如下:html部分。<;<。DOCTYPE html>;<;html lang="en">;<;head>;<;meta charset="UTF-8">;<;title>;瀑布流布局<;/title>;<;script src="wallpoll.js">;<;/script>;<;link rel="stylesheet" href="wallpoll.css" rel="external nofollow" >;<;/head>;<;body>;<;div id="main">;<;<。
推荐度:
导读本文实例为大家分享了JS实现瀑布流布局展示的具体代码,供大家参考,具体内容如下:html部分。<;<。DOCTYPE html>;<;html lang="en">;<;head>;<;meta charset="UTF-8">;<;title>;瀑布流布局<;/title>;<;script src="wallpoll.js">;<;/script>;<;link rel="stylesheet" href="wallpoll.css" rel="external nofollow" >;<;/head>;<;body>;<;div id="main">;<;<。

本文实例为大家分享了JS实现瀑布流布局展示的具体代码,供大家参考,具体内容如下

html部分

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>瀑布流布局</title>
 <script src="wallpoll.js"></script>
 <link rel="stylesheet" href="wallpoll.css" rel="external nofollow" >
</head>
<body>
 <div id="main">
 <div class="box">
 <div class="pic">
 <img src="wallpoll/0.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/0.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/1.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/2.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/3.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/4.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/5.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/6.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/7.jpg" alt="">
 </div>
 </div>

 <div class="box">
 <div class="pic">
 <img src="wallpoll/8.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/9.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/10.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/11.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/12.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/13.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/14.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/15.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/16.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/17.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/18.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/19.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/20.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/21.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/22.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/23.jpg" alt="">
 </div>
 </div>
 <div class="box">
 <div class="pic">
 <img src="wallpoll/24.jpg" alt="">
 </div>
 </div>
 </div>
</body>
</html>

css部分

*{
 padding:0px;
 margin:0px;
}
.main{
 position:relative;
}
.box{
 padding:15px 0 0 10px;
 float:left;
}
.pic{
 padding:10px;
 border: 1px solid #ccc;
 border-radius: 5px;
 box-shadow: 0 0 5px #ccc;
 /*float:left;*/
}
img{
 width:170px;
 height:auto;
}

JS部分

window.onload=function(){
 waterfall('main','box');
 var dataInt={"data":[{"src":"0.jpg"},{"src":"1.jpg"},{"src":"2.jpg"},{"src":"3.jpg"},{"src":"4.jpg"}]}
 window.onscroll=function(){
 if(checkScrollSlide){
 for(var i=0;i<dataInt.data.length;i++){
 var oparent=document.getElementById("main");
 var box=document.createElement("div");
 box.className="box";
 oparent.appendChild(box);
 var pic=document.createElement("div");
 pic.className="pic";
 box.appendChild(pic);
 var img=document.createElement("img");
 img.src="wallpoll/"+dataInt.data[i].src;
 pic.appendChild(img);
 }
 }
 waterfall('main','box');
 }
}
function waterfall(parent,child){
 var oparent=document.getElementById(parent);
 var boxs=oparent.getElementsByClassName(child);
 var boxw=boxs[0].offsetWidth;
 var cols=Math.floor(document.documentElement.clientWidth/boxw);
 oparent.style.cssText="width:"+cols*boxw+"px;margin:0 auto";
 var harr=new Array();
 for(var i=0;i<boxs.length;i++){
 if(i<cols){
 harr.push(boxs[i].offsetHeight);
 }else{
 var minH=Math.min.apply(null,harr);
 var index=getMinhIndex(harr,minH);
 boxs[i].style.position="absolute";
 boxs[i].style.top=minH+"px";
 boxs[i].style.left=boxs[index].offsetLeft+"px";
 harr[index]+=boxs[i].offsetHeight;
 }
 }
}
function getMinhIndex(arr,val){
 for(var i in arr){
 if(arr[i]===val){
 return i;
 }
 }
}
function checkScrollSlide(){
 var oparent=document.getElementById("main");
 var boxs=oparent.getElementsByClassName("box");
 var lastBox=boxs[boxs.length-1];
 var height=lastBox.offsetTop+Math.floor(lastBox.offsetHeight/2);
 var scrollHeigth=document.documentElement.scrollTop || document.body.scrollTop;
 // console.log(scrollHeigth);
 var seeHeigth=document.documentElement.clientHeigth || document.body.clientHeigth;
 return seeHeigth+scrollHeigth>height?true:false;
}

课程地址。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

JS实现瀑布流布局

本文实例为大家分享了JS实现瀑布流布局展示的具体代码,供大家参考,具体内容如下:html部分。<;<。DOCTYPE html>;<;html lang="en">;<;head>;<;meta charset="UTF-8">;<;title>;瀑布流布局<;/title>;<;script src="wallpoll.js">;<;/script>;<;link rel="stylesheet" href="wallpoll.css" rel="external nofollow" >;<;/head>;<;body>;<;div id="main">;<;<。
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top