亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

(x) 秒后移動圖像

(x) 秒后移動圖像

MMTTMM 2023-08-05 21:00:14
我試圖在 5 秒后將圖像移動到網頁上的隨機位置,到目前為止我有這個:var image = document.getElementById('image');var position = 0;var timing = setInterval(timing, 5000);image.style.position = 'relative';image.onclick=function move(direction){  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';}如何讓它在 5 秒后移動?
查看完整描述

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);


查看完整回答
反對 回復 2023-08-05
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號