您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍JS如何实现图片预加载之无序预加载功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
图片预加载之无序预加载的效果图如下所示,如果大家感觉不错,请参考实现代码。
具体代码如下所示:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>PreLoading</title> <style> *{margin:0; padding:0; border:none; outline:0; text-decoration:none;} html,body,.box{width:100%; height:100%;} .box{display:none;} #img{width:90%; height:90%; margin:2vh auto 0; display:block; box-shadow:0 0 10px gray;} .box .btns{width:140px; height:40px; display:block; margin:20px auto;} .box .btns .btn{width:60px; height:40px; display:block; border:1px gray solid; background-color:#ccc; text-align:center; line-height:40px; float:left;} .box .btns .btn:nth-of-type(2){margin-left:16px;} .load{width:100%; height:100%; display:block; font-size:60px; font-family:"微软雅黑"; color:#ccc; text-align:center; line-height:100vh; position:fixed;} </style> </head> <body> <div class="box"> <img id="img" src="" alt="pic"> <p class="btns"><a href="javascript:" rel="external nofollow" rel="external nofollow" class="btn">prev</a><a href="javascript:" rel="external nofollow" rel="external nofollow" class="btn">next</a></p> </div> <p class="load">0%</p> <script type="text/javascript"> var imgs = ['http://down.699pic.com/photo/50036/7661.jpg?_upt=da51378d1494571758&_upd=500367661.jpg', 'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/09/0F/ChMkJljskIqIPX9bAAMPyuIn8DcAAbj8QB7XpYAAw_i343.jpg', 'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/09/0F/ChMkJljskLeIaW-JAAIudN_yqvgAAbj8gDQO5AAAi6M64.jpeg', 'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/08/ChMkJlauzISIH0uXAARUHuJLVX8AAH8-gHu6zsABFQ2166.jpg', 'http://desk.fd.zol-img.com.cn/t_s1920x1080c5/g5/M00/0F/08/ChMkJlauzISIIL5TAAObxg4-XeUAAH8-gHzP3EAA5ve000.jpg']; // 绑定按钮事件 var btns = document.getElementsByClassName('btn'), img = document.getElementById('img'), index = 0; for(var i=0;i<btns.length;i++){ btns[i].onclick = function(){ if(this.innerHTML === 'next'){ index = Math.min(++index , imgs.length-1); img.setAttribute('src',imgs[index]); } if(this.innerHTML === 'prev'){ index = Math.max(--index , 0); img.setAttribute('src',imgs[index]); } } } // 计数变量 var count = 0, load = document.getElementsByClassName('load')[0], box = document.getElementsByClassName('box')[0]; // 无序预加载 for(var i=0;i<imgs.length;i++){ (function(i){ var imgObj = new Image(); imgObj.onload = function(){ load.innerHTML = Math.round((count + 1) / imgs.length * 100) + '%'; count++; if(count >= imgs.length-1){ load.style.display = 'none'; box.style.display = 'block'; img.setAttribute('src',imgs[0]); document.title = '1/' + imgs.length; } } imgObj.onerror = function(){ load.innerHTML = Math.round((count + 1) / imgs.length * 100) + '%'; count++; if(count >= imgs.length-1){ load.style.display = 'none'; box.style.display = 'block'; img.setAttribute('src',imgs[0]); document.title = '1/' + imgs.length; } } imgObj.src = imgs[i]; })(i); } </script> </body> </html>
以上是“JS如何实现图片预加载之无序预加载功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。