這篇文章給大家分享的是有關js如何實現自動輪換選項卡的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
效果圖:

代碼:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style>
*{padding:0;margin:0;}
ul{list-style:none;}
#content{width:300px;height:200px;margin:150px auto;border:10px solid #ccc;padding:10px;}
#top{width:300px;height:50px;background:#ccc;}
#top a{height:50px;line-height:50px;font-size:20px;text-decoration:none;color:#000;display:inline-block;padding:0 10px;}
#top a.active{background:yellow;}
#main{width:300px;height:150px;background:#f1f1f1;}
#main img{width:200px;height:150px;}
#main ul{width:100px;height:150px;display:inline-block;float:right;}
#main ul li{width:100px;height:50px;background:#f1f1f1;border-bottom:1px dotted #000;line-height:50px;text-align:center;}
#main ul li.active{background:blue;}
</style>
<script>
window.onload = function () {
var content = document.getElementById('content');
var top = document.getElementById('top');
var aA = top.getElementsByTagName('a');
var main = document.getElementById('main');
var img = main.getElementsByTagName('img')[0];
var aLi = main.getElementsByTagName('li');
var arr = [
{
title : '動漫',
subtitle : ['波波鳥','白魔女','小龍女'],
pics : ['img/1.png','img/2.png','img/3.png'],
},
{
title : '購物',
subtitle : ['頭盔','雪橇','內衣'],
pics : ['img/4.png','img/5.png','img/6.png'],
}
];
var row = 0, col = 0;
var timer = null;
for ( var i = 0; i < arr.length; i++ ) {
aA[i].innerHTML = arr[i].title;
aA[i].index = i;
aA[i].onmouseover = function () {
tab(this.index);
}
}
tab(0);
content.onmouseover = function () {
for ( var i = 0; i < aA.length; i++ ) {
if (aA[i].className === 'active') {
row = i;
break;
}
}
for ( var i = 0; i < aLi.length; i++ ) {
if (aLi[i].className === 'active'){
col = i;
break;
}
}
clearInterval(timer);
}
content.onmouseout = autoPlay;
// 自動播放
function autoPlay() {
clearInterval(timer);
timer = setInterval(function () {
// 子標題++,逢子標題長度,
// 并且主標題加1,
// 當子標題和主標題當前inded=長度時,歸0
col++;
if(col === aLi.length) row++;
row %= aA.length;
col %= aLi.length;
for ( var i = 0; i < aLi.length; i ++ ) {
aLi[i].className = '';
}
aLi[col].className = 'active';
img.src = arr[row].pics[col];
for ( var i = 0; i < aLi.length; i++ ) {
aLi[i].innerHTML = arr[row].subtitle[i];
}
for ( var i = 0; i < aA.length; i ++ ) {
aA[i].className = '';
}
aA[row].className = 'active';
}, 2000);
}
autoPlay();
function tab(index) {
for ( var i = 0; i < aA.length; i++ ){
aA[i].className = '';
}
aA[index].className = 'active';
for ( var j = 0; j < arr[index].subtitle.length; j++ ) {
aLi[j].innerHTML = arr[index].subtitle[j];
aLi[j].index = j;
aLi[j].onmouseover = function () {
for ( var i = 0; i < aLi.length; i ++ ) {
aLi[i].className = '';
}
this.className = 'active';
img.src = arr[index].pics[this.index];
}
}
img.src = arr[index].pics[0];
for ( var i = 0; i < aLi.length; i++ ){
aLi[i].className = '';
}
aLi[0].className = 'active';
}
}
</script>
</head>
<body>
<div id="content">
<div id="top">
<a href="javascript:;"></a>
<a href="javascript:;"></a>
</div>
<div id="main">
<img/>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</body>
</html>感謝各位的閱讀!關于“js如何實現自動輪換選項卡”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。