官网瀑布流实现分享thinkphp

来源:互联网 发布:日本买房知乎 编辑:程序博客网 时间:2024/06/06 03:02
很多人都想做瀑布流的效果,这里告诉大家官网使用的方法。
首先要下载瀑布流的插件jquery.masonry.min.js 地址:http://masonry.desandro.com/index.html里面包含的很多示例,但是都是英文的。。。我给大家写个小例子吧

流程:

1,页面初始化时,调用插件进行一次排版;
2,当用户将滚动条拖到底部时,用ajax加载一次数据,并排版显示
3,重复2,直到无数据

html代码
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><!--样式--><style type="text/css">body {margin:40px auto; width:800px; font-size:12px; color:#666;}.item{    border: 1px solid #D4D4D4;    color: red;    margin: 0 10px 10px 0;    padding: 10px;    position: relative;    width: 200px;}.loading-wrap{    bottom: 50px;    width: 100%;    height: 52px;    text-align: center;    display: none;}.loading {    padding: 10px 10px 10px 52px;    height: 32px;    line-height: 28px;    color: #FFF;    font-size: 20px;    border-radius: 5px;    background: 10px center rgba(0,0,0,.7);}.footer{    border: 2px solid #D4D4D4;}</style><!--样式--></head><body><!--引入所需要的jquery和插件--><script type="text/javascript" src="/test/public/Js/jquery-1.7.2.min.js"></script><script type="text/javascript" src="/test/public/Js/jquery.masonry.min.js"></script><!--引入所需要的jquery和插件--><!--瀑布流--><div id="container" class=" container"><!--这里通过设置每个div不同的高度,来凸显布局的效果--><volist name="height" id="vo">    <div class="item" style="height:{$vo}px;">瀑布流下来了</div></volist></div><!--瀑布流--><!--加载中--><div id="loading" class="loading-wrap">    <span class="loading">加载中,请稍后...</span></div><!--加载中--><!--尾部--><div class="footer"><center>我是页脚</center></div><!--尾部--><script type="text/javascript">$(function(){    //页面初始化时执行瀑布流    var $container = $('#container');     $container.masonry({        itemSelector : '.item',        isAnimated: true     });     //用户拖动滚动条,达到底部时ajax加载一次数据    var loading = $("#loading").data("on", false);//通过给loading这个div增加属性on,来判断执行一次ajax请求    $(window).scroll(function(){        if(loading.data("on")) return;        if($(document).scrollTop() > $(document).height()-$(window).height()-$('.footer').height()){//页面拖到底部了            //加载更多数据            loading.data("on", true).fadeIn();         //在这里将on设为true来阻止继续的ajax请求            $.get(                "getMore",                 function(data){                   //获取到了数据data,后面用JS将数据新增到页面上                    var html = "";                    if($.isArray(data)){                        for(i in data){                            html += "<div class=\"item\" style=\"height:"+data[i]+"px;\">瀑布又流下来了</div>";                        }                        var $newElems = $(html).css({ opacity: 0 }).appendTo($container);                        $newElems.imagesLoaded(function(){                            $newElems.animate({ opacity: 1 });                            $container.masonry( 'appended', $newElems, true );                               });                      //一次请求完成,将on设为false,可以进行下一次的请求                        loading.data("on", false);                    }                    loading.fadeOut();                },                "json"            );        }    });});</script></body></html>

Action代码

class UserAction extends Action{//初始化的数据        public function index(){        for ($i=0;$i<10;$i++){            $res[$i] = rand(100, 400);        }        $this->assign('height', $res);        $this->display();            }//获取一次请求的数据        public function getMore(){        for ($i=0;$i<6;$i++){            $res[$i] = rand(100, 400);        }        $this->ajaxReturn($res);    }}

注意:

通过判断窗口是否滚动到页面底部来决定用ajax加载一次数据。如果不做处理,会一下子请求很多次。所以,要使用条件来限制。
我使用的是往一个元素上赋值 $("#loading").data("on", true);,在请求期间判断是true则不继续请求,然后在页面请求完成后再赋值为false

放几张示例图片:


源码下载:http://www.thinkphp.cn/extend/293.html
原创粉丝点击