JavaScript与HTML5实现美女瀑布流布局,本方法是把图片的路径写在了JS的数组里,不过重点好像不是在这里,而是在图片如何自动排列的问题,你可以运行本实例后,点击“加载瀑布流布局”按钮,即可看到图片的瀑布流排列效果,现在很流行这个,希望您从本代码中能找到一些灵感。
<!DOCTYPE HTML> <html> <head> <title>jQuery+HTML5图片瀑布流效果丨河北电动叉车|石家庄苗木</title> <style type="text/css"> *{margin:0;padding:0} div.section{overflow:hidden} div.aside{width:225px;float:left;display:inline} div.aside div.content{margin:5px;background:#666} div.aside div.content img{width:205px;margin:5px;} div.aside div.content div.imgcaption{margin:0 5px;line-height:20px} </style> </head> <body> <div class="section"> <div class="aside"></div> <div class="aside"></div> <div class="aside"></div> <div class="aside"></div> </div> <button>加载瀑布流布局</button> </body> <script type="text/javascript" src="/images/jquery-1.6.2.min.js"></script> <script type="text/javascript"> var json=[ {"text":"1","src":"/images/m01.jpg","height":"273"}, {"text":"2","src":"/images/m02.jpg","height":"273"}, {"text":"3","src":"/images/m03.jpg","height":"306"}, {"text":"4","src":"/images/m04.jpg","height":"270"}, {"text":"5","src":"/images/m05.jpg","height":"273"}, {"text":"6","src":"/images/m06.jpg","height":"307"}, {"text":"7","src":"/images/m07.jpg","height":"272"}, {"text":"8","src":"/images/m08.jpg","height":"285"}, {"text":"9","src":"/images/m09.jpg","height":"303"}, {"text":"10","src":"/images/m10.jpg","height":"272"}, {"text":"11","src":"/images/m05.jpg","height":"273"}, {"text":"12","src":"/images/m07.jpg","height":"272"}, {"text":"13","src":"/images/m04.jpg","height":"285"}, {"text":"14","src":"/images/m09.jpg","height":"303"}, {"text":"15","src":"/images/m10.jpg","height":"272"}, {"text":"16","src":"/images/m01.jpg","height":"273"}, {"text":"17","src":"/images/m02.jpg","height":"273"}, {"text":"18","src":"/images/m03.jpg","height":"280"} ] function getSmallDiv(wrap,oD){ var len=oD.length; var h=Infinity; var getD; for(var i=0;i<len;i++){ if(oD.eq(i).height()<h){ h=oD.eq(i).height(); getD=oD.eq(i); } } return getD; } $("button").click(function(){ for(var i=0;i<json.length;i++){ var str; str="<div class=\"content\">"; str+="<img src="+json[i].src+" height="+json[i].height+" alt=\"\" />"; str+="<div class=\"imgcaption\">"+json[i].text+"</div>"; str+="</div>"; getSmallDiv($(".section"),$(".aside")).append(str); } }); </script> </html> <br><br><br>所需js文件:<a href="/images/jquery-1.6.2.min.js" target=_blank>jquery-1.6.2.min.js</a><br><hr>
时间: 2024-10-12 21:43:51