简单的瀑布流效果

折腾几天,终于做了个简单的无插件的瀑布流效果

直接上代码,可以实际使用。有需要的拿去参考。手机竖屏显示2列,横屏显示3列。电脑根据浏览器大小显示2-4列,具体列数可根据浏览器大小调整。

图片是效果截图。图片资料自己准备

<!DOCTYPE html>
<meta charset="utf-8" />
<body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<style>
	.parent1 { 
		width:100%;
		-moz-column-count: 2;
        -webkit-column-count: 2;
        column-count: 2;
	 }
	 .child1 {
		margin-bottom:20px;	 
		-moz-page-break-inside: avoid;
		-webkit-column-break-inside: avoid;
		break-inside: avoid;
		color:#0f0;		}
</style>
<script>
var x=0;
var heng=heng_flag();
var mobile_flag = isMobile();
$(document).ready(function(){

var x=document.documentElement.clientWidth||document.body.clientWidth;
heng=heng_flag();    
x=$(".aacc").width();
buju(x);

 
 
 //监听横竖屏
    var evt = "onorientationchange" in window ? "orientationchange" : "resize";
	
	
    window.addEventListener(evt,resize,false);
    function resize(fals) {
    heng=heng_flag();	   
    }
    resize(true);

 
 
 
    $(window).resize(function(){
    x=document.documentElement.clientWidth||document.body.clientWidth;
    x=$(".aacc").width();
    buju(x);
	
  }); 
  
  
  
});



    




function buju(w){
	var c=0;
	var w_c=0;
	if(heng){
	//alert("heng1 "+heng);
		if(w>1400){
		c=4;
		w_c=4.15;
		}else if(w>800){
		c=3;
		w_c=3.15;
		}else{
		c=2;
		w_c=2.1;
		}
		if(mobile_flag){
			c=4;
			w_c=4.15;
		}
	}else{
		c=2;
		w_c=2.1;
	}
	$(".parent1").css("column-count",c);
	$(".parent1").css("-moz-column-count",c);
	$(".parent1").css("-webkit-column-count",c);
	$(".child1 img").width(w/w_c);
}

function heng_flag() {
       if(window.orientation == 0 || window.orientation == 180) {
            return false;
       }else {
			return true;
       }
}

function isMobile() {
    var userAgentInfo = navigator.userAgent;

    var mobileAgents = [ "Android", "iPhone", "SymbianOS", "Windows Phone", "iPad","iPod"];

    var mobile_flag = false;

    //根据userAgent判断是否是手机
    for (var v = 0; v < mobileAgents.length; v++) {
        if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
            mobile_flag = true;
            break;
        }
    }

     var screen_width = window.screen.width;
     var screen_height = window.screen.height;    

     //根据屏幕分辨率判断是否是手机
     if(screen_width < 500 && screen_height < 800){
         mobile_flag = true;
     }

     return mobile_flag;
}



</script>

<div id id="aacc" class="aacc" style="width:95%;">
	<div class="parent1">
		<div class="child1"><img src="images/9.jpg">5</div>
		<div class="child1"><img src="images/1.jpg">1</div>
		<div class="child1"><img src="images/2.jpg">2</div>
		<div class="child1"><img src="images/3.jpg">3</div>
		<div class="child1"><img src="images/5.jpg">6</div>
		<div class="child1"><img src="images/4.jpg">3</div>
		<div class="child1"><img src="images/6.jpg">5</div>

		<div class="child1"><img src="images/7.jpg">6</div>
		<div class="child1"><img src="images/8.jpg">0</div>
		<div class="child1"><img src="images/0.jpg">8</div>
		<div class="child1"><img src="images/1.jpg">9</div>

	</div>
</div>
</body>

 

图片4列效果

 

图片2列效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
jQuery实现瀑布流效果需要先引入jQuery库,然后通过计算每个元素的位置和高度,来实现元素的动态排列。 以下是一个简单的示例代码: HTML部分: ```html <div id="waterfall"> <div class="item">item1</div> <div class="item">item2</div> <div class="item">item3</div> <div class="item">item4</div> <div class="item">item5</div> <div class="item">item6</div> </div> ``` CSS部分: ```css #waterfall { width: 800px; /* 瀑布流容器宽度 */ margin: 0 auto; } .item { width: 380px; /* 每个元素宽度 */ margin: 10px; float: left; background-color: #eee; } ``` jQuery部分: ```javascript $(function() { // 获取瀑布流容器的宽度 var containerWidth = $('#waterfall').width(); // 获取每个元素的宽度和间距 var itemWidth = $('.item').outerWidth(true); // 计算列数 var cols = Math.floor(containerWidth / itemWidth); // 创建一个数组用于存放每一列的高度 var heightArr = []; for (var i = 0; i < cols; i++) { heightArr.push(0); } // 遍历每个元素 $('.item').each(function() { // 获取元素的高度 var itemHeight = $(this).outerHeight(true); // 找到高度最小的那一列 var minHeight = Math.min.apply(null, heightArr); var minIndex = $.inArray(minHeight, heightArr); // 设置元素的位置 $(this).css({ 'left': minIndex * itemWidth, 'top': minHeight }); // 更新该列的高度 heightArr[minIndex] += itemHeight; }); }); ``` 上述代码实现了一个简单瀑布流效果,可以根据实际需求进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值