美文网首页
进阶任务十八-瀑布流布局

进阶任务十八-瀑布流布局

作者: RookieD | 来源:发表于2017-10-17 16:40 被阅读0次

实现一个瀑布流布局效果

<!doctype html>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <style>
    .content{
      position: relative;
    }

    .item{
      position: absolute;
      width: 200px;
      margin-right: 10px;
      margin-top: 10px;
      transition: all 1s;
    }

    .h1{
      height: 200px;
      background-color: #f4b300;
    }

    .h2{
      height: 300px;
      background-color: #691BB8;
    }

    .h3{
      height: 400px;
      background-color: #006ac1;
    }
    </style>
    <body>  
        <div class="content">
            <div class="item h1">
                1
            </div>
            <div class="item h3">
                2
            </div>
            <div class="item h2">
                3
            </div>
            <div class="item h1">
                4
            </div>
            <div class="item h1">
                5
            </div>
            <div class="item h3">
                6
            </div>
            <div class="item h3">
                7
            </div>
            <div class="item h2">
                8
            </div>
            <div class="item h1">
                9
            </div>
            <div class="item h3">
                10
            </div>
            <div class="item h3">
                11
            </div>
            <div class="item h3">
                12
            </div>
            <div class="item h2">
                13
            </div>
            <div class="item h2">
                14
            </div>
        </div>
        <script>
            waterfull()
            $(window).resize(function() {
                waterfull()
            })
            
            function waterfull(){
                var colLength = parseInt($(".content").width()/$(".item").width())
                var itemArr = []
                for(var i = 0; i < colLength; i++) {
                    itemArr[i] = 0
                }

                $(".item").each(function() {
                    var minValue = Math.min.apply(null, itemArr)
                    var minIndex = itemArr.indexOf(minValue)

                    $(this).css({
                        top: itemArr[minIndex],
                        left: $(this).outerWidth(true) * minIndex
                    })

                    itemArr[minIndex] += $(this).outerHeight(true)
                })
            }
            
        </script>
    </body> 
</html>

根据课程视频实现一个新闻瀑布流新闻网站

<!doctype html>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <style>
        html,body,ul,li,p,div {
            padding:0;
            margin: 0;
            box-sizing: border-box;
        }

        ul,li {
            list-style: none;
        }

        .warp {
            width: 900px;
            margin: 0 auto;
        }

        .clearfix:after {
            content: "";
            display: block;
            clear: both;
        }

        #pic-ct {
            position: relative;
        }

        #pic-ct .item {
            position: absolute;
            padding: 0 0 10px 0;
            width: 280px;
            margin: 10px;
            border: 1px solid #dfdfdf;
            background: #fff;
            transition: all 0.8s;
            opacity: 0;
        }

        #pic-ct .item img {
            margin: 10px;
            width: 260px;
        }

        #pic-ct .item .header {
            height: 25px;
            margin: 0 12px;
            border-bottom: 1px solid #dbdbdb;
        }

        #pic-ct .desp {
            font-size: 12px;
            line-height: 1.8;
            margin: 10px 15px 0;
            color: #777371;
        }
        .load {
            visibility: hidden;
            height: 1px;
        }
        .hide {
            display: none;
        }
    </style>
    <body>  
        <div class="warp">
            <div class="ct-waterfall">
                <ul id="pic-ct" class="clearfix">
                    <li class="item hide"></li>
                </ul>
                <div class="load"></div>
            </div>
        </div>
        <script>
            var curPage = 1;
            var perPageCount = 12
            var colSumHeight = []
            var nodeWidth = $(".item").outerWidth(true)
            var colNum = parseInt($("#pic-ct").width()/nodeWidth)
            for(var i=0; i < colNum; i++) {
                colSumHeight[i] = 0
            }

            start()

            $(window).scroll(function() {
                if(isVisible($(".load"))) {
                    start()
                    console.log("test")
                }
            })


            function start() {
                getData(function(newsList) {
                    $.each(newsList, function(idx, news) {
                        var $node = getNode(news)
                        $node.find("img").load(function(){
                            $("#pic-ct").append($node)
                            waterFallPlace($node)
                        })
                    })
                })
            }
            

            function getData(callback) {
                $.ajax({
                    url: "http://platform.sina.com.cn/slide/album_tech",
                    dataType: "jsonp",
                    jsonp: "jsoncallback",
                    data: {
                        app_key: "1271687855",
                        num: perPageCount,
                        page: curPage,
                    }
                }).done(function(ret) {
                    if(ret && ret.status && ret.status.code === "0"){
                        callback(ret.data);
                        curPage++
                    }else {
                        console.log("get error data")
                    }
                })
            }

            function getNode(item){
                var tpl = ''
                tpl += '<li class="item">'
                tpl += '<a href="' + item.url +'" class="link">![](' + item.img_url + ')</a>'
                tpl += '<h4 class="header">' + item.short_name + '</h4>'
                tpl += '<p class="desp">' + item.short_intro + '</p>'
                tpl += '</li>'
                return $(tpl)
            }

            function waterFallPlace($node) {
                var idx = 0
                var minSumHeight = colSumHeight[0]

                for (var i = 0; i < colSumHeight.length; i++) {
                    if(colSumHeight[i] < minSumHeight) {
                        idx = i
                        minSumHeight = colSumHeight[i]
                    }
                }

                $node.css({
                    left: nodeWidth * idx,
                    top: minSumHeight,
                    opacity: 1
                })

                colSumHeight[idx] += $node.outerHeight(true)
                $("#pic-ct").height(Math.max.apply(null, colSumHeight))
            }

            function isVisible($el) {
                var scrollH = $(window).scrollTop()
                var winH = $(window).height()
                var top = $el.offset().top

                if( top < winH + scrollH) {
                    return true
                }else {
                    return false
                }
            }

        </script>
    </body> 
</html>

相关文章

  • 进阶任务十八-瀑布流布局

    实现一个瀑布流布局效果 根据课程视频实现一个新闻瀑布流新闻网站

  • 瀑布流布局 的学习

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

  • 瀑布流、木桶布局

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

  • 进阶任务18 瀑布流布局

    瀑布流布局预览 新闻瀑布流网站预览 这个由于自己选的API返回的数据里description 都是一样的,所以每个...

  • 瀑布流照片墙布局

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

  • CSS经典布局

    圣杯布局 瀑布流

  • 瀑布流

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

  • 瀑布流布局_木桶布局

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

  • 瀑布流和懒加载结合

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

  • 瀑布流

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

网友评论

      本文标题:进阶任务十八-瀑布流布局

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