简单js瀑布流写法

勇旭分享的简单js瀑布流写法
视图层写法html
<style type="text/css">
    .pictureflow{width:700px;}
    .pictureflow .imglist{width:210px;padding:2px 5px; margin:5px; height:100%; border:1px solid #ccc; overflow:hidden;float:left; }
    .pictureflow .imglist .imgo{margin:0 auto;padding-top:5px;}
    .pictureflow .imglist .imgo img{width:200px;border:1px solid #ccc; overflow:hidden;}
</style>

    <div class="pictureflow"></div>

</div>
<script type="text/javascript">
    $(document).ready(function(){
        $(".pictureflow").pictureflow();
    });
    $.fn.extend({
        pictureflow:function(options){
            // 用的时候参数别写错了
            var settings = $.extend({
                'col': 3,
                'width': '500',
                'url':''
            }, options);
            var indexpage = 1;	// 备用(无用)
            var pcount = 10;	// 备用(无用)
            var obj = $(this);
            // 生成div个数
            for(var i = 0; i < settings.col; i++){
                $(obj).append("<div class='imglist'></div>");
            }
            function win_bottom(){
                if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
                    $(window).unbind("scroll");	// 解除绑定  加载完图片继续绑定(绑定在下边)
                    addimg();
                }
            }
            $(window).bind("scroll", win_bottom);
            var ajaxNum = 1;
            function addimg(){
                var jpg = 0;
                var html = '';
                var imgo = '';
                var imgo_h = 0;
                for(var i = 0; i < pcount; i++){
                    if(ajaxNum == 1){
                        jpgindex = Math.ceil(Math.random()*100) % 6;
                    }else{
                        $.ajax({
                            type : 'POST',
                            url  : '<?= Yii::app()->createUrl('ams/xu/getAjaxPic');?>',
                            data : {jpgindex : ajaxNum},
                            success : function(msg){
                                jpgindex = msg;
                            }
                        })
                    }

                    html = "<div class='imgo'>";
                    html += "<img src='ams/upload/dealerImage/" + jpgindex + ".jpg'";
                    html += '</div>';

                    // 找到高度最小的一个
                    imgo = $(obj).find(".imglist").eq(0);
                    imgo_h = imgo.height();
                    $(obj).find(".imglist").each(function(){
                        if(imgo_h > $(this).height()){
                            imgo = $(this);
                            imgo_h = imgo.height();
                        }
                    });
                    $(imgo).append(html);
                }
                if(ajaxNum == 7){
                    ajaxNum = 1;
                }else{
                    ajaxNum++;
                }

                // 重新绑定事件
                $(window).bind("scroll", win_bottom);
            }
            addimg();
        }
    });
</script>
控制器层,滚动条通过ajax加载数据,图片的相关信息
    public function actionGetAjaxPic(){
        echo $_POST['jpgindex']+1;
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值