纯js 轮播的封装
纯js 轮播的封装
静水流深01 发表于9个月前
纯js 轮播的封装
  • 发表于 9个月前
  • 阅读 56
  • 收藏 1
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			body,
			ul,
			li {
				padding: 0;
				margin: 0;
			}
			
			.carouselBox {
				width: 512px;
				height: 384px;
				border: 3px solid black;
				margin: 0 auto;
				position: relative;
			}
			
			.carouselBox ul {
				position: absolute;
				left: 50%;
				transform: translateX(-50%);
				bottom: 10px;
				z-index: 2;
				overflow: hidden;
			}
			
			.carouselBox ul li {
				list-style: none;
				cursor: pointer;
				-moz-user-select: none;
				user-select: none;
				width: 18px;
				height: 18px;
				font-size: 14px;
				line-height: 18px;
				text-align: center;
				background: #ccc;
				float: left;
				margin: 2px;
			}
			
			.carouselBox ul li.selected {
				background: orange;
			}
			
			.carouselBox ul li.normal {
				background: #ccc;
			}
			
			.carouselBox button {
				position: absolute;
				top: 50%;
				transform: translateY(-50%);
				font-size: 40px;
				font-weight: 200;
				opacity: 0;
			}
			
			.carouselBox button.show {
				opacity: .7;
			}
			
			.carouselBox .btnL {
				left: 10px;
			}
			
			.carouselBox .btnR {
				right: 10px;
			}
		</style>
	</head>

	<body>
		<div class="carouselBox" id="carouselBox">
			<img class="carouselImg" src="images/1.jpg" alt="" />
			<ul>
				<li class="selected">1</li>
				<li>2</li>
				<li>3</li>
				<li>4</li>
				<li>5</li>
				<li>6</li>
			</ul>
			<button class="btnL">&lt;</button>
			<button class="btnR">&gt;</button>
		</div>
		<script type="text/javascript">
			//获取轮播盒字元素节点
			var carouselBox = document.getElementById("carouselBox");

			//调用轮播
			carousel(carouselBox);
			//封装轮播
			function carousel(carouselBox) {

				var carouselBox = carouselBox;
				//获取轮播图片节点
				var carouselImg = carouselBox.getElementsByClassName("carouselImg")[0];
				//获取到所以轮播按钮节点
				var lis = carouselBox.getElementsByTagName("li");
				var len = lis.length;
				//左点击按钮
				var btnL = carouselBox.getElementsByClassName("btnL")[0];
				//右点击按钮
				var btnR = carouselBox.getElementsByClassName("btnR")[0];
				//定义默认图片
				var autoImg = 1;
				//定时器
				var time = null;
				//当前li下标值
				var currIndex = null;
				//调用自动轮播
				autoCarousel();
				//调用点击轮播
				clickCarousel();

				//自动轮播
				function autoCarousel() {
					time = setInterval(autoChange, 1000);
				}
				//自动切换函数
				function autoChange() {
					//如果是最后一张变成第一张
					if(autoImg == 6) {
						autoImg = 1;
					} else {
						autoImg++;
					}
					carouselImg.src = "images/" + autoImg + ".jpg";

					//对应得按钮背景色改变
					bgChange(autoImg - 1);
				}

				//背景色改变函数
				function bgChange(t) {

					for(var i = 0; i < len; i++) {
						if(i === t) {
							//如果是和当前图片对应得下标的li则改变背景 否则变为正常的
							lis[i].className = "selected";
						} else {
							lis[i].className = "normal";
						}
					}
				}

				//点击轮播
				function clickCarousel() {
					for(var i = 0; i < len; i++) {
						lis[i].onclick = function() {
							currIndex = getIndex(this);
							bgChange(currIndex);
							carouselImg.src = "images/" + (currIndex + 1) + ".jpg";
							autoImg = currIndex;
						}
					}
				}

				//获取到点击li的下标
				function getIndex(t) {
					//定义个标签
					var index = -1;
					for(var i = 0; i < len; i++) {
						//找到当前点击对象 并记录下标值
						if(lis[i] === t) {
							index = i;
							break;
						}
					}
					return index;
				}
				//鼠标移入的时候清楚定时器
				carouselBox.onmouseenter = function() {
					clearTimeout(time);
					btnL.className = "show btnL";
					btnR.className = "show btnR";
				}
				//鼠标移出继续轮播
				carouselBox.onmouseleave = function() {
					autoCarousel();
					btnL.className = "btnL";
					btnR.className = "btnR";
				}
				//左点击按钮事件
				btnL.onclick = function() {
					currIndex = autoImg;
					if(autoImg == 1) {
						autoImg = 6;
					} else {
						autoImg--;
					}
					carouselImg.src = "images/" + autoImg + ".jpg";

					//对应得按钮背景色改变
					bgChange(autoImg - 1);
				}
				//右按钮点击事件
				btnR.onclick = function() {
					currIndex = autoImg;
					autoChange();
				}
			}
		</script>
	</body>

</html>
标签: js 轮播 封装
共有 人打赏支持
粉丝 3
博文 15
码字总数 14681
×
静水流深01
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: