HTML CSS +js 实现tab选项卡,自动轮换

26 篇文章 0 订阅
21 篇文章 0 订阅
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Tab-选项切换</title>
	<style>
		*{  margin: 0;
			padding: 0;
		}
		.container{
			width: 500px;
			height: 180px;
			margin: 20px auto;
			border: 1px solid red;
		}
		.header{
			width: 500px;
			height: 40px;
			/*border: 1px solid red;*/
			box-sizing: border-box;
			background-color: #fff;
		}
		.header ul{
			display: flex;
			justify-content: space-between;
		}
		.header ul li{
			padding: 0;
			list-style: none;
			width: 100px;
			height: 39px;
			background-color: gray;
			text-align: center;
		}
		.header ul li:hover{
			border-bottom: 1px solid white;
		}
		.header ul li a{
			text-decoration:none;
			color:black;
			line-height: 40px;
			font-weight: bold;
		}
		.content{
			width: 500px;
			height: 140px;
			overflow: hidden;
		}
		.con{
			width: 500px;
			height: 140px;
		}
		.con ul li{
			list-style: none;
		}
		.con ul li p{
			font-size: 35px;
			float: left;
			padding: 10px 30px 10px;
		}
		.header ul li.select{
			background-color: white;
		}

	</style>
</head>
<body>
	<div class="container">
		<div class="header" id="header">
			<ul>
				<li class="select"><a href="#">呵呵</a></li>
				<li><a href="#">嘻嘻</a></li>
				<li><a href="#">哈哈</a></li>
				<li><a href="#">嘘嘘</a></li>
				<li><a href="#">咻咻</a></li>
			</ul>
		</div>
		<div class="content" id="content" style="display: block">
			<div class="con">
				<ul>
					<li><p>离离原上草</p></li>
					<li><p>一岁一枯荣</p></li>
					<li><p>野火烧不尽</p></li>
					<li><p>春风吹又生</p></li>
				</ul>
			</div>
			<div class="con" style="display: none">
				<ul>
					<li><p>小时不识月</p></li>
					<li><p>呼作白玉盘</p></li>
					<li><p>野火烧不尽</p></li>
					<li><p>春风吹又生</p></li>
				</ul>
			</div>
			<div class="con" style="display: none">
				<ul>
					<li><p>青青子衿</p></li>
					<li><p>悠悠我心</p></li>
					<li><p>野火烧不尽</p></li>
					<li><p>春风吹又生</p></li>
				</ul>
			</div>
			<div class="con" style="display: none">
				<ul>
					<li><p>山不在高</p></li>
					<li><p>有仙则灵</p></li>
					<li><p>野火烧不尽</p></li>
					<li><p>春风吹又生</p></li>
				</ul>
			</div>
			<div class="con" style="display: none">
				<ul>
					<li><p>野火烧不尽</p></li>
					<li><p>野火烧不尽</p></li>
					<li><p>野火烧不尽</p></li>
					<li><p>野火烧不尽</p></li>
				</ul>
			</div>
		</div>
	</div>
	<script src='./jquery/jquery-3.2.1.min.js'></script>

	<script type="text/javascript">
		function finder(id){
			return typeof id==='string'?document.getElementById(id):id;
		}
		window.οnlοad=function(){
			var lis=finder('header').getElementsByTagName('li');
			var divs=finder('content').getElementsByTagName('div');
			for(var i=0; i<lis.length; i++){
				lis[i].id = i;
				lis[i].οnmοuseοver=function(){
					for(var j=0;j<lis.length;j++){
						lis[j].className = '';
						divs[j].style.display = 'none';
					}
					this.className = 'select';
					divs[this.id].style.display = 'block';

				}
			}

		
		    var i = 0;
			var timer = null;
			function setMyTimer(){
				timer = setInterval(function(){
					if(i >= lis.length){
						i = 0;
					}
					for(var j=0;j<lis.length;j++){
						lis[j].className = '';
						divs[j].style.display = 'none';
					}
					lis[i].className = 'select';
					divs[i].style.display = 'block';
					i++;
				},2000)
			}

			$("lis").on("mouseover",function(){
				timer && clearInterval(timer);
				timer = null;
			})
			$("lis").on("mouseout",function(){
				(!timer) && setMyTimer();
			})
			setMyTimer();
		}

	</script>
</body>
</html>
主要思路为:选项栏五个选项按钮,对应着下面的五个div,控制按钮索引的切换,从而切换不同的div,其他的隐藏。自动
切换即让索引值自动增加,从而使div跟随着变化。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值