【HTML+CSS+JS】粗略的图片轮播

利用CSS和JS实现粗略的图片轮播

源码

HTML部分

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<link rel="stylesheet" type="text/css" href="pictureChange.css" />
	<script src="pictureChange.js"></script>
	<body>
		<div id="container">
			<div class="control">
				<div class="picture">
					<img src="pic/img1.jpg" />
					<img src="pic/img2.jpg" />
					<img src="pic/img3.jpg" />
				</div>
			</div>
			<div class="button">
				<img src="pic/img1.jpg" />
				<img src="pic/img2.jpg" />
				<img src="pic/img3.jpg" />
			</div>
		</div>
	</body>
</html>

CSS部分

*{
	padding: 0;
	margin: 0;
}
body{
	background: #f3f3f3;
}
#container{
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%,-50%);
	width: 300px;
	height: 400px;
	border: 1px solid #55ff00;
	padding: 10px;
	background: white;
	text-align: center;
}
.control{
	width: 300px;
	height: 300px;
	overflow: hidden;
}
.picture{
	width: 1000px;
	height: 300px;
	margin-left: 0;
	animation: switch 8s ease-out infinite;
	transition-duration: 1s;
}
.picture img{
	float: left;
	width: 300px;
	height: 300px;
	max-width: 100%;
	max-height: 100%;
}
@keyframes switch{
	0%,25%{
		margin-left: 0;
	}
	35%,60%{
		margin-left: -300px;
	}
	70%,95%{
		margin-left: -600px;
	}
	100%{
		margin-left: 0;
	}
}
.button{
	padding-top: 6px;
	width: 300px;
	height: 95px;
	text-align: center;
}
.button img{
	display: inline-block;
	cursor: pointer;
	width: 95px;
	height: 95px;
	max-width: 100%;
	max-height: 100%;
	border: 0.5px solid white;
	opacity: 0.5;
}

JS部分

window.onload = function(){
	var button = document.getElementsByClassName("button");
	var btn = button[0].getElementsByTagName("img");
	var picture = document.getElementsByClassName("picture");
	
	function reAnimation(){
		for(let j = 0;j<btn.length;j++){
			btn[j].style.borderColor="white";
			btn[j].style.opacity="0.5";
		}
		picture[0].style.animation = "switch 8s ease-out infinite";
	}
	
	
	for (let i = 0;i<btn.length;i++){
		btn[i].onclick = function(){
			for(let j = 0;j<btn.length;j++){
				btn[j].style.borderColor="white";
				btn[j].style.opacity="0.5";
			}
			
			btn[i].index = i;
			picture[0].style.animation = "none";
			picture[0].style.marginLeft = -i*300+"px";
			btn[i].style.borderColor = "#000000";
			btn[i].style.opacity = "1";
			setTimeout(reAnimation,5000);
			
		}
		
	}
	
}

效果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值