js实现轮播图(手动+自动)

目录

设置大体样式

图片播放

完整代码


设置大体样式

html 复制代码
<input type="button" value="<" id="pre" onclick="pre()" onmouseover="stop()" onmouseout="start()" class="left">
<img src="../img/1.jpg" id="imgid" onmouseover="stop()" onmouseout="start()">
<input type="button" value=">" id="next" onclick="next()" onmouseover="stop()" onmouseout="start()" class="right">

将图片设置为相对定位,按钮设置为绝对定位,这样就可以将按钮镶嵌在图片中

css 复制代码
<style type="text/css">
			#imgs{
				position: relative;
				}
			.right,.left{
				position: absolute;
				top:230px;
				border-radius:50%
			}
			.right{
				left:575px ;
			}
</style>

图片播放

javascript 复制代码
var imgArr = ["../img/1.jpg","../img/2.jpg","../img/3.jpg","../img/4.jpg","../img/5.jpg"];				
				var index =0 ;		
				
				function pre(){
					var img = document.getElementById("imgid");	
					if(index == 0){
						index = 5;
					}
					index-=1;
					img.src = imgArr[index];
				}
				function next(){
					var img = document.getElementById("imgid");	
					if(index == 4){
						index = -1;
					}
					index+=1;
					img.src = imgArr[index];
}

添加dom对象,将图片存入数组中,用下标来访问图片内容

javascript 复制代码
var t;
function start(){
	t = setInterval(next,1000)
}
function stop(){
	clearInterval(t);
}

设置自动播放的事件, 并添加onmouseover和onmouseout事件,当鼠标放在图片或者按钮上,停止自动播放,离开后开始自动播放

完整代码

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#imgs{
				position: relative;
				}
			.right,.left{
				position: absolute;
				top:230px;
				border-radius:50%
			}
			.right{
				left:575px ;
			}	
		</style>
	</head>
	<body>
		<script>		
				var imgArr = ["../img/1.jpg","../img/2.jpg","../img/3.jpg","../img/4.jpg","../img/5.jpg"];				
				var index =0 ;		
				
				function pre(){
					var img = document.getElementById("imgid");	
					if(index == 0){
						index = 5;
					}
					index-=1;
					img.src = imgArr[index];
				}
				function next(){
					var img = document.getElementById("imgid");	
					if(index == 4){
						index = -1;
					}
					index+=1;
					img.src = imgArr[index];
				}
				var t;
				function start(){
					t = setInterval(next,1000)
				}
				
				function stop(){
					clearInterval(t);
				}
		</script>	
		<input type="button" value="<" id="pre" onclick="pre()" onmouseover="stop()" onmouseout="start()" class="left">
		<img src="../img/1.jpg" id="imgid" onmouseover="stop()" onmouseout="start()">
		<input type="button" value=">" id="next" onclick="next()" onmouseover="stop()" onmouseout="start()" class="right">
	</body>
</html>
相关推荐
JOJO___13 分钟前
Spring MVC 全局异常 总结
java·spring·mvc
潘多编程19 分钟前
Spring Boot集成LiteFlow使用详解
java·spring boot·后端
码农褚20 分钟前
java springboot 集成 okHttp 发送get、post请求
java·spring boot·okhttp
啊卡无敌1 小时前
Spring事务
java·数据库·mysql·spring
噜噜噜鹿鹿1 小时前
【秋招笔试题】多多排序
java·开发语言
pink大呲花1 小时前
JavaScript 输出方式
开发语言·javascript·ecmascript
aeoliancrazy1 小时前
el-table翻页记录勾选,正常记录取消勾选,不受翻页影响
前端·vue.js·elementui
dzx1562 小时前
Java基础|多线程:多线程分页拉取
java·开发语言
BK俊河2 小时前
SpringBoot基础知识
java·spring boot·spring
CH3_CH2_CHO2 小时前
JAVA基本简介(期末)
java