【发布时间】:2011-08-06 14:12:57
【问题描述】:
我正在尝试做一个shakeEffect,但我得到了这个错误:
Uncaught TypeError: Cannot read property '1' of undefined
setTimeout.elementsCollection.style.position shake.js:66
第66行是:
this.elementsCollection[ i ].style.left = parseInt( Math.random() * 20 ) + 'px';
以及完整的代码:
Shake.prototype.shakeEffect = function(){
if( this.elementsCollection.length != false ){
var positions = this.shakePosition.split( '|' );
for( var i = 0; i < this.elementsCollection.length; ++i ){
this.elementsCollection[ i ].style.position = 'relative';
this.effectInterval = setInterval( function( elementsCollection ) {
for( var x = 0; x < positions.length; ++x ){
switch( positions[ x ] ){
case 'left':
this.elementsCollection[ i ].style.left = -Math.abs( Math.random() * 20 ) + 'px';
break;
case 'right':
this.elementsCollection[ i ].style.left = parseInt( Math.random() * 20 ) + 'px';
break;
case 'top':
case 'up':
this.elementsCollection[ i ].style.top = -Math.abs( Math.random() * 20 ) + 'px';
break;
case 'down':
this.elementsCollection[ i ].style.top = parseInt( Math.random() * 20 ) + 'px';
break;
}
}
} , this.getInterval() );
setTimeout( function(){
return function(){
clearInterval( this.effectInterval );
this.elementsCollection[ i ].style.position = null;
}
} , this.getTimeout() );
}
}
}
谢谢!
【问题讨论】:
标签: javascript this setinterval prototype-programming