怎么加个开关。。控制重复执行,弄了好几天要崩溃了。
就是运动后 DIV2 跟span复位,再次点击后又重新运动了
window.onload = function(){
var oDiv = document.getElementById('div1');
var oDiv2 = document.getElementById('div2');
var aSpan = oDiv.getElementsByTagName('span');
var len = 10;
var num =0;
var time = null;
for(var i=0;i<len;i++){
oDiv.innerHTML += '<span style="left:'+i*40+'px">+1</span>'
}
oDiv.onclick=function(){ //点击DIV1,DIV2回变宽透明,span 会向上透明,我想让他运动完后立马返回初始位置,再次点击又重新动了。
clearInterval(time)
time =setInterval(function(){
startMove(oDiv2,{width:400,opacity:100},function(){
startMove(oDiv2,{opacity:0})
})
startMove(aSpan[num],{bottom:100,opacity:0})
num++
if(num==len){
clearInterval(time)
}
},100)
}
function startMove(obj, json, fnEnd){
clearInterval(obj.timer);
obj.timer=setInterval(function (){
var bStop=true; //假设:所有值都已经到了
for(var attr in json){
var cur=0;
if(attr=='opacity'){
cur=Math.round(parseFloat(getStyle(obj, attr))*100);
}else{
cur=parseInt(getStyle(obj, attr));
}
var speed=(json[attr]-cur)/6;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
if(cur!=json[attr])
bStop=false;
if(attr=='opacity'){
obj.style.filter='alpha(opacity:'+(cur+speed)+')';
obj.style.opacity=(cur+speed)/100;
}else{
obj.style[attr]=cur+speed+'px';
}
}
if(bStop){
clearInterval(obj.timer);
if(fnEnd)fnEnd();
}
}, 30);
}
重复执行?
楼主的意思是点击过后不断重复某个“效果”是不?
那就可以给这个div绑定一个事件,而这个事件的处理程序就是完成那个要不断重复的效果………
var timer=null;
div.onclick=function(){
clearInterval(timer);
timer=setInterval(function(){
//这里写要重复执行的代码
},30)
}