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);
添加回答
举报