网页中十分常见的Tab切换效果,你要学习一下吗...

本文介绍了网页中常见的Tab切换效果,包括手动切换和自动切换两种模式。通过提供详细的源代码,包括demo01.css、demo01.js、demo02.js、demo03.js、demo01.html、demo02.html、demo03.html,帮助读者理解和实现这一交互功能。
摘要由CSDN通过智能技术生成

效果图

手动切换

在这里插入图片描述

自动切换

在这里插入图片描述

说明

  • 文件夹结构如下图所示
    在这里插入图片描述

源代码

demo01.css
*{
   
	margin: 0;
	padding: 0;
}
ul{
   
	list-style: none;
}
a{
   
	text-decoration: none;
	color: #000;
}
body{
   
	font-size: 12px;
}

.notice{
   
	/* 水平垂直居中新方式 */
	position: absolute;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	margin: auto;

	width: 298px;
	height: 98px;
	
	border: 1px solid #eee;
	overflow: hidden;
}
.notice-title{
   
	height: 27px;
	position: relative;
	background: #f7f7f7;
}
.notice-title ul{
   
	position: absolute;
	width: 301px;
	left: -1px;
}
.notice-title li{
   
	float: left;
	width: 58px;
	height: 26px;
	line-height: 26px;
	text-align: center;
	overflow: hidden;
	padding: 0 1px;
	background: #f7f7f7;
	border-bottom: 1px solid #eee;
}
/* 被选中的li 的样式 */
.notice-title li.select{
   
	background: #fff;
	border-bottom: 1px solid #fff;
	border-left: 1px solid #eee;
	border-right: 1px solid #eee;
	font-weight: bold;
	padding: 0;
}
.notice-title li a:hover{
   
	color: #f90;
}


.notice-con .mod{
   
	margin: 10px;
	margin-left: 19px;
}
.notice-con .mod li{
   
	float: left;
	width: 120px;
	margin-right: 10px;
	height: 25px;
	overflow: hidden;
}
.notice-con .mod li a{
   
	white-space: nowrap;
	overflow: hidden;
}
.notice-con li a:hover{
   
	color: #f90;
}

demo01.js

window.onload = function(){
   

	// 获取标题和div
	var titles = document.querySelector('.notice-title').getElementsByTagName('li');
	var divs = document.querySelector('.notice-con').getElementsByTagName('div');
	
	if(titles.length != divs.length){
   
		return;
	}

	// 遍历titles
	for(var i=0; i<titles.length; i++){
   
		// 给每个title 添加id 互相区分
		titles[i].id = i;
		
		titles[i].onmouseover = function(){
   
			for(var j=0; j<titles.length; j++){
   
				titles[j].className = '';
				divs[j].style.display = 'none';
			}
			this.className = 'select';
			divs[this.id].style.display = 'block';
		}
	}
}
demo02.js

window.onload = function(){
   

	// 获取标题和div
	var titles = document.querySelector('.notice-title').getElementsByTagName('li');
	var divs = document.querySelector('.notice-con').getElementsByTagName('div');
	// 保存定时器id
	var timer = null;
	
	if(titles.length != divs.length){
   
		return;
	}

	// 遍历titles
	for(var i=0; i<titles.length; i++){
   
		// 给每个title 添加id 互相区分
		titles[i].id = i;
		
		titles[i].onmouseover = function(){
   
			// 保存当前对象
			var index = this;
			// 清除之前打开的定时器,防止重复开启
			if(timer){
   
				clearTimeout(timer);
				timer = null;
			}
			
			// 延迟半秒进行切换
			timer = setTimeout(function(){
   
				for(var j=0; j<titles.length; j++){
   
					titles[j].className = '';
					divs[j].style.display = 'none';
				}
				index.className = 'select';
				divs[index.id].style.display = 'block';
				
				
			}, 300);
		}
	}
}
demo03.js
window.onload = function(){
   

	// 获取标题和div
	var titles = document.querySelector('.notice-title').getElementsByTagName('li');
	var divs = document.querySelector('.notice-con').getElementsByTagName('div');
	// 保存定时器id
	var timer = null;
	// 表示当前高亮的title 的索引 
	var index = 0;
	
	if(titles.length != divs.length){
   
		return;
	}
	
	for(var i=0; i<titles.length; i++
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值