用jquery實現瀑布流(方式1-固定寬度和列數,按順序添加圖片)

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>瀑布流</title>
    <style type="text/css">
        *{margin: 0;padding: 0}
        #container{width: 90%;margin:0 auto;}
        .topic {width:200px; border:1px solid #ccc; border-radius:5px; padding:5px; box-shadow:5px 5px 5px #ccc; float:left; margin:5px;}
        .topic img {width:200px;}
    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.js"></script>
    <script type="text/javascript">
    $(function(){
        
            var conWidth=$("#container").innerWidth(),
                toWidth=$(".topic").eq(0).outerWidth(),
                cols=Math.floor(conWidth/toWidth),
                spacing=(conWidth-toWidth*cols)/(cols+1);
            var html="";
            console.log(cols);
            //向contaner容器中存放cols個列的盒子
            for(var i=0;i<cols;i++){
                html+="<div style=‘display:inline-block;vertical-align:top;width:"+toWidth+"px;margin-left:"+spacing+"px;‘></div>"
            }
            var $conCols=$(html).appendTo("#container");
            //遍曆載入的圖片
            $(".topic").each(function(index,element){
                var cur=index%cols;
                $conCols.eq(cur).append(element);
            });    
            //動態向頁面添加圖片
            html="";
            for(var i=16;i<56;i++){
                html+="<div class=‘topic‘><img src=‘img/"+i+".jpg‘></div>";
            }
            $(html).each(function(index,element){
                    var cur=index%cols;
                    $conCols.eq(cur).append(element);
            });
    });
    </script>
</head>
<body>
    <div id="container">
        <div class="topic"><img src="img/1.jpg"></div>
        <div class="topic"><img src="img/2.jpg"></div>
        <div class="topic"><img src="img/3.jpg"></div>
        <div class="topic"><img src="img/4.jpg"></div>
        <div class="topic"><img src="img/5.jpg"></div>
        <div class="topic"><img src="img/6.jpg"></div>
        <div class="topic"><img src="img/7.jpg"></div>
        <div class="topic"><img src="img/8.jpg"></div>
        <div class="topic"><img src="img/9.jpg"></div>
        <div class="topic"><img src="img/10.jpg"></div>
        <div class="topic"><img src="img/11.jpg"></div>
        <div class="topic"><img src="img/12.jpg"></div>
        <div class="topic"><img src="img/13.jpg"></div>
        <div class="topic"><img src="img/14.jpg"></div>
        <div class="topic"><img src="img/15.jpg"></div>
    </div>
</body>
</html>

用jquery實現瀑布流(方式1-固定寬度和列數,按順序添加圖片)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.