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

目录

设置大体样式

图片播放

完整代码


设置大体样式

<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">

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

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

图片播放

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对象,将图片存入数组中,用下标来访问图片内容

var t;
function start(){
	t = setInterval(next,1000)
}
function stop(){
	clearInterval(t);
}

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

function oper(oper){
					document.getElementById("pre").style.opacity=oper;
					document.getElementById("next").style.opacity=oper;
}

设置透明度,当鼠标移入图片时,显示按钮,当鼠标离开图片时,按钮消失 

完整代码

<!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);
				}
                function oper(oper){
					document.getElementById("pre").style.opacity=oper;
					document.getElementById("next").style.opacity=oper;
				}
		</script>
        <div onmouseover="oper(0.5)" onmouseout="oper(0)" >	
		<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">
        <div/>
	</body>
</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用HTML和JavaScript来实现,你可以使用以下代码作为参考: HTML代码: ```html <div class="cut"> <img class="img" src="./images/1.webp" alt=""> <div class="contral prev"><<</div> <div class="contral next">>></div> <ul class="btn"><!-- 圆点 --> <li class="active"></li> <li></li> <li></li> </ul> </div> ``` CSS代码: ```css <style type="text/css"> .cut { position: relative; width: 590px; height: 470px; background-color: antiquewhite; margin: 0 auto; } .contral { position: absolute; top: 50%; width: 24px; height: 40px; line-height: 40px; color: #fff; text-align: center; font-size: 14px; background-color: rgba(0, 0, 0, .2); cursor: pointer; } .prev { left: 0; } .next { right: 0; } .btn { position: absolute; bottom: 20px; left: 30px; width: 150px; height: 15px; } .cut>ul>li { float: left; width: 10px; height: 10px; margin-right: 4px; border: 1px solid rgba(0, 0, 0, .05); border-radius: 50%; background-color: rgba(255, 255, 255, 0.4); list-style: none; } .cut .active { position: relative; top: -1px; width: 10px; height: 10px; border:2px solid rgba(0, 0, 0, .1) ; background-color: #fff; } </style> ``` 以上代码中,`<div class="cut">`是外层容器,用于包裹轮的内容。`<img>`标签用于展示片。`<div class="contral">`是控制按钮,用于切换上一张和下一张片。`<ul class="btn">`是圆点指示器,用于显示当前展示的片。通过设置CSS样式,可以实现的布局和样式。 你可以根据需要使用JavaScript来添加事件处理程序,实现自动切换和用户手动切换等功能。例如,你可以使用`setInterval`函数来定时切换片,或者使用`addEventListener`函数来监听控制按钮的点击事件。 希望以上信息对你有帮助。如果你需要更具体的代码实现或者其他问题,请提供更多细节,我将尽力帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值