js+css实现瀑布流

瀑布流是一种很常见的布局,实现的方法也有很多,以下是仿照花瓣网的瀑布流展示,希望能帮到各位小主们。



<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Documenttitle>
	<style>
		html,body{
      
			background: #f5f5f5;
		}
		.wrapper {
      
			margin: 30px auto;
			position: relative;
		}

		.CascadeFlowItem {
      
			width: 236px;
			font-size: 12px;
			background-color: #fff;
			box-shadow: 0 1px 3px rgba(0, 0, 0, .02), 0 4px 8px rgba(0, 0, 0, .02);
			border-radius: 3px;
			overflow: hidden;
			-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, .02), 0 4px 8px rgba(0, 0, 0, .02);
			-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, .02), 0 4px 8px rgba(0, 0, 0, .02);
			position: absolute;
		}

		.img {
      
			width: 100%;
			height: auto;
		}

		.description {
      
			display: block;
			padding: 0 16px;
			margin: 10px 0;
			line-height: 1.35em;
			overflow: hidden;
			word-wrap: break-word;
		}

		.bottom_line {
      
			color: #999;
			border-top: 1px solid #F2F2F2;
			background: #FAFAFA;
			position: relative;
			height: 50px;
			display: flex;
			align-items: center;
			padding: 0 15px;
			box-sizing: border-box;
			background-color: #fff;

		}

		.bottom_line>a {
      
			color: #9E7E6B;
		}

		.author {
      
			max-width: 75px;
		}

		.sub_board {
      
			max-width: 75px;
		}

		.ellipsis {
      
			overflow: hidden;
			text-overflow: ellipsis;
			display: inline-block;
			white-space: nowrap;
			-webkit-line-clamp: 1;
		}

		.label {
      
			color: #999;
		}
	style>
head>

<body>
	<div class="container">
		<div class="wrapper">div>
	div>
	<script>
		let list = [
			{
      
				img: './images/1.png',
				description: "EE, M4 Miv4t",
				author: "懶懶懶",
				sub_board: "插画"
			},
			...];
		let wrapper = document.getElementsByClassName('wrapper')[0];
		let wrapper_html = ``;
		for (var i = 0; i < list.length; i++) {
      
			wrapper_html = wrapper_html +
				`
${ list[i].img}">

${ list[i].description}

`
} wrapper.innerHTML = wrapper_html; function waterFall() { let bodyWidth = getSize().width - 300; let list = document.getElementsByClassName('CascadeFlowItem'); let itemWidth = 236;//每个盒子的宽是固定的 let wrapperTop = 30; let itemMarginRight = 20;//盒子与盒子之间的距离 let itemMarginTop = 20;//盒子与盒子之间的距离 let heightArr = [];//所有高度 let rowHeightArr = [];//每一行的高度 let col = Math.floor(bodyWidth / (itemWidth + itemMarginRight));//列数 //console.log(list) for (var i = 0; i < list.length; i++) { heightArr.push(window.getComputedStyle(list[i]).height.split('px')[0] * 1); } for (var i = 0; i < list.length; i++) { if (i < col) { //确定第一行 list[i].style.top = wrapperTop + 'px'; list[i].style.left = itemWidth * i + itemMarginRight * i + 'px'; rowHeightArr.push(heightArr[i]); } else { let minHeight = rowHeightArr[0]; var index = 0; for (var j = 0; j < rowHeightArr.length; j++) { if (minHeight > rowHeightArr[j]) { minHeight = rowHeightArr[j]; index = j; } } //设置其他行 list[i].style.top = wrapperTop + minHeight + itemMarginTop + 'px'; list[i].style.left = itemWidth * index + itemMarginRight * index + 'px'; //更新最小的高 rowHeightArr[index] = rowHeightArr[index] + heightArr[i] + itemMarginTop; //console.log(rowHeightArr) } } //console.log(heightArr) //设置wrapper的宽和高 wrapper.style.cssText=`width: ${ (col * itemWidth + itemMarginRight * (col - 1) + 'px')}; height: ${ (rowHeightArr.slice(0).sort(function (a, b) { return b - a })[0] + 80 + 'px')}`; } function getSize() { return { width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight } } window.onload=function(){ waterFall(); } window.onresize=function(){ waterFall(); }
script> body> html>

效果图:

你可能感兴趣的:(html,css,js)