我想简单地每x毫秒设置一次形状的动画。我在CODEPEN中这样做。无法在codepen中为DOM元素设置动画
我试图移动它使用:
的JavaScript:
ball.getBoundingClientRect().left += 100 + 'px'
ball.style.left += 100 + 'px'
的jQuery:
$('#ball').position().left += 100 + 'px'
但似乎没有任何工作。球出现,但不动。超时也被称为。在控制台中没有错误被抛出。
var ball = null;
var ball_x = null;
var ball_y = null;
function doMove() {
ball.style.left += 100 + 'px';
setTimeout(doMove,100);
}
function init() {
ball = document.getElementById('ball');
ball_x = ball.getBoundingClientRect().left; //displays correct location
ball_y = ball.getBoundingClientRect().top; //displays correct location
doMove();
}
window.onload = init;
CSS:
#ball {
width: 25px;
height: 25px;
background-color: red;
border-radius: 50%;
position: absolute;
left: 100px;
top: 200px;
}
HTML:
<div>
<div id='ball'></div>
</div>
凡* codepen *网址是什么? – Rayon
@RayonDabre见上面 – Growler
'getBoundingClientRect()'是只读属性.. – Rayon