masonry结合json 制作无限滚动的瀑布流

时间:2021-11-14 22:25:18

做前端这行的 能直接贴代码就直接贴代码了,不用多说什么别的

效果需要引入jquery和jquery.masonry.min.js这两个JS

JS代码如下:

$(document).ready(function(){
var $container = $('.space_main_con1 .con');
var animated;
var i=1;
//判断是否需要 masonry.js这个里面提供动画 如果是IE浏览器并且版本低于9.0 则添加动画 否则就用 css3的动画(css3的动画代码在下面有贴出)
$.browser.msie && $.browser.version < 9.0 ? animated = true : animated = false;
$container.imagesLoaded(function(){
$container.masonry({
itemSelector: '.space_item',
columnWidth:288,
gutterWidth:16,//每两列之间的间隙为5像素
isAnimated: animated,
animationOptions: {
duration: 400,
easing: 'linear',
queue: false
}
});
}); $(".space_item").live("mouseover",function(){
$(this).addClass("on");
})
$(".space_item").live("mouseout",function(){
$(this).removeClass("on");
});
function getMore(){
$("#infscr-loading").show();
var json = "json.php";
$.getJSON(json, function(data){
$.each(data,function(i){
var url=data[i].url;
var img=data[i].img;
var user=data[i].user;
var space=data[i].space;
var time=data[i].time;
var content=data[i].content;
var simg=data[i].simg;
var pname=data[i].pname;
var pprice=data[i].pprice;
html='<div class="space_item"><a class="p_img" href="'+url+' target="_blank"><img src="'+img+'" width="260" height="260" /></a><p class="author">达人:<a href="'+space+'" target="_blank">'+user+'</a></p><p class="time">发布时间:'+time+'</p><div class="arrow"></div><div class="content">'+content+'</div><div class="cfix pro_box"><a class="p_simg" target="_blank" href="'+url+'"><img src="'+simg+'" width="50" height="50" /></a><div class="p_info"><p class="p_name"><a target="_blank" href="'+url+'">'+pname+'</a></p><p class="p_price">¥'+pprice+'</p></div></div></div>';
$boxes = $(html).css({ opacity: 0 });
$boxes.animate({opacity:1});
$container.append($boxes).masonry('appended',$boxes,true);
});
i=1;
$("#infscr-loading").hide();
});
};
//滚动到底部的时候
$(window).bind("scroll",function(){
if( $(document).scrollTop() + $(window).height() + $(".footer").outerHeight()-100 > $(document).height() && i==1) {
i=0;
getMore();
}
});
});

另外有一段比较好看的css3 动画效果配合[这段代码不知道是哪位大神写的 =_=感谢大神写的这么好的效果动画]

/**** 转变 ****/
.transitions-enabled .masonry,.transitions-enabled.masonry .masonry-brick{-webkit-transition-duration:0.7s;-moz-transition-duration:0.7s;-o-transition-duration:0.7s;transition-duration:0.7s;}
.transitions-enabled .masonry{-webkit-transition-property:height, width;-moz-transition-property:height, width;-o-transition-property:height, width;transition-property:height, width;}
.transitions-enabled .masonry .masonry-brick{-webkit-transition-property:left, right, top;-moz-transition-property:left, right, top;-o-transition-property:left, right, top;transition-property:left, right, top;}
/* 禁用容器上的转变 */
.transitions-enabled .infinite-scroll.masonry{-webkit-transition-property:none;-moz-transition-property:none;-o-transition-property:none;transition-property:none;}
/* 无限滚动装载机 */
#infscr-loading{text-align:center;padding:10px;background:#000;opacity:0.8;filter:alpha(opacity=80);color:#FFF;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;}
#infscr-loading{position:fixed;bottom:0px;left:45%;z-index:9999;width:200px;}
*html,*html body /* 修正IE6振动bug */{background-image:url(about:blank);background-attachment:fixed;}
*html #infscr-loading{position:absolute;bottom:expression(eval(document.documentElement.scrollTop));}
#page-nav{display:none;}

对应的HTML代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>masonry结合json 制作无限滚动的瀑布流</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.masonry.min.js"></script>
</head>
<body>
<div class="space_main space_main_con1">
<div class="con transitions-enabled infinite-scroll">
<div class="space_item">
<a class="p_img" href="#" target="_blank"><img src="tmp/xinpin/n1.jpg" width="260" height="260" /></a>
<p class="author">达人:<a href="#" target="_blank">grapecxh</a></p>
<p class="time">发布时间:2014-12-24</p>
<div class="arrow"></div>
<div class="content">
评论内容评论内容评论内容评论内容评论内容评论内容评论内容评论内容评论内容评论内容评论内容
</div>
<div class="cfix pro_box">
<a class="p_simg" target="_blank" href="#"><img src="tmp/xinpin/n1.jpg" width="50" height="50" /></a>
<div class="p_info">
<p class="p_name"><a target="_blank" href="#">商品名称商品名称商品名</a></p>
<p class="p_price">¥68</p>
</div>
</div>
</div>
<!--还有更多相同如上省略了-->
</div>
</div> </body>
</html>

相关的另外一位大神写了一篇文章,这篇就是根据这位大神写的修改而来的  大神写的代码链接:http://my.oschina.net/mmeng/blog/73525