帮助中心
获取牛币
联系我们
关于我们
版权声明
素材牛首页
微博登录
QQ登录
微信扫码登录
注册
登录
新浪微博
QQ登陆
微信登录
热门搜索:
小程序
支付
Java
后台模板
上传
商城模板
jQuery
手机
Thinkphp
微信
Vue.js
首 页
网页特效
整站源码
PHP实例
网站模版
工具箱
常用代码
论坛
游戏源码
资源分享
当前位置:
首页
»
精选常用代码
» 很少见的JS水平布局瀑布流效果
很少见的JS水平布局瀑布流效果
浏览:1928次
-
评论:4次
-
发布时间:2023-02-23
Html
Css
Js
* { margin:0; padding:0; box-sizing:border-box; } .content { overflow:hidden; width:1920px; margin:0 auto; border:1px solid red; } .long { width:3000px; } .box { float:left; /* height:300px; */ /* line-height:300px; */ text-align:center; background-color:pink; /* margin-bottom:10px; */ /* margin-right:10px; */ } /* 清除浮动 */ .clearfix:after { visibility:hidden; clear:both; display:block; content:"."; height:0 } .clearfix { *zoom:1 }
images = [{ height: 300, width: 4000 }, { height: 300, width: 400 }, { height: 300, width: 400 }, { height: 100, width: 500 }, { height: 200, width: 500 }, { height: 200, width: 100 }, { height: 400, width: 300 }, { height: 300, width: 400 }, { height: 500, width: 400 }, { height: 200, width: 400 }, { height: 100, width: 400 }, { height: 300, width: 400 }, { height: 200, width: 500 }, { height: 300, width: 1900 }, { height: 100, width: 500 }, { height: 200, width: 100 }, { height: 400, width: 300 }, { height: 300, width: 400 }, { height: 500, width: 400 }, { height: 300, width: 400 }, { height: 200, width: 400 }, { height: 300, width: 400 }, { height: 100, width: 500 }, { height: 200, width: 500 }, { height: 100, width: 400 }, { height: 200, width: 100 }, { height: 400, width: 300 }, { height: 300, width: 400 }, { height: 500, width: 400 }, { height: 200, width: 400 }, { height: 100, width: 400 }, { height: 300, width: 400 }, ] function filledImages(images, lineHeight, lineWidth, margin) { let i = index = 0 let splitImg = [] while (i < images.length && ++index) { let totalWidth = 0; let rotalRowWidth = 0 let minPay = Number.MAX_VALUE let picNum = 0 let rowImg = [] for (let j = i; j < images.length; j++, picNum++) { const img = images[j]; let newWidth = lineHeight / img.height * img.width // console.log(newWidth); totalWidth += newWidth // 计算损失宽度 let pay = totalWidth - (lineWidth - margin * picNum) // console.log(totalWidth, pay); // 添加这张图片之后 pay 的绝对值比 minPay 的绝对值大 if (Math.abs(pay) > Math.abs(minPay)) break // 更新最小损失宽度 minPay = pay rotalRowWidth += newWidth rowImg.push({ ...img, height: lineHeight, width: newWidth }) } i += picNum console.log(`第${index}行,宽度一共增加${-minPay}px,一行放${picNum}张`); for (let k = 0; k < rowImg.length; k++) { // 根据比例修改 newWidth let ratioPag = -minPay * rowImg[k].width / rotalRowWidth rowImg[k].width = rowImg[k].width + ratioPag } splitImg.push(rowImg) } return splitImg } function createBox(img, margin) { let div = document.createElement('div') div.classList.add('box') div.style.width = img.width + 'px' div.style.height = img.height + 'px' div.style.lineHeight = img.height + 'px' div.style.marginRight = margin + 'px' div.style.marginBottom = margin + 'px' div.innerText = `height:${img.height} width:${img.width}` return div } const lineHeight = 350 const lineWidth = 1200 const margin = 6 let imgs = filledImages(images, lineHeight, lineWidth, margin) console.log(imgs); let content = document.querySelector('.content') content.style.width = lineWidth + 'px' for (let i = 0; i < imgs.length; i++) { let img = imgs[i] let row = document.createElement('div') row.classList.add('clearfix') row.classList.add('long') for (let j = 0; j < img.length; j++) { let box = createBox(img[j], margin) row.appendChild(box) } content.appendChild(row) }
↑上面代码改变,会自动显示代码结果
jQuery调用版本:
1.11.3
<!doctype html><html><head><meta charset="utf-8"><title>很少见的JS水平布局瀑布流效果-www.sucainiu.com</title><script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script><style>
</style></head><body>
<script>
</script>
</body></html>
立即下载
收藏
积分说明:注册即送10牛币,每日签到可获得5牛币,成为VIP会员可永久免牛币下载!
充值积分
充值会员
更多说明»
代码描述:使用原生js实现的水平布局瀑布流,效果类似500px官网,用户可以手动配置每行高度和总宽度
纯CSS实现的多颜色斜线边框效果
JS按钮控制数字动态加减效果
讨论这个常用代码(4)
回答他人问题或分享心得会奖励牛币
Lv1 新人
wx***47
2023年03月07日
666666
回复
Lv5 码农
qq***10
2023年03月28日
666666
回复
Lv1 新人
wb***66
2023年03月31日
66666
回复
Lv4 码徒
ha***ok
2023年07月10日
第一次见
回复
文明上网,理性发言! 😉 阿里云幸运券,
戳我领取
发表评论
提交回复
我的积分余额:
0
已下载次数:
1928
所需牛币:
5
开始下载
牛币获取:
签到、评论、充值
» 在线充值
(10牛币=1元)
成为VIP可永久免积分下载全部常用代码
» 查看详情
VIP
客服
签到
充值