js实现轮播图(幻灯片)思路原生+es6

网页中经常可以看到的轮播图效果点击小按钮切换非JQuery中fadeIn效果

图片链接

https://s3.ax1x.com/2020/12/06/DXl03Q.jpg
https://s3.ax1x.com/2020/12/06/DXlw9g.jpg
https://s3.ax1x.com/2020/12/06/DXla4S.jpg

实现思路

轮播图示例

布局思路

①需要两个盒子,父盒子中有一个轮播图盒子,父盒子的尺寸和每一张轮播图的尺寸相同,轮播图盒子的尺寸,高度固定,宽度为百分比,x张图片宽度为x百%,如图,3张图片所以宽度为300%

②给每张图片左浮动使其并列,这样父盒子因为设置溢出隐藏,则只显示一张轮播图

③设置轮播图盒子绝对定位,我们通过js控制轮播图盒子在父盒子的left定位值来实现切换效果

④图下的小圆点,用3个div小尺寸,设置圆角边框50%和背景颜色,并给每个按钮设置点击事件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			li{
				list-style: none;
			}
			*{padding: 0;margin: 0;}
			.box{
				width: 1200px;height: 300px;
				margin: 50px auto;
				overflow: hidden;
				position: relative;
			}
			.box .h-img{
				height: 330px;width: 300%;
				position: absolute;left: 0;
				transition: left 1s;
			}
			.h-img a{
				display: block;width: 1200px;height: 300px;
			}
			
			.fl{
				float: left;
			}
			#btn{width: 100px;height: 25px; position: absolute;bottom:10px; left:50%;margin-left: -50px; z-index: 9;}
			#btn li{
				width: 20px;height: 20px;border-radius: 50%;float: left;background-color: #cccccc;
				margin-left: 10px;
			}
			
		</style>
	</head>
	<body>
		<div class="box">
			
				<ul id="btn">
					<li></li>
					<li></li>
					<li></li>
				</ul>
			
			<div class="h-img fl" id="box">
				<a class="fl"  href="#"><img src="https://s3.ax1x.com/2020/12/06/DXl03Q.jpg" ></a>
				<a class="fl"  href="#"><img src="https://s3.ax1x.com/2020/12/06/DXlw9g.jpg" ></a>
				<a class="fl"  href="#"><img src="https://s3.ax1x.com/2020/12/06/DXla4S.jpg" ></a>
			</div>
		</div>
		<script type="text/javascript">
			var a =document.getElementById("box");
			var b =document.getElementById("btn").getElementsByTagName("li");
	// 前期思路
			//let c=[1,2,3];
			// b[0].οnclick=function(){
			// 	a.style.left="0%";
			// }
			// b[1].οnclick=function(){
			// 	a.style.left="-100%";
			// }
			// b[2].οnclick=function(){
			// 	a.style.left="-200%";
			// 	alert(typeof c)
	//es6语法
			// [...b].forEach(
			// (item,index)=>{
			// 	item.onclick = ()=>{
			// 		a.style.left=index * -100 +'%';
			// 	}
			// }
			
			// )
	// js语法
			[...b].forEach(function(item,index){
				item.onclick=function(){
					a.style.left=index * -100 +"%"
				}
			});
			
		</script>
	</body>
</html>

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值