这篇文章主要介绍了JS如何实现瀑布流布局,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都做网站、余杭网络推广、重庆小程序开发、余杭网络营销、余杭企业策划、余杭品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供余杭建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
1、能够嵌入动态文本于HTML页面。2、对浏览器事件做出响应。3、读写HTML元素。4、在数据被提交到服务器之前验证数据。5、检测访客的浏览器信息。6、控制cookies,包括创建和修改等。7、基于Node.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; }
感谢你能够认真阅读完这篇文章,希望小编分享的“JS如何实现瀑布流布局”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
网站栏目:JS如何实现瀑布流布局
当前网址:https://www.cdcxhl.com/article22/jscsjc.html
成都网站建设公司_创新互联,为您提供品牌网站建设、商城网站、小程序开发、网站策划、网页设计公司、品牌网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联