用原生JS做呼吸轮播图的方法及代码是什么
Admin 2022-06-24 群英技术资讯 319 次浏览
今天给大家分享一个用原生JS实现的呼吸轮播图,效果如下:
以下是代码实现,欢迎大家复制粘贴。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>原生JS实现呼吸轮播图</title> <style> ul { margin: 0; padding-left: 0; } li { list-style: none; } img { border: none; } #main { width: 280px; height: 330px; overflow: hidden; position: relative; margin: 20px auto 0 auto; } #main ul { position: absolute; left: 0; } #main ul li { width: 280px; height: 330px; float: left; position: absolute; filter: alpha(opacity=0); opacity: 0; } #btn { line-height: 14px; text-align: center; background: #eeeeee; width: 280px; margin: 10px auto 0 auto; display: flex; flex-direction: row; justify-content: space-around; align-items: center; } #btn a { display: inline-block; width: 14px; height: 14px; text-decoration: none; line-height: 12px; text-align: center; border-radius: 7px; } #btn a.index { background-color: #ccc; } #btn a.active { background-color: red; } </style> <script type="text/javascript" src="js/move.js"></script> <script> window.onload = function () { var oMain = document.getElementById('main'); var oUl = oMain.getElementsByTagName('ul')[0]; var aLi = oUl.getElementsByTagName('li'); var oBtn = document.getElementById('btn'); var aA = oBtn.getElementsByTagName('a'); var iNow = 1; var iNow2 = 1; var bBtn = true; var num = 3; var timer = null; oUl.style.width = aLi.length * aLi[0].offsetWidth + 'px'; aA[0].onclick = function () { if (bBtn) { clearInterval(timer); timer = setInterval(autoPlay, 3000); for (var i = 0; i < aLi.length; i++) { aLi[i].style.position = 'relative'; aLi[i].style.filter = 'alpha(opacity=100)'; aLi[i].style.opacity = 1; } oUl.style.left = -(iNow - 1) * aLi[0].offsetWidth + 'px'; if (iNow == 1) { iNow = aLi.length; aLi[aLi.length - 1].style.position = 'relative'; aLi[aLi.length - 1].style.left = -aLi.length * aLi[0].offsetWidth + 'px'; } else { iNow--; } iNow2--; toRun(); bBtn = false; } }; aA[aA.length - 1].onclick = function () { if (bBtn) { clearInterval(timer); timer = setInterval(autoPlay, 3000); for (var i = 0; i < aLi.length; i++) { aLi[i].style.position = 'relative'; aLi[i].style.filter = 'alpha(opacity=100)'; aLi[i].style.opacity = 1; } oUl.style.left = -(iNow - 1) * aLi[0].offsetWidth + 'px'; if (iNow == aLi.length) { iNow = 1; aLi[0].style.position = 'relative'; aLi[0].style.left = aLi.length * aLi[0].offsetWidth + 'px'; } else { iNow++; } iNow2++; toRun(); bBtn = false; } }; for (var i = 1; i < aA.length - 1; i++) { aA[i].index = i; aA[i].onclick = function () { clearInterval(timer); timer = setInterval(autoPlay, 3000); iNow = this.index; iNow2 = this.index; toShow(); }; }; function toRun() { for (var i = 1; i < aA.length - 1; i++) { aA[i].className = 'index'; } aA[iNow].className = 'active'; startMove(oUl, { left: -(iNow2 - 1) * aLi[0].offsetWidth }, function () { if (iNow == 1) { aLi[0].style.position = 'relative'; aLi[0].style.left = 0; oUl.style.left = 0; iNow2 = 1; } else if (iNow == aLi.length) { aLi[aLi.length - 1].style.position = 'relative'; aLi[aLi.length - 1].style.left = 0; oUl.style.left = -(aLi.length - 1) * aLi[0].offsetWidth + 'px'; iNow2 = aLi.length; } for (var i = 0; i < aLi.length; i++) { aLi[i].style.position = 'absolute'; aLi[i].style.filter = 'alpha(opacity=0)'; aLi[i].style.opacity = 0; } oUl.style.left = 0; aLi[iNow2 - 1].style.zIndex = num++; aLi[iNow2 - 1].style.filter = 'alpha(opacity=100)'; aLi[iNow2 - 1].style.opacity = 1; bBtn = true; }); }; function toShow() { for (var i = 1; i < aA.length - 1; i++) { aA[i].className = 'index'; } for (var i = 0; i < aLi.length; i++) { startMove(aLi[i], { opacity: 0 }); } aA[iNow].className = 'active'; startMove(aLi[iNow - 1], { opacity: 100 }, function () { aLi[iNow - 1].style.zIndex = num++; }); } timer = setInterval(autoPlay, 3000); function autoPlay() { if (iNow == aLi.length) { iNow = 1; iNow2 = 1; } else { iNow++; iNow2++; } toShow(); } }; </script> </head> <body> <div id="main"> <ul> <li style="z-index:2; filter:alpha(opacity=100); opacity:1;"> <a> <img src="images/0.jpg" /> </a> </li> <li> <a> <img src="images/1.jpg" /> </a> </li> <li> <a> <img src="images/2.jpg" /> </a> </li> <li> <a> <img src="images/3.jpg" /> </a> </li> </ul> </div> <div id="btn"> <a class="prev" href="javascript:;"> <</a> <a class="active" href="javascript:;"> </a> <a class="index" href="javascript:;"></a> <a class="index" href="javascript:;"></a> <a class="index" href="javascript:;"></a> <a class="next" href="javascript:;">></a> </div> </body> </html>
以下是上面代码中引入的最重要的运动函数 move.js的代码:
function startMove(obj, json, fnEnd) { clearInterval(obj.timer); obj.timer = setInterval(function () { doMove(obj, json, fnEnd); }, 30); } function doMove(obj, json, fnEnd) { var iCur = 0; var attr = null; var bStop = true; for (attr in json) { if (attr == 'opacity') { if (parseInt(100 * getStyle(obj, attr)) == 0) { iCur = parseInt(100 * getStyle(obj, attr)); } else { iCur = parseInt(100 * getStyle(obj, attr)) || 100; } } else { iCur = parseInt(getStyle(obj, attr)) || 0; } var iSpeed = (json[attr] - iCur) / 5; iSpeed = (iSpeed > 0) ? Math.ceil(iSpeed) : Math.floor(iSpeed); if (json[attr] != iCur) { bStop = false; } if (attr == 'opacity') { obj.style.filter = 'alpha(opacity=' + (iCur + iSpeed) + ')'; obj.style.opacity = (iCur + iSpeed) / 100; } else { obj.style[attr] = iCur + iSpeed + 'px'; } } if (bStop) { clearInterval(obj.timer); if (fnEnd) { fnEnd.call(obj); } } } function stopMove(obj) { clearInterval(obj.timer); } function getStyle(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj)[attr]; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
最近在开发一套系统,前端使用VUE开发,由于本人是后端开发,前端也会一点,但是VUE接触不多,在VUE项目开发遇到的一些坑记录一下,不是专业前端写好的不好,大家不要唝。。。在VUE项目中
本篇文章带大家吃透JavaScript中的程序控制流与函数,希望对大家有所帮助!
这篇文章主要为大家详细介绍了VUE使用canvas实现签名组件,兼容PC移动端,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
javascript中的运算符大多由标点符号表示,少数由关键字表示,它们的语法言简意赅,它们的数量却着实不少,下面这篇文章主要给大家介绍了关于JavaScript中一些强大的运算符的相关资料,需要的朋友可以参考下
本篇文章给大家带来了关于javascript的相关知识,其中主要整理了处理树状结构数据的增删改查的相关问题,相比普通的数组结构数据,树状结构的处理就没有数组那么的直观,但是也没那么复杂,需要多一步递归查找来对数据进行深度遍历操作,下面一起来看一下,希望对大家有帮助。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008