本文实例为大家分享了js实现瀑布流图片效果的具体代码,供大家参考,具体内容如下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; } #wrap{ position: relative; list-style: none; margin: 0 auto; } #wrap li{ width: 200px; font-size: 1.5rem; position: absolute; background-color: #CCCCCC; -webkit-transition: all 2s; } #wrap li div{ width: 100%; height: 100%; overflow: hidden; } </style> </head> <body> <ul id="wrap"></ul> </body> <script type="text/javascript" src="/upload/otherpic41/72925.html","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg"]; $("li").each(function(index, el) { var n = RandomNumBoth(0,imgArr.length-1); var imgsrc = imgArr[n]; $(this).html("<div><img onload='loadImage()' src='"+imgsrc+"'></div>"); }); }); function loadImage(){ $("img").each(function(index, el) { var a=$(this).width(), b=$(this).height(), pa = $(this).parent().width(), pb = $(this).parent().height(), e=a/b, //图片宽高比 3 pe=pa/pb; //承载图片容器宽高比 if(e>=pe){ $(this).css({height:"100%",width:"auto"}); var imgW = $(this).width(), iW = $(this).parent().width(), w = -(imgW - iW)/2; $(this).css('marginLeft',w); }else{ $(this).css({width:"100%",height:"auto"}); var imgH = $(this).height(), iH = $(this).parent().height(), h = -(imgH - iH)/2; $(this).css('marginTop',h); } }); } function RandomNumBoth(Min,Max){ var Range = Max - Min; var Rand = Math.random(); var num = Min + Math.round(Rand * Range); return num; } </script> </html>
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章标题:JavaScript实现瀑布流图片效果-创新互联
文章起源:https://www.cdcxhl.com/article46/djhdhg.html
成都网站建设公司_创新互联,为您提供商城网站、响应式网站、微信公众号、建站公司、域名注册、做网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联