js实现无缝轮播图
网络编程 2021-07-04 15:02www.168986.cn编程入门
这篇文章主要为大家详细介绍了js实现无缝轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了js实现轮播图的具体代码,供大家参考,具体内容如下
CSS样式:
<style type="text/css"> #box{width: 1000px;height: 375px;border: 3px solid black; margin: 30px auto;position: relative;overflow: hidden;} #box .img{position: absolute;left: 0;: 0;} #box .img img{width: 1000px;height: 375px;float: left;} #box .btn input{border-radius: 50%;border: 0;width: 40px; height: 40px;font-size: 25px;z-index: 5;: 165px;position: absolute;} #left{left: 0;} #right{right: 0;} </style>
HTML结构:
<div id="box"> <div class="img"> <img src="img/1.jpg" > <img src="img/2.jpg" > <img src="img/3.jpg" > <img src="img/4.jpg" > <img src="img/1.jpg" > //利用第一张图,达到无缝轮播的视觉效果 </div> <div class="btn"> <input type="button" id="left" value="<" /> <input type="button" id="right" value=">" /> </div> </div>
JavaScript:
<script> class LunBO{ constructor() { this.img = document.querySelector(".img"); this.child = this.img.children; this.left = document.getElementById("left"); this.right = document.getElementById("right"); this.index = 0; } init(){ this.img.style.width = this.child.length * this.child[0].offsetWidth + "px"; //有多少图片设置多少宽度 } addEvent(){ var that = this; this.left.onclick = function(){ that.changeIndex(1); //点击左按钮,设置索引 } this.right.onclick = function(){ that.changeIndex(2); //点击右按钮,设置索引 } } changeIndex(i){ if(i==1){ if(this.index == 0){ this.index = this.child.length - 2; this.img.style.left = -(this.child.length-1) * this.child[0].offsetWidth + "px"; }else{ this.index--; } }else{ if(this.index == this.child.length-1){ this.index = 1; this.img.style.left = 0; }else{ this.index++; } } this.setSplace(); } setSplace(){ move(this.img,"left",-this.index * this.child[0].offsetWidth); //调用动画 } } var lb = new LunBO(); lb.init(); lb.addEvent(); </script>
设置动画:
function move(ele,attr,target){ clearInterval(ele.t); ele.t=setInterval(()=>{ let iNow; if(attr=="opacity"){ iNow=getStyle(ele,attr)*100; }else{ iNow=parseInt(getStyle(ele,attr)); } let speed=(target-iNow)/8; speed=speed<0?Math.floor(speed):Math.ceil(speed); if(iNow==target){ clearInterval(ele.t); }else{ if(attr=="opacity"){ ele.style.opacity=(iNow+speed)/100; }else{ ele.style[attr]=iNow+speed+"px"; } } },30) } function getStyle(ele,attr){ if(ele.currentStyle){ return ele.currentStyle[attr]; }else{ return getComputedStyle(ele,false)[attr]; } }
精彩专题分享:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程