這篇文章主要介紹了JS如何實現(xiàn)瀑布流布局,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網綜合服務,包含不限于成都網站建設、成都做網站、余杭網絡推廣、重慶小程序開發(fā)、余杭網絡營銷、余杭企業(yè)策劃、余杭品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供余杭建站搭建服務,24小時服務熱線:18980820575,官方網址:www.rwnh.cn
1、能夠嵌入動態(tài)文本于HTML頁面。2、對瀏覽器事件做出響應。3、讀寫HTML元素。4、在數(shù)據(jù)被提交到服務器之前驗證數(shù)據(jù)。5、檢測訪客的瀏覽器信息。6、控制cookies,包括創(chuàng)建和修改等。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如何實現(xiàn)瀑布流布局”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!
網站欄目:JS如何實現(xiàn)瀑布流布局
當前網址:http://www.rwnh.cn/article22/jscsjc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供品牌網站建設、商城網站、小程序開發(fā)、網站策劃、網頁設計公司、品牌網站制作
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)