美文网首页
瀑布流布局基本思路

瀑布流布局基本思路

作者: 书中有凉气 | 来源:发表于2017-01-04 23:42 被阅读0次

主要思路如下
1.计算页面的宽度,计算出页面可放数据块的列数。
2.将各个数据块的高度尺寸记入数组中
3.用绝对定位先将页面第一行填满,因为第一行的top位置都是一样的,然后用数组记录每一列的总高度。
4.继续用绝对定位将其他数据块定位在最短的一列的位置之后然后更新该列的高度。
5.当浏览器窗口大小改变时,重新执行一次上面1-4步以重新排放(列数随页面宽度而改变,因而需要重新排放)。
6.滚动条滚动到底部时加载新的数据进来后也是定位在最短的一列的位置之后然后更新该列的高度。

预览:
https://jirengu-inc.github.io/jrg-renwu8/homework/%E5%BC%A0%E8%BD%A9/renwu30.html
源码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>renwu30</title>
    <script src='//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'></script>
    <style>
        .water{
            width: 300px;
            margin: 5px;
            position: absolute;
            transition: all 1s;
        }
        .blue{
            background-color: blue;
            height: 300px;
        }
        .green{
            background-color: green;
            height: 400px;
        }
        .grey{
            background-color: grey;
            height: 700px;
        }
        .red{
            background-color: red;
            height: 450px;
        }
        .yellow{
            background-color: yellow;
            height: 250px;
        }
    </style>
</head>
<body>
    <div class="water yellow"></div>
    <div class="water blue"></div>
    <div class="water grey"></div>
    <div class="water yellow"></div>
    <div class="water green"></div>
    <div class="water red"></div>
    <div class="water blue"></div>
    <div class="water yellow"></div>
    <div class="water red"></div>
    <div class="water yellow"></div>
    <div class="water red"></div>
    <div class="water green"></div>
    <div class="water blue"></div>
    <div class="water grey"></div>
    <div class="water blue"></div>
    <div class="water yellow"></div>
    <div class="water blue"></div>
    <div class="water green"></div>
    <div class="water red"></div>
    <div class="water green"></div>
</body>
<script>
    var zhangxuan=(function(){


        function init(f, s){  //传入参数f:父容器,s:瀑布节点
            var $nodeW=s.outerWidth(true),
                $winW=f.width(),
                $nodeNum=parseInt($winW/$nodeW),
                nodesSumHeight=[];

            for(var i=0;i<$nodeNum;i++){
                nodesSumHeight.push(0);
            }

            s.each(function(){   //这边是遍历所选节点,注意内部修改节点css需要用this
                var $el=$(this),
                    $nodeH=$el.outerHeight(true),
                    temp=getmin(nodesSumHeight);
                $el.css({
                    left:$nodeW*temp.idx,
                    top:temp.min
                })
                nodesSumHeight[temp.idx]+= $nodeH  //把this的高度加入数组中
            })



        }

        function getmin(arr){   //用来node高度数组中return最小值和下标
            var idx=0,
                minSumHeight=arr[0];
            for(var i=0;i<arr.length;i++){
                if(arr[i]< minSumHeight){
                    idx=i;
                    minSumHeight= arr[i];
                }
            }
            return{idx:idx, min:minSumHeight}
        }

        $(window).on('resize', function(){
            init($(window), $('.water'))            
        })


        return {
            init:init
        }


    })()

    zhangxuan.init($(window), $('.water'));
</script>
</html>

相关文章

  • 瀑布流布局 的学习

    1- 实现瀑布流布局效果 瀑布流效果瀑布流代码木桶布局效果木桶布局代码瀑布流布局 2- 实现一个新闻瀑布流新闻...

  • 瀑布流、木桶布局

    瀑布流 瀑布流效果代码 木桶布局 木桶布局效果(加载有点慢)代码

  • 瀑布流照片墙布局

    title: 瀑布流照片墙布局 瀑布流概念 瀑布流布局是错落式的布局方式。它有一个特点,整个布局以列为单位,下一个...

  • CSS经典布局

    圣杯布局 瀑布流

  • 瀑布流

    瀑布流布局瀑布流jsonp新闻页

  • 瀑布流布局_木桶布局

    题目1: 实现一个瀑布流布局效果 瀑布流 题目2:实现木桶布局效果 木桶布局 题目3:**实现一个新闻瀑布流新闻网...

  • 瀑布流和懒加载结合

    实现一个瀑布流布局效果 瀑布流

  • 瀑布流

    什么是瀑布流: 欣赏 pinterest 样式分析 瀑布流,又被称作为瀑布流式布局,是一种比较流行的网站页面布局方...

  • 瀑布流

    1、什么是瀑布流 瀑布流,又称瀑布流式布局。是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚...

  • 瀑布流的三种实现

    先来欣赏三个瀑布流的网站 pinterest 淘宝爱逛街 蘑菇街 什么是瀑布流? 瀑布流,又称瀑布流式布局。 这种...

网友评论

      本文标题:瀑布流布局基本思路

      本文链接:https://www.haomeiwen.com/subject/zlvavttx.html