1 回答

TA貢獻1809條經驗 獲得超8個贊
創建一個moveImage函數:
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 以使用新函數:
image.addEventListener('click', moveImage);
更新間隔以使用新函數:
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);
添加回答
舉報