用JS怎样写瀑布流布局,具体过程是什么
Admin 2022-10-20 群英技术资讯 800 次浏览
关于“用JS怎样写瀑布流布局,具体过程是什么”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。瀑布流 又称瀑布流式布局,是比较流行的一种网站页面布局方式。即多行等宽元素排列,后面的元素依次添加到其后,等宽不等高,根据图片原比例缩放直至宽度达到我们的要求,依次按照规则放入指定位置。
先看效果:

不完整html代码:
?< div id = "container" > < div class = "box" > < div class = "box-img" > < img src = "./img/1.jpg" alt = "" > </ div > </ div > < div class = "box" > < div class = "box-img" > < img src = "./img/2.jpg" alt = "" > </ div > </ div > < div class = "box" > < div class = "box-img" > < img src = "./img/3.jpg" alt = "" > </ div > </ div > </ div > ...... <!-- 省略了图片,多少张图片自行决定--> |
完整的css代码
?*{ padding : 0 ; margin : 0 ; } #container{ position : relative ; } .box{ float : left ; padding : 15px ; } .box-img { width : 150px ; padding : 5px ; border : 1px solid #ccc ; box-shadow: 0 0 5px #ccc ; border-radius: 5px ; } .box-img img{ width : 100% ; height : auto ; } |
简单地来说,如果要实现瀑布流布局,得完成这几件事
function getChildElemnt() { const contentArr = [] //定义数组准备装图 const parent = document.getElementById(container) //得到整个页面 const allContent = parent.getElementsByTagName( '*' ) //得到整个标签 console.log(allContent); for ( var i = 0; i < allContent.length; i++) { if (allContent[i].className == 'box' ) { contentArr.push(allContent[i]) //将class='box'的标签装入数组 } } console.log(contentArr); return contentArr //返回数组 } |
var ccontent = getChildElemnt() var imgWidth = ccontent[0].offsetWidth //令所有图片宽度等于第一张图片 |
var dWidth=document.documentElement.clientWidth //页面宽度 var num = Math.floor(dWidth/ imgWidth) //Math.floor()向下取整 |
因为在瀑布流布局中,当第一行图片已经摆满后,第二行的第一张图片要放在第一行中高度最小的图片的下面
?var BoxHeightArr = [] //定义一个数组,把每张图片的高度依次放进去 for ( var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight //将图片的高度存入数组 } else { //当第一行已经存放不了图片后 var minHeight = Math.min.apply( null , BoxHeightArr) //比较出上一行最小的高度 } } |
//定义一个getMinHeightLocation函数,给它传入BoxHeightArr上一行全部图片,和minHeight上一行图片的最小高度 function getMinHeightLocation(BoxHeightArr, minHeight) { for ( var i in BoxHeightArr) { if (BoxHeightArr[i] === minHeight) { //当图片高度等于最小高度时,该图片的位置为最小高度图片的位置 return i } } } |
for ( var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight } else { var minHeight = Math.min.apply( null , BoxHeightArr) var minIndex = getMinHeightLocation(BoxHeightArr, minHeight) ccontent[i].style.position = 'absolute' //将要插入的图片绝对定位,即元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定 ccontent[i].style.top = minHeight + 'px' //令插入的图片到顶端的距离刚好等于要插其下面图片的高度 ccontent[i].style.left = ccontent[minIndex].offsetLeft + 'px' //令插入的图片到最左边的距离刚好等于要插其下面图片到最左边的距离 BoxHeightArr[minIndex] = BoxHeightArr[minIndex] + ccontent[i].offsetHeight //插入图片后,得将这位置的高度设为两张图片的高度和 } } |
优化代码,提高性能
?window.onload = function () { imgLocation( 'container' , 'box' ) //构造函数imgLocation } //用window.onload = function() {}函数就不用等着body页面中调用就可以执行了 // 获取到当前有多少张图片要摆放 function imgLocation(parent, content) { //令parent='container',content='box' // 将parent下所有的内容全部取出 var cparent = document.getElementById(parent) var ccontent = getChildElemnt(cparent, content) var imgWidth = ccontent[0].offsetWidth var num = Math.floor(document.documentElement.clientWidth / imgWidth) cparent.style.cssText = `width: ${imgWidth * num} px` var BoxHeightArr = [] for ( var i = 0; i < ccontent.length; i++) { if (i < num) { BoxHeightArr[i] = ccontent[i].offsetHeight } else { var minHeight = Math.min.apply( null , BoxHeightArr) var minIndex = getMinHeightLocation(BoxHeightArr, minHeight) ccontent[i].style.position = 'absolute' ccontent[i].style.top = minHeight + 'px' ccontent[i].style.left = ccontent[minIndex].offsetLeft + 'px' BoxHeightArr[minIndex] = BoxHeightArr[minIndex] + ccontent[i].offsetHeight } } // console.log(BoxHeightArr); } function getChildElemnt(parent, content) {parent= 'container' ,content= 'box' const contentArr = [] const allContent = parent.getElementsByTagName( '*' ) console.log(allContent); for ( var i = 0; i < allContent.length; i++) { if (allContent[i].className == content) { contentArr.push(allContent[i]) } } console.log(contentArr); return contentArr } function getMinHeightLocation(BoxHeightArr, minHeight) { for ( var i in BoxHeightArr) { if (BoxHeightArr[i] === minHeight) { return i } } } |
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
我们知道JavaScript设计模式有很多,本文主要给大家介绍JavaScript观察者模式的内容。那么究竟JavaScript观察者模式是什么呢?如何实现?接下来我们详细的了解看看。
jq去掉html标签的方法:1、使用remove(),语法“$("选择器").remove()”;2、使用empty(),语法“$("选择器").empty()”;3、使用detach(),语法“$("选择器").detach()”。
这篇文章主要为大家详细介绍了uniapp微信小程序实现一个页面多个倒计时,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
目录1. 自定义组件官方文档1.1 创建自定义组件1.1.1 声明组件1.1.2 编辑组件1.2 使用自定义组件1.3 页面向自定义组件传递数据(父传子)1.4 组件将事件传给页面(子传父)1. 自定义组件小程序允许我们使用自定义组件的方式来构建页面。官方文档自定义组件是不是用的微信的组件感觉很爽啊,如果不够用怎么办?
vue怎样做点击展开收起的功能?在很多网站上都能看到点击展开收起的功能,点击展开收起的效果能让网页设计更简洁好看,也是比较使用的,因此这篇文章就给大家分享一下vue实现点击展开收起效果的方法。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008