网页瀑布流布局实现方法及功能扩展

Web前端评论1K阅读模式

图片分享网站>Pinterest(beta), 在不久前获得了2700$的投资,估值已超2亿元(据说现该公司团队有8人)。该网站的一大特色就是每一张分享的图片被拟作“Pin”被钉在页面上面,每 一Pin依尺寸智能排列,同时滚动页面时异步加载新Pin,极具效果。这种创新模式被在国内也被广泛copy,其中最有前途的当属Mark之,以 “Mark”代“Pin”,也算有些想法。

下面简单模拟下这种布局,这种布局在国内有人称之为瀑布流,当然这个名称主要还是指Pin在异步加载时的效果,而这个在本demo并没有做。(本demo兼容所有浏览器,但还是建议你使用firefox,chrome等浏览器,效果更佳)

  1. <!doctype html>  
  2.   <html>  
  3.   <head>  
  4.   <meta charset="UTF-8" />  
  5.   <title>等宽格子堆砌</title>  
  6.   <style>  
  7.   *{padding:0;margin:0;}  
  8.   #wrap{position:relative;zoom:1;margin:0px auto;}  
  9.   #wrap li{width:250px;float:left;list-style:none;}  
  10.   .boxCont{position:relative;margin:15px;border:1px solid #ccc;background:#eee;  
  11. background: -webkit-gradient(linear, 0% 20%, 0% 92%, from(#fff), to(#f3f3f3), color-stop(.1,#fff));  
  12. background: -webkit-linear-gradient(0 0 270deg, #f3f3f3, #f3f3f3 10%, #fff);  
  13. background: -moz-linear-gradient(0 0 270deg, #f3f3f3, #f3f3f3 10%, #fff);  
  14. background: -o-linear-gradient(0 0 270deg, #f3f3f3, #f3f3f3 10%, #fff);  
  15. -webkit-border-radius: 60px / 5px;  
  16. -moz-border-radius: 60px / 5px;  
  17. border-radius:60px / 5px;  
  18. -webkit-box-shadow: 0px 0px 35px rgba(0, 0, 0, 0.1) inset;  
  19. -moz-box-shadow: 0px 0px 35px rgba(0, 0, 0, 0.1) inset;  
  20. box-shadow: 0px 0px 35px rgba(0, 0, 0, 0.1) inset;  
  21. }  
  22. .boxCont:before{  
  23. content: '';  
  24. width: 50px;  
  25. height: 50px;  
  26. top:0; right:0;  
  27. position:absolute;  
  28. display: inline-block;  
  29. z-index:-1;  
  30. -webkit-box-shadow: 10px -10px 8px rgba(0, 0, 0, 0.2);  
  31. -moz-box-shadow: 10px -10px 8px rgba(0, 0, 0, 0.2);  
  32. box-shadow: 10px -10px 8px rgba(0, 0, 0, 0.2);  
  33. -webkit-transform: rotate(2deg) translate(-14px,20px) skew(-20deg);  
  34. -moz-transform: rotate(2deg) translate(-14px,20px) skew(-20deg);  
  35. -o-transform: rotate(2deg) translate(-14px,20px) skew(-20deg);  
  36. transform: rotate(2deg) translate(-14px,20px) skew(-20deg);  
  37. }  
  38. .boxCont:after{  
  39. content: '';  
  40. width: 100px;  
  41. height: 100px;  
  42. top:0; left:0;  
  43. position:absolute;  
  44. z-index:-1;  
  45. display: inline-block;  
  46. -webkit-box-shadow: -10px -10px 10px rgba(0, 0, 0, 0.2);  
  47. -moz-box-shadow: -10px -10px 10px rgba(0, 0, 0, 0.2);  
  48. box-shadow: -10px -10px 10px rgba(0, 0, 0, 0.2);  
  49. -webkit-transform: rotate(2deg) translate(20px,25px) skew(20deg);  
  50. -moz-transform: rotate(2deg) translate(20px,25px) skew(20deg);  
  51. -o-transform: rotate(2deg) translate(20px,25px) skew(20deg);  
  52. transform: rotate(2deg) translate(20px,25px) skew(20deg);  
  53. }  
  54. </style>  
  55. </head>  
  56. <body>  
  57. <ul id="wrap"></ul>  
  58. </body>  
  59. <script type="text/javascript">  
  60. var $id = function(o){ return document.getElementById(o) || o};  
  61. function sort(el){  
  62. var h = [];  
  63. var box = el.getElementsByTagName("li");  
  64. var minH = box[0].offsetHeight,  
  65. boxW = box[0].offsetWidth,  
  66. boxH,  
  67. n = document.documentElement.offsetWidth / boxW | 0;  //计算页面能排下多少Pin  
  68. el.style.width = n * boxW + "px";  
  69. for(var i = 0; i < box.length; i++) {  
  70. boxh = box[i].offsetHeight; //获取每个Pin的高度  
  71. if(i < n) { //第一行Pin以浮动排列,不需绝对定位  
  72. h[i] = boxh;  
  73. box[i].style.position = '';  
  74. else {  
  75. minH =  Math.min.apply({},h); //取得各列累计高度最低的一列  
  76. minKey = getarraykey(h, minH);  
  77. h[minKey] += boxh ; //加上新高度后更新高度值  
  78. box[i].style.position = 'absolute';  
  79. box[i].style.top = minH + 'px';  
  80. box[i].style.left = (minKey * boxW) + 'px';  
  81. }  
  82. }  
  83. };  
  84. /* 返回数组中某一值的对应项数 */  
  85. function getarraykey(s, v) {  
  86. for(k in s) {  
  87. if(s[k] == v) {  
  88. return k;  
  89. }  
  90. }  
  91. };  
  92. /* 随机创建Pin */  
  93. var pin = '';  
  94. for(i = 0; i < 30; i++) {  
  95. height = Math.floor(Math.random()*200 + 200);  
  96. pin += '<li><div class="boxCont" style="height:' + height + 'px;"></div></li>';  
  97. };  
  98. $id("wrap").innerHTML = pin;  
  99. window.onload = window.onresize = function() {  
  100. sort($id("wrap"));  
  101. };  
  102. </script>  
  103. </html>  

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证