zoukankan      html  css  js  c++  java
  • Masonry插件:内容始终水平居中

    跟随浏览器框架大小,主题内容居中

    代码

    <!DOCTYPE html>
     <html>
    <head>
        <title>masonry瀑布流插件</title>
        <meta charset="utf-8">
        <script src="./jquery.min.js"></script>
        <script src="./masonry.js"></script>
        <style>
            #masonry{margin: 0 auto;}
            #masonry .box{ 250px; padding: 0px; background: #f00; margin:  0 0 10px;}
            #masonry .box img{ 100%}
    
            .text{text-align:  center;padding:  20px; background: #eee; margin-bottom:  20px;}
        </style>
    </head>
    <body>
    <div class="text">浏览器不管怎么切换 Masonry内容都会居中</div>
    <div class="content">
        <div id="masonry">
            <div class="box">111</div>
            <div class="box"><img src="./images/a02.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
            <div class="box"><img src="./images/a03.jpg"></div>
            <div class="box">343</div>
            <div class="box"><img src="./images/a02.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
            <div class="box"><img src="./images/a03.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
            <div class="box"><img src="./images/a03.jpg"></div>
            <div class="box">343</div>
            <div class="box"><img src="./images/a02.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
            <div class="box">111</div>
            <div class="box"><img src="./images/a02.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
            <div class="box"><img src="./images/a03.jpg"></div>
            <div class="box">343</div>
            <div class="box"><img src="./images/a02.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
            <div class="box"><img src="./images/a03.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
            <div class="box"><img src="./images/a03.jpg"></div>
            <div class="box">343</div>
            <div class="box"><img src="./images/a02.jpg"></div>
            <div class="box"><img src="./images/a01.jpg"></div>
        </div>
    </div>
    <script type="text/javascript">
        $(function() {
            var $container = $('#masonry');
            $container.imagesLoaded(function() {
                $container.masonry({
                    itemSelector: '.box',
                    gutter: 20, // 盒子间距
                    isAnimated: true,
    
                });
            });
    
            masonryWidth();
        });
    
        // 设置masonry 宽度
        function masonryWidth(){
            $browser = document.body.clientWidth;
    
            $boxWidth = 250;         // 盒子宽度
            $boxSpacing = 20;        //盒子与盒子之间的间距
    
            if($browser <=500 ){
                $("#masonry").css('width',250);
            }else{
                $num = ($browser-$boxWidth) / ($boxWidth + $boxSpacing );
                $n = parseInt($num);
                $masonryWidth = $n * ($boxWidth + $boxSpacing) + $boxWidth;
    
                $("#masonry").css('width',$masonryWidth);
            }
    
            // 浏览器窗口变动
            window.onresize = masonryWidth;
        }
    
        </script>
    </body>
    </html>
  • 相关阅读:
    FineUI官方论坛出现空白页的解决办法!
    【三石jQuery视频教程】03.创建垂直时间表(Timeline)
    【三石jQuery视频教程】02.创建 FontAwesome 复选框和单选框
    【三石jQuery视频教程】01.图片循环展示_再次重发
    【三石jQuery视频教程】01.图片循环展示
    FineUI小技巧(7)多表头表格导出
    FineUI(专业版)实现百变通知框(无JavaScript代码)!
    FineUI(专业版)v1.2.0 和 FineUI(开源版)v4.1.1 同时发布!
    FineUI(专业版)新增 5 款 Metro 皮肤,邀您共赏!
    FineUI(专业版)公测版发布(这速度,真TM快!)
  • 原文地址:https://www.cnblogs.com/wesky/p/6690104.html
Copyright © 2011-2022 走看看