原生js实现tab选项卡

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		* {
			margin: 0;
			padding: 0;
			list-style: none;
			text-decoration: none;
			color: #000;
			font-size: 12px;
		}
		.news {
			width: 190px;
		    height: 154px;
		    border-bottom: 1px solid #ccc;
		    background-color: #fff;
		    padding: 7px 13px 0;
		    box-sizing: border-box;
		}
		.news_t {
		    height: 19px;
		    border-bottom: 1px solid #ccc;
		    position: relative;
		    text-align: center;
		}
		.news_t a {
		    float: left;
		    height: 17px;
		    line-height: 17px;
		    width: 38px;
   			border-right: 1px solid #ccc;
		}
		.news_t div {
		    height: 2px;
		    width: 28px;
		    background-color: #f10215;
		    position: absolute;
		    bottom: -1px;
		    left: 4px;
		}
		.hide {
			display: none;
		}
		.show {
			display: block;
		}
		.newContainer {
			padding-top: 10px;
		}
		.news_b {
		    padding-top: 15px;
		}
	</style>
</head>
<body>
	<div class="news">
		<div id="newsT" class="news_t">
			<a href="#">促销</a>
			<a href="#">公告</a>
			<div id="flag"></div>
		</div>
		<!-- 详细内容 -->
		<div id="newContainer">
			<div class="news_b">
				<li><a href="#">6折促销</a></li>
				<li><a href="#">热卖商品</a></li>
			</div>
			<div class="news_b hide">
				<li><a href="#">5折公告</a></li>
				<li><a href="#">今日公告</a></li>
			</div>
		</div>
	</div>

	<script src="js/common.js"></script>
	<script src="js/animate.js"></script>
	<script>
		// 获取元素
		var newT = my$('newsT');
		// 红色的线
		var flag = my$('flag');
		// 详细内容
		var newContainer = my$('newContainer');

		//1 获取到a标签注册事件
		for (var i = 0; i < 2; i++) {
			var link = newT.children[i];
			link.onmouseenter = linkMouseenter;
			// 设置a标签的自定义属性,记录索引(字符串类型)
			link.setAttribute('index', i);
		}

		function linkMouseenter() {
			// 让红色的线执行动画
  			// 获取当前触发事件的a标签的位置
  			var offsetLeft = this.offsetLeft;
  			animation(flag, offsetLeft + 4);

  			// 显示对应的详细内容
  			// 让所有的详细内容隐藏
  			for (var i = 0, len = newContainer.children.length; i < len; i++) { // 优化代码
  				var div = newContainer.children[i];
  				// 判断类样式中是否已经有hide
  				if (div.className.indexOf('hide') === -1) {
  					div.className = 'news_b hide';
  				}
  			}
  			// 当前对应的详细内容显示
  			var index = parseInt(this.getAttribute('index'));
  			newContainer.children[index].className = 'news_b show';
		}
	</script>
</body>
</html>

common.js:

// 通过id获取元素
function my$(element) {
	var el = document.getElementById(element);
	return el;
}

animate.js:

// 动画封装
// var timerId = null; // 需全局变量
function animation(element, target) {
	// 给每个element增加一个属性timerId,保证定时器标志互不干扰
	if (element.timerId) {
		clearInterval(element.timerId);
		element.timerId = null;
	}
	element.timerId = setInterval(function () {
		// 步距
		var step = 10;
		var current = element.offsetLeft;

		// 如果当前位置 > 目标位置 step < 0
		if (current > target) {
			step = -Math.abs(step);
		}

		// 当前位置和目标位置的差值小于step
		// console.log(Math.abs(current - target));
		// console.log(Math.abs(step));
		if (Math.abs(current - target) <= Math.abs(step)) { 
			// 停止定时器
			clearInterval(element.timerId);
			element.style.left = target + 'px';  // 赋值为目标值
			// 退出函数
			return;
		};
		// 移动盒子
		current += step;
		element.style.left = current + 'px';
	}, 30);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的例子: HTML: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h3>Tab 1 Content</h3> <p>Some text...</p> </div> <div id="tab2" class="tabcontent"> <h3>Tab 2 Content</h3> <p>Some text...</p> </div> <div id="tab3" class="tabcontent"> <h3>Tab 3 Content</h3> <p>Some text...</p> </div> ``` CSS: ```css /* 隐藏所有tab内容 */ .tabcontent { display: none; } /* 标签页按钮样式 */ .tab button { background-color: #eee; border: none; color: black; padding: 10px 20px; cursor: pointer; } /* 激活的标签页按钮样式 */ .tab button.active { background-color: #ccc; } /* 标签页内容样式 */ .tabcontent { padding: 20px; border: 1px solid #ccc; } /* 第一个标签页默认显示 */ #tab1 { display: block; } ``` JavaScript: ```javascript function openTab(event, tabName) { // 获取所有的 tab 内容 var tabcontent = document.getElementsByClassName("tabcontent"); // 隐藏所有的 tab 内容 for (var i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // 获取所有的 tab 按钮 var tablinks = document.getElementsByClassName("tablinks"); // 将所有的 tab 按钮样式设置为非激活状态 for (var i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // 显示当前选中的 tab 内容 document.getElementById(tabName).style.display = "block"; // 设置当前选中的 tab 按钮为激活状态 event.currentTarget.className += " active"; } ``` 当用户点击任意一个标签页按钮时,`openTab`函数会被调用,隐藏所有标签页内容,将当前标签页内容设置为显示状态,同时将当前标签页按钮样式设置为激活状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值