純css 實現瀑布流效果

原理是 css 的 column 屬性,話不多說直接上代碼

<html>
	<head>
		<link rel="stylesheet" type="text/css" href="css/main.css">
		<script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>

		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
			}

			#main {
				-webkit-column-count: 2;
				-moz-column-count: 2;
				column-count: 2;
				-moz-column-gap: 20px;
				-webkit-column-gap: 20px;
				column-gap: 20px;
				padding: 10px;
			}

			.box {
				padding: 15px;
				border: solid 2px #eeeeee;
				border-radius: 4px;
				margin-bottom: 15px;
				cursor: pointer;
			}

			.box img {
				width: 100%;
			}
		</style>
	</head>
	<body>
		<div id="main">
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<!-- 這裏省略多個class爲box的div-->
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
			<div class="box">
				<div class="pic">
					<img src="">
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		var width = 300,
			height = 300;
		$('.box img').each(function() {
			// 隨機圖片的高寬,如果大小一樣,就沒必要用瀑布流了
			width = Math.floor(Math.random() * 100) + 300;
			height = Math.floor(Math.random() * 500) + 300;
			$(this).attr('src', 'http://placekitten.com/' + height + '/' + width);
		});
	</script>
</html>

參照:https://www.cnblogs.com/CraryPrimitiveMan/p/3777033.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章