实现图片首尾平滑轮播(JS原生方法—节流)
网络编程 2021-07-04 17:32www.168986.cn编程入门
狼蚁网站SEO优化长沙网络推广就为大家带来一篇实现图片首尾平滑轮播(JS原生方法—节流)。长沙网络推广觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
给出HTML代码,要注意轮播图片表(#list)末尾加上第一个图片1.jpg,在首部加上一个图片5.jpg。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首尾轮播</title> <link rel="stylesheet" href="首尾轮播.css" rel="external nofollow" > <script src="首尾轮播.js"></script> </head> <body> <div id="container"> <div id="list"> <div><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/5.jpg" alt=""></a></div> <div><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/1.jpg" alt=""></a></div> <div><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/2.jpg" alt=""></a></div> <div><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/3.jpg" alt=""></a></div> <div><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/4.jpg" alt=""></a></div> <div><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/5.jpg" alt=""></a></div> <div><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="../imgs/1.jpg" alt=""></a></div> </div> <div class="arrow" id="prev"><</div> <div class="arrow" id="next">></div> </div> </body> </html>
接下来给出css和js代码,大家可以酌情根据图片的大小、数量、宽度,调整container、list的参数,这也是封装JS所要考虑的参数。
{ margin: 0; padding: 0; } #container{ height: 400px; width: 500px; margin: 0 auto; position: relative; overflow: hidden; border: 1px solid black; } #list>div { float: left; } #list{ position: absolute; height: 400px; width: 3600px; } #list img{ height: 400px; width: 500px; } .arrow { user-select:none; position: absolute; :150px; z-index: 2; background-color: #aaa; height: 100px; width: 80px; cursor: pointer; opacity: 0.5; display: none; line-height: 100px; text-align: center; color: #222; font-size: 3em; } #container:hover .arrow{ display: block; } #prev{ left:20px; } #next{ right: 20px; }
在JS中,我们可以通过改变speed变量来控制图片切换的速度....这里用的是 element.style.transition来控制的过渡效果.
window.onload=function(){ var container = document.getElementById('container'); var list = document.getElementById('list');//获取图片容器 var prev = document.getElementById('prev');//向前按钮 var next = document.getElementById('next');//向后按钮 var nowP = 1; //显示位置 var judge = null; //执行权 var speed = 0.1; // 切换速度 秒 prev.onclick=function(){ if(!judge){ judge = setTimeout(function(){ if(nowP==1){setTimeout(function(){ list.style.transition="left 0s"; list.style.left = "-2500px"; nowP = 5;},speed1000);} //当到达图片表左边缘时与动画同步切换 list.style.transition = "left "+speed+"s"; move(500); nowP--; judge = null; },speed1000); } }; next.onclick=function(){ if(!judge){ judge = setTimeout(function(){ if(nowP==5){setTimeout(function(){ list.style.transition="left 0s"; list.style.left = "-500px"; nowP = 1;},speed1000);} //当到达图片表右边缘时与动画同步切换 list.style.transition = "left "+speed+"s"; move(-500); nowP++; judge = null; },speed1000); } }; function move(num){ var term = parseInt(list.style.left) + num ; list.style.left = term+"px"; } var roll= setInterval(function(){ next.onclick(); },2000); container.onmouseenter=function(){ clearInterval(roll); }; container.onmouseleave=function() { roll=setInterval(function(){ next.onclick(); },2000); }; };
狼蚁网站SEO优化是一个演示demo,简单来说原理就是在切换到图片表首和表尾的动画开始时,设置一个延迟执行时间与动画过渡时间相同的setTimeout函数来执行瞬间切换,再通过节流来保证最大的切换速度(speed)。
本人也是初学前端,如果有帮助的话,点一下推荐吧
以上这篇实现图片首尾平滑轮播(JS原生方法—节流)就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
上一篇:react中的ajax封装实例详解
下一篇:打造通用的匀速运动框架(实例讲解)
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程