时间:2021-05-26
本文实例为大家分享了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; }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;}课程地址。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了js实现瀑布流布局的具体代码,供大家参考,具体内容如下1.实现瀑布流布局思路准备好数据之后.绑定滚动事件.判断页面是否到底(滚动的距离+可是
本文用纯js代码手写一个瀑布流网页效果,初步实现一个基本的瀑布流布局,以及滚动到底部后模拟ajax数据加载新图片功能。缺点:1.程序不是响应式,不能实时调整页面
原生JS实现的瀑布流布局,代码及demo代码地址:https://github.com/leozdgao/responsive_waterfallDemo:ht
本文实例为大家分享了js实现瀑布流布局的具体代码,供大家参考,具体内容如下原理:1、瀑布流布局,要求进行布局的元素等宽,然后计算元素的宽与浏览器的宽度之比,得到
下面奉上一则用JS实现瀑布流的方法,望批评。复制代码代码如下:瀑布流布局测试body{background-color:#eee;font-size:84%;t