首页 > 代码库 > Masonry插件:内容始终水平居中
Masonry插件:内容始终水平居中
跟随浏览器框架大小,主题内容居中
代码
<!DOCTYPE html> <html> <head> <title>masonry瀑布流插件</title> <meta charset="utf-8"> <script src=http://www.mamicode.com/"./jquery.min.js"></script> <script src=http://www.mamicode.com/"./masonry.js"></script> <style> #masonry{margin: 0 auto;} #masonry .box{width: 250px; padding: 0px; background: #f00; margin: 0 0 10px;} #masonry .box img{width: 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=http://www.mamicode.com/"./images/a02.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a01.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a03.jpg"></div> <div class="box">343</div> <div class="box"><img src=http://www.mamicode.com/"./images/a02.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a01.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a03.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a01.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a03.jpg"></div> <div class="box">343</div> <div class="box"><img src=http://www.mamicode.com/"./images/a02.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a01.jpg"></div> <div class="box">111</div> <div class="box"><img src=http://www.mamicode.com/"./images/a02.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a01.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a03.jpg"></div> <div class="box">343</div> <div class="box"><img src=http://www.mamicode.com/"./images/a02.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a01.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a03.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a01.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./images/a03.jpg"></div> <div class="box">343</div> <div class="box"><img src=http://www.mamicode.com/"./images/a02.jpg"></div> <div class="box"><img src=http://www.mamicode.com/"./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>
Masonry插件:内容始终水平居中
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。