JavaScript特效:实现轮播图

本文介绍了一种使用HTML, CSS和JavaScript实现的轮播图效果,支持自动播放和手动按钮切换。通过克隆列表项并应用CSS transform实现平滑过渡,确保了良好的用户体验。

实现效果

html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" href="../css/marquee.css" />
	</head>
	<body>
		<br />
		<br />
		<br />
		<div class="marquee">
			<div class="box">
				<div class="list"><img src="../img/11.PNG" alt="" /></div>
				<div class="list"><img src="../img/22.PNG" alt="" /></div>
				<div class="list"><img src="../img/33.PNG" alt="" /></div>
			</div>
		
		</div>
		<button id="preBtn">pre</button>
		<button id="nextBtn">next</button>
		<br />
	
	</body>
	<script type="text/javascript" src="../js/jquery-1.9.1.min.js" ></script>
	<script>
		$(()=>{
			var outerWidth=450;
			var outerHeight=450;
			var speed=3;
			
			var transitionTime=0.3;
			var initIndex=0;
			var setTimeId=null;
			
			var $marquee=$('.marquee');
			var $box=$marquee.find('.box');
			var $list=$box.find('.list');
			
			
			var listLength=$list.length;
			var contentWidth=outerWidth*listLength;
			
			var style=document.styleSheets;
			//Document.styleSheets 只读属性,返回一个由 StyleSheet 对象组成的 StyleSheetList,每个 StyleSheet 对象都是一个文档中链接或嵌入的样式表。
			
			$marquee.css({
				"width":`${outerWidth}px`,
				"height":`${outerHeight}px`
			});
			$list.css({
				"width":`${outerWidth}px`,
				"height":`${outerHeight}px`
			});
			$box.css({
				"width":`${contentWidth}px`,
				"height":`${outerHeight}px`
			});
			var clone=$list.eq(0).clone();
			$box.append(clone);//追加
			
			function autoplay(){
					
					setTimeId=setInterval(()=>{
						initIndex++;
						$box.css({
							"transition":`transform ${transitionTime}s ease `,
							"transform":`translateX(-${outerWidth*initIndex}px)`
						})
						initIndex===listLength?initIndex=0:'';
					},speed*1000)
			}
			//autoplay();
			
			$box[0].addEventListener('transitionend',()=>{
				$box.css({
					"transition":`none`,
					"transform":`translateX(-${outerWidth*initIndex}px)`
				})
			})
			$('#preBtn').mouseenter(()=>{
				if(initIndex===0){
					initIndex=listLength;
					$box.css({
						"transition":`none`,
						"transform":`translateX(-${outerWidth*initIndex}px)`
					})
				}
			})
			$('#preBtn').click(()=>{

				initIndex--;
				$box.css({
					"transition":`transform ${transitionTime}s ease `,
					"transform":`translateX(-${outerWidth*initIndex}px)`
					})
				initIndex===0?initIndex=listLength:'';			
						
			})
			
			$('#nextBtn').click(()=>{
				initIndex++;
				$box.css({
					"transition":`transform ${transitionTime}s ease `,
					"transform":`translateX(-${outerWidth*initIndex}px)`
					})
					initIndex===listLength?initIndex=0:'';										
			})
			//
		})
	</script>
</html>

css

@charset "utf-8";

*{margin: 0;padding: ;}
body{
	width: 100%;
	height: 100%;
}
.marquee{
	overflow: hidden;
	position: absolute;
	left: 20%;
	border: 2px solid black;
}
.box{
	
	display: flex;
}
.list{
	overflow: hidden;
	flex-shrink:0 ;
	img{
		height: 100%;
		width: 100%;
	}
	}
	
#nextBtn{
	position: absolute;
	left: 30%;
	bottom:30%;
	width: 60px;
	background: #000000;
	color: white;
	height: 40px;
	line-height: 30px;
	text-align: center;		
}
#preBtn{
	position: absolute;
	left: 20%;
	bottom:30%;
	background: #000000;
	color: white;
	width: 60px;
	height: 40px;
	line-height: 30px;
	text-align: center;	
}

源码下载地址: https://github.com/YanHSana/lunbotu

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值