1 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個贊
創(chuàng)建一個moveImage函數(shù):
function moveImage(){
var top = Math.floor(Math.random()*75);
var left = Math.floor(Math.random()*75);
var right = Math.floor(Math.random()*75);
var bottom = Math.floor(Math.random()*75);
image.style.top = top + 'px';
image.style.left = left + 'px';
image.style.right = right + 'px';
image.style.bottom = bottom + 'px';
}
更新 onClick 以使用新函數(shù):
image.addEventListener('click', moveImage);
更新間隔以使用新函數(shù):
var timing = setInterval(moveImage, 5000);
整個東西:
var image = document.getElementById('image');
image.style.position = 'relative';
function moveImage(){
var top = Math.floor(Math.random()*75);
var left = Math.floor(Math.random()*75);
var right = Math.floor(Math.random()*75);
var bottom = Math.floor(Math.random()*75);
image.style.top = top + 'px';
image.style.left = left + 'px';
image.style.right = right + 'px';
image.style.bottom = bottom + 'px';
};
image.addEventListener('click', moveImage);
var timing = setInterval(moveImage, 5000);
添加回答
舉報