原生js實現(xiàn)瀑布流效果
本文實例為大家分享了js實現(xiàn)瀑布流效果的具體代碼,供大家參考,具體內容如下
CSS樣式:
<style> .cont{margin: 0 auto;position: relative;} .box{float: left;padding: 5px;} .imgbox{border: black solid 1px;padding: 5px;border-radius: 5px;} .imgbox img{width: 200px;display: block;}</style>
HTML結構:
<div class='cont'> <div class='box'> <div class='imgbox'> <img src='http://www.leifengta.com.cn/bcjs/img/1.jpg' > </div> </div> //......此處省略雷同代碼 <div class='box'> <div class='imgbox'> <img src='http://www.leifengta.com.cn/bcjs/img/2.jpg' > </div> </div> </div>
JavaScript代碼:
<script> onload = function(){ var wf = new WaterF(); wf.init(); } class WaterF{ constructor(){ this.clientW = document.documentElement.clientWidth; this.abox = document.querySelectorAll('.box'); this.cont = document.querySelector('.cont'); } init(){ this.maxNum = parseInt(this.clientW / this.abox[0].offsetWidth); this.cont.style.width = this.maxNum * this.abox[0].offsetWidth + 'px'; this.firstLine(); this.otherLine(); } firstLine(){ this.heightArr = []; for(var i=0;i<this.maxNum;i++){ this.heightArr.push(this.abox[i].offsetHeight); } } otherLine(){ for(var i=this.maxNum;i<this.abox.length;i++){ var min = Math.min(...this.heightArr); var minIndex = this.heightArr.indexOf(min); this.abox[i].style.position = 'absolute'; this.abox[i].style.top = min + 'px'; this.abox[i].style.left = minIndex * this.abox[0].offsetWidth + 'px'; this.heightArr[minIndex] = this.heightArr[minIndex] + this.abox[i].offsetHeight; } } }</script>
小編還為大家準備了精彩的專題:瀑布流布局匯總
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. 詳解Android studio 動態(tài)fragment的用法2. 解決Android studio xml界面無法預覽問題3. 圖文詳解vue中proto文件的函數(shù)調用4. Spring Boot和Thymeleaf整合結合JPA實現(xiàn)分頁效果(實例代碼)5. php模擬實現(xiàn)斗地主發(fā)牌6. 什么是python的自省7. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現(xiàn)8. vue 使用localstorage實現(xiàn)面包屑的操作9. .Net Core使用Coravel實現(xiàn)任務調度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備