如何用js实现,实现选项卡切换的效果

如题所述

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tab标签切换效果</title>
<style>
*{ margin:0; padding:0;list-style: none;}
body {font:12px/1.5 Tahoma;}
#outer {width:450px;margin:150px auto;}
#tab {overflow:hidden;zoom:1;background:#000;border:1px solid #000;}
#tab li {float:left;color:#fff;height:30px; cursor:pointer; line-height:30px;padding:0 20px;}
#tab li.current {color:#000;background:#ccc;}
#content {border:1px solid #000;border-top-width:0;}
#content ul {line-height:25px;display:none; margin:0 30px;padding:10px 0;}
</style>
</head>

<body>
<!-- html代码begin -->
<div id="outer">
<ul id="tab">
<li class="current">tab标签</li>
<li>qq在线客服代码</li>
<li>css3</li>
</ul>
<div id="content">
<ul style="display:block;">
<a href="">tab标签</a>
</ul>
<ul>
<a href="">服代码</a>
</ul>
<ul>
<a href="">css</a>
</ul>
</div>
</div>
<!-- html代码end -->

<script src="jquery.min.js"></script>
<script>
$(function(){
window.onload = function()
{
var $li = $('#tab li');
var $ul = $('#content ul');

$li.mouseover(function(){
var $this = $(this);
var $t = $this.index();
$li.removeClass();
$this.addClass('current');
$ul.css('display','none');
$ul.eq($t).css('display','block');
})
}
});
</script>

</body>
</html>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-04-18

活动作品如何在一个网页实现登录_注册表单切换?14分钟带你使用CSS+JS实现炫酷滑动切换效果

第2个回答  2021-11-02
思路:
选项卡就是点击按钮切换到相应内容,其实就是点击按钮把内容通过display(block none)来实现切换的。
1、首先获取元素。
2、for循环历遍按钮元素添加onclick 或者 onmousemove事件。
3、因为点击当前按钮时会以高亮状态显示,所以要再通过for循环历遍把所有的按钮样式设置为空和把所有DIV的display设置为none。
4、把当前按钮添加样式,把当前DIV显示出来,display设置为block。
注:给多个元素添加多个事件要用for循环历遍。如选项卡是点击切换,当前按钮高度,点击和按钮高亮就是2个事件,所以要用2个for循环历遍。