js實(shí)現(xiàn)tab欄切換效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)tab欄切換效果的具體代碼,供大家參考,具體內(nèi)容如下
效果展示:

源碼展示:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js實(shí)現(xiàn)tab欄切換</title>
<style>
* {
margin: 0;
margin: 0;
padding: 0;
list-style: none;
}
.nav {
width: 100%;
height: 50px;
}
.nav ul {
width: 600px;
height: 50px;
margin: 0 auto;
}
.nav ul li {
width: 120px;
height: 50px;
font-weight: 800;
font-size: 18px;
color: #515151;
line-height: 50px;
text-align: center;
float: left;
cursor: pointer;
}
.tiao {
width: 600px;
height: 5px;
background-color: #515151;
margin: 0 auto;
position: relative;
top: 0;
left: 0;
}
.zhou {
width: 120px;
height: 5px;
background-color: red;
position: absolute;
top: 0;
left: 0;
}
.ww {
width: 0px;
border-width: 8px;
border-style: solid;
border-color: rgba(250, 0, 255, 0) rgba(250, 0, 255, 0) red rgba(250, 0, 255, 0);
position: absolute;
top: -16px;
left: 56px;
}
.nei {
width: 600px;
height: 300px;
margin: 0 auto;
}
.nei li {
width: 600px;
height: 300px;
color: #fff;
font-family: "微軟雅黑";
font-size: 40px;
text-align: center;
line-height: 300px;
display: none;
margin-top: 10px;
}
</style>
</head>
<body>
<div class="nav">
<ul>
<li onmouseover="don(0)">大娃</li>
<li onmouseover="don(1)">二娃</li>
<li onmouseover="don(2)">三娃</li>
<li onmouseover="don(3)">四娃</li>
<li onmouseover="don(4)">五娃</li>
</ul>
</div>
<div class="tiao">
<div id="zhou" class="zhou" style="left: 0;">
<div class="ww"></div>
</div>
</div>
<div id="nei" class="nei">
<ul>
<li style="background-color:#e4007f; display: block;">大娃出世</li>
<li style="background-color:#687de8">二娃出世</li>
<li style="background-color:#2fb936">三娃出世</li>
<li style="background-color:#4dd5d0">四娃出世</li>
<li style="background-color:#e24759">五娃出世</li>
</ul>
</div>
<script>
var k;
var kk = 0;
function don(gh) {
if (kk == 0) {
kk = 1
var w1 = document.getElementById('zhou')
var t = parseInt(w1.style.left)
if (t < gh * 120) {
k = window.setInterval(
function () {
goright(gh * 120)
}, 30
)
} else if (t > gh * 120) {
k = window.setInterval(
function () {
goleft(gh * 120)
}, 30
)
} else {
kk = 0
}
var w2 = document.getElementById('nei').getElementsByTagName('li')
for (var i = 0; i < w2.length; i++) {
w2[i].style.display = "none"
}
w2[gh].style.display = "block"
}
}
//右移動(dòng)
function goright(gh1) {
var w1 = document.getElementById('zhou')
var t = parseInt(w1.style.left)
t += 20
if (t >= gh1) {
t = gh1
window.clearInterval(k)
kk = 0
}
w1.style.left = t + "px"
}
//左移動(dòng)
function goleft(gh1) {
var w1 = document.getElementById('zhou')
var t = parseInt(w1.style.left)
t -= 20
//alert(t)
if (t <= gh1) {
t = gh1
window.clearInterval(k)
kk = 0
}
w1.style.left = t + "px"
}
</script>
<hr>
<pre style="color:red">
感: 最近貢獻(xiàn)一下我在教學(xué)中的小案例可以能給你一些幫助 ,希望繼續(xù)關(guān)注我的博客
--王
</pre>
</body>
</html>
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊兩個(gè)精彩的專題:javascript選項(xiàng)卡操作方法匯總 jquery選項(xiàng)卡操作方法匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS+CSS實(shí)現(xiàn)簡(jiǎn)易的滑動(dòng)門效果代碼
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)簡(jiǎn)易的滑動(dòng)門效果代碼,涉及JavaScript動(dòng)態(tài)遍歷及修改頁(yè)面元素屬性的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
node.js+express+mySQL+ejs+bootstrop實(shí)現(xiàn)網(wǎng)站登錄注冊(cè)功能
這篇文章主要介紹了node.js+express+mySQL+ejs+bootstrop實(shí)現(xiàn)網(wǎng)站登錄注冊(cè)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
微信小程序?qū)崿F(xiàn)簡(jiǎn)易的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)易的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
JavaScript+Canvas實(shí)現(xiàn)帶跳動(dòng)效果的粒子動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)JavaScript和Canvas實(shí)現(xiàn)帶跳動(dòng)效果的粒子動(dòng)畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-03-03
javascript 面向?qū)ο髮?shí)戰(zhàn)思想分享
萬(wàn)物皆對(duì)象,所以,任何事物都是有特征(屬性)和動(dòng)作(方法)的,一般拿到一份需求分檔,或者你瀏覽一個(gè)網(wǎng)頁(yè)看到一個(gè)畫面的時(shí)候,腦子里就要有提煉出來(lái)的屬性和方法的能力,那你才是合格的2017-09-09
JS嵌套函數(shù)調(diào)用上下文的問(wèn)題解決
這篇文章主要探討了JS嵌套函數(shù)調(diào)用上下文的問(wèn)題,需要的朋友可以參考下2014-03-03

