通过瀑布流加深对js的理解

今天只做了一个瀑布流的小功能
<!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>底部自动填充对齐jquery瀑布流 - 站长素材</title>
<style type="text/css">
*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
body{font:12px/180% Arial, Helvetica, sans-serif, "新宋体";color:#333;}
a{text-decoration:none;color:#333;-webkit-transition:color .2s;-moz-transition:color .2s;-o-transition:color .2s;-ms-transition:color .2s;transition:color .2s;}
a:hover{text-decoration:underline;color:#ff7d8e;}
a:focus{outline:none;}
/* brand-list */
.brand-list{width:1020px;margin:15px auto 0 auto;position:relative;}
.brand-list .item{position:absolute;width:202px;background-color:#E3F3FF;border:1px solid #f8f8f8;padding:18px;top:0;left:40%;-webkit-transition:all 0.3s;-moz-transition:all 0.3s;-o-transition:all 0.3s;-ms-transition:all 0.3s;transition:all 0.3s;}
.brand-list .hover{box-shadow:0px 1px 8px rgba(200,200,200,.6);background-color:#fff;border-color:#c8c8c8;}
.brand-list .additem{padding:0;width:240px;border:none;}
.brand-list .item h3{font-size:42px;color:#666;font-family:arial;font-size:18px;font-weight:800;line-height:.8;margin-bottom:20px;}
.brand-list .item p{margin-bottom:15px;}
</style>
</head>
<body>

<div class="brand-list" id="brand-waterfall">

    <!-- 循环字母模块 item -->
    <div class="item" id="brand-a">
        <h3>A</h3>
        <p><img src="news_07.jpg" width="204" height="131" /></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">ETUDEHOUSE/爱丽小屋</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">abeeco</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">AFU/阿芙</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Egyptian-Magic-Cream/埃及魔法膏</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">adidas/阿迪达斯</a></p>
    </div>

    <div class="item" id="brand-b">
        <h3>B</h3>
        <p><img src="images/news_27.jpg" width="202" height="151" /></p>
    </div>

    <div class="item" id="brand-c">
        <h3>C</h3>
        <p><img src="images/news_35.jpg" width="202" height="141" />&nbsp;<img src="images/news_37.jpg" width="202" height="202" /></p>
    </div>

    <div class="item" id="brand-d">
        <h3>D</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">Dr.Jart+Dr.MJDior/迪奥</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">dodo</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Dior/迪奥</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Dove/多芬</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">DHC/蝶翠诗</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Davidoff/大卫杜夫</a></p>
    </div>

    <div class="item" id="brand-e">
        <h3>E</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">N-Dorphin/恩朵娉</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">OPERA/娥佩兰</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">ELF</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">eos</a></p>
    </div>

    <div class="item" id="brand-f">
        <h3>F</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">芳草集</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">THEFACESHOP/菲诗小铺</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Fanxishop/凡茜</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">FANCL/芳珂(芳凯尔)</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">VERSACE/范思哲</a></p>
    </div>

    <div class="item" id="brand-g">
        <h3>G</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">ARDELL/艾黛尔</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">ANNASUI/安娜苏</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">ETUDEHOUSE/爱丽小屋</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">abeeco</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">AFU/阿芙</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Egyptian-Magic-Cream/埃及魔法膏</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">adidas/阿迪达斯</a></p>
    </div>

    <div class="item" id="brand-h">
        <h3>H</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">LAMER/海蓝之谜</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">BOCOTON/海诺丝丽</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">CO.E/韩伊</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Herbacin/贺本清</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花瑶花</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花漾美姬</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">HEY NATURE/韩彩妮</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Hipitch/黑龙堂</a></p>
    </div>

    <div class="item" id="brand-l">
        <h3>L</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">LAMER/海蓝之谜</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">BOCOTON/海诺丝丽</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">CO.E/韩伊</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">CO.E/韩伊</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">CO.E/韩伊</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Herbacin/贺本清</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花瑶花</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花漾美姬</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">HEY NATURE/韩彩妮</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Hipitch/黑龙堂</a></p>
    </div>

    <div class="item" id="brand-m">
        <h3>M</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">LAMER/海蓝之谜</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">BOCOTON/海诺丝丽</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">CO.E/韩伊</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Herbacin/贺本清</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花瑶花</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花漾美姬</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">HEY NATURE/韩彩妮</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Hipitch/黑龙堂</a></p>
    </div>

    <div class="item" id="brand-n">
        <h3>N</h3>
        <p><a href="http://sc.chinaz.com/" target="_blank">LAMER/海蓝之谜</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">LAMER/海蓝之谜</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">LAMER/海蓝之谜</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">BOCOTON/海诺丝丽</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">CO.E/韩伊</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Herbacin/贺本清</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花瑶花</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">花漾美姬</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">HEY NATURE/韩彩妮</a></p>
        <p><a href="http://sc.chinaz.com/" target="_blank">Hipitch/黑龙堂</a></p>
    </div>

</div>

<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
    $('#brand-waterfall').waterfall();
});

;(function ($) {
    $.fn.waterfall = function(options) {
        var df = {
            item: '.item',
            margin: 15,
            addfooter: true
        };
        options = $.extend(df, options);
        return this.each(function() {
            var $box = $(this), pos = [],
            _box_width = $box.width(),
            $items = $box.find(options.item),
            _owidth = $items.eq(0).outerWidth() + options.margin,
            _oheight = $items.eq(0).outerHeight() + options.margin,
            _num = Math.floor(_box_width/_owidth);

            (function() {
                var i = 0;
                for (; i < _num; i++) {
                    pos.push([i*_owidth,0]);
                } 
            })();

            $items.each(function() {
                var _this = $(this),
                _temp = 0,
                _height = _this.outerHeight() + options.margin;

                _this.hover(function() {
                    _this.addClass('hover');
                },function() {
                    _this.removeClass('hover');
                });

                for (var j = 0; j < _num; j++) {
                    if(pos[j][1] < pos[_temp][1]){
                        //暂存top值最小那列的index
                        _temp = j;
                    }
                }
                this.style.cssText = 'left:'+pos[_temp][0]+'px; top:'+pos[_temp][1]+'px;';
                //插入后,更新下该列的top值
                pos[_temp][1] = pos[_temp][1] + _height;
            });

            // 计算top值最大的赋给外围div
            (function() {
                var i = 0, tops = [];
                for (; i < _num; i++) {
                    tops.push(pos[i][1]);
                }
                tops.sort(function(a,b) {
                    return a-b;
                });
                $box.height(tops[_num-1]);

                //增加尾部填充div
                if(options.addfooter){
                    addfooter(tops[_num-1]);
                }

            })();

            function addfooter(max) {
                var addfooter = document.createElement('div');
                addfooter.className = 'item additem';
                for (var i = 0; i < _num; i++) {
                    if(max != pos[i][1]){
                        var clone = addfooter.cloneNode(),
                        _height = max - pos[i][1] - options.margin;
                        clone.style.cssText = 'left:'+pos[i][0]+'px; top:'+pos[i][1]+'px; height:'+_height+'px;';
                        $box[0].appendChild(clone);
                    }
                }
            }

        });
    }
})(jQuery);
</script>
<div style="text-align:center;clear:both">
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值