本文实例为大家分享了js选项卡切换效果的具体代码,供大家参考,具体内容如下
实现思路:
1、首先获取id元素。
2、for循环历遍按钮元素添加onclick事件。
3、排他思想,点击按钮时设置所有选项卡按钮样式为空,利用this关键字指定当前选项卡获得高亮样式。
4、下面的多个div内容块以此类推。
源代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简单的选项卡切换(仿Hao123导航)</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 278px;
margin: 0 auto;
margin-top: 100px;
background-color: #F7F7F8;
overflow: hidden;
}
.btn button {
outline:none;
color: #616161;
font:14px/100% arial,"Hiragino Sans GB","Hiragino Sans GB W3",\5b8b\4f53;
border: none;
height: 34px;
width: 51px;
background-color: #F7F7F8;
float: left;
cursor: pointer;
}
.box .btn i {
height: 16px;
border-left: 1px solid #EAEAEA;
margin-top: 9px;
float: left;
_font-size: 0px;
}
.box .btn button:hover {
color: #0AA770;
}
.box .btn s {
cursor: pointer;
text-decoration: none;
font:14px/34px arial,"Hiragino Sans GB","Hiragino Sans GB W3",\5b8b\4f53;
}
#box1 #btns .clickbtn {
border-top: 1px solid #0AA770;
color: #0AA770;
}
.bottom {
display: none;
position: absolute;
width: 278px;
height: 110px;
color: #fff;
text-align: center;
font:14px/100% arial,"Hiragino Sans GB","Hiragino Sans GB W3",\5b8b\4f53;
}
.bottom a {
color: #fff;
position: relative;
top: -20px;
left: 0px;
text-decoration: none;
}
.bottom a:hover {
text-decoration: underline;
}
</style>
<script>
window.onload = function(){
var btns = document.getElementById("btns").getElementsByTagName("button");
var divs = document.getElementById("bottomdivs").getElementsByTagName("div");
btns[0].className = "clickbtn";
for(var i = 0;i<btns.length;i++){
btns[i].index = i;
btns[i].onclick = function(){
//alert(this.index);
for(var j = 0;j<btns.length;j++){
btns[j].className = "";
}
this.className = "clickbtn";
for(var b = 0;b<btns.length;b++){
divs[b].style.display = "none";
}
divs[this.index].style.display = "block";
}
}
}
</script>
<body>
<div class="box" id="box1">
<div class="btn" id="btns">
<button>推介</button>
<i></i>
<button>社会</button>
<i></i>
<button>娱乐</button>
<i></i>
<button>军事</button>
<i></i>
<button>体育</button>
<s>+</s>
</div>
<div id="bottomdivs">
<div class="bottom" style="display: block">
<img src="/UploadFiles/2021-04-02/01.jpg">
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“简单实现js选项卡切换效果”评论...
更新日志
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]