谁帮我看看,这两种代码有什么区别,第一个正常运行第二个却不行!!我测试了好长时间都找不到问题所在。

来源:3-1 jQuery实现瀑布流布局图片定位

低熵体

2016-09-23 17:36

$( window ).on( "load", function(){
    waterfall();  
});

function waterfall(){
    var $aPin = $( "div.pin" );
    var pinHArr=[];//用于存储 每列中的所有块框相加的高度。
    $aPin.each( function( index, value ){
        var pinH = $aPin.eq( index ).height();
        if( index < 6 ){
            pinHArr[ index ] = pinH; //第一行中的num个块框pin 先添加进数组pinHArr
        }else{
            var minH = Math.min.apply( null, pinHArr );//数组pinHArr中的最小值minH
            var minHIndex = $.inArray( minH, pinHArr );
            $( value ).css({
                'position': 'absolute',
                'top': minH + 15,
                'left': $aPin.eq( minHIndex ).position().left
            });
            pinHArr[ minHIndex ] += $aPin.eq( index ).height() + 15;//更新添加了块框后的列高
        }
    });
}
$(window).on("load",function(){
	waterfall();
});

function waterfall(){
	var $aPin=$("div.pin");
	var pinHArr=[];
	$aPin.each(function(index,value){
		var pinH=$aPin.eq(index).height();
		if(index<6){
			pinHArr[index]=pinH;
		}else{
			var minH=Math.min.apply(null,pinHArr);
			var minHIndex=$.inArray(minH,pinHArr);
			$(value).css({
				'position': 'absolute',
				'top': minH+15,
				'left': $aPin.eq(minHIndex).position().left
			});			
			pinH[minHIndex]+=$aPin.eq(index).height()+15;
		}
	});
}


写回答 关注

1回答

  • 低熵体
    2016-09-23 17:42:15

    自己找到了,20行数组名写错......

瀑布流布局

瀑布流布局是网站比较流行的一种布局方式,教你实现三大方式

97758 学习 · 736 问题

查看课程

相似问题