您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)js如何實(shí)現(xiàn)自動輪換選項(xiàng)卡的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
效果圖:
代碼:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標(biāo)題文檔</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 : ['頭盔','雪橇','內(nèi)衣'], 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 () { // 子標(biāo)題++,逢子標(biāo)題長度, // 并且主標(biāo)題加1, // 當(dāng)子標(biāo)題和主標(biāo)題當(dāng)前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>
感謝各位的閱讀!關(guān)于“js如何實(shí)現(xiàn)自動輪換選項(xiàng)卡”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。