qq_一梦两三年丶_0
2017-01-19 17:11
index怎么在不优化代码情况下传值
function $(id) {
return typeof id === "string" ? document.getElementById(id) : id;
}
window.onload = function() {
var index = 0;
var timer = null;
var tits = $("tabTit").getElementsByTagName("li");
var txts = $("tabTxt").getElementsByClassName("demo");
if(tits.length != txts.length) {return;}
for(var i=0,l=tits.length; i<l; i++) {
tits[i].id = i;
tits[i].onmouseover = function() {
clearInterval(timer);
styleFun(this.id);
}
tits[i].onmouseout = function() {
timer = setInterval(autoPlay, 2000);
}
}
//在开启定时器的同时清楚定时器并置空
if(timer) {
clearInterval(timer);
timer = null;
}
timer = setInterval(autoPlay, 2000);
function autoPlay() {
index++;
if(index >= tits.length) {
index = 0;
}
styleFun(index);
}
function styleFun(ele) {
for(var j=0,m=tits.length; j<m; j++) {
tits[j].className = "";
txts[j].style.display = "none";
}
tits[ele].className = "select";
txts[ele].style.display = "block";
//将鼠标移入移出时的index传给autoPlay;
index = ele;
}
}还是传值吧,这样是比较优化的,用的次数多了,你就知道老师的方法是非常不错的!
Tab选项卡切换效果
65460 学习 · 581 问题
相似问题