我想簡單地每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