我剛剛開始學習如何使用 JavaScript 進行編碼,以達到藝術目的,如果有人可以幫助我完成特定的工作,我將非常感激。我收集了數字日記的所有元素(并將它們分成 div 和 imgs),我希望它們全部隨機且同時(加載時)放置在屏幕上。這是我到目前為止所取得的成就:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> #image{ position: absolute; } </style> <script type="text/javascript"> function Init(){ picture=document.getElementById("image"); spaceH=screen.height - picture.height; spaceW=screen.width - picture.width; setInterval (mover, 0); } function mover(){ picture.style.top=Math.round(Math.random()* spaceH) + "px"; picture.style.left=Math.round(Math.random()* spaceW) + "px"; } </script> </head> <body onload="Init()"> <img src="myImage" id="image"> </body></html>我只能使用一張圖像來完成此操作,并且無法想象如何使用其中的許多圖像來完成此操作(我的意思是我知道我必須使用數組,但我無法自己弄清楚如何使用)。另外,如果我嘗試替換 div 的 img 標簽,它也不起作用。有人可以幫忙嗎?我對此要發瘋了。
1 回答

LEATH
TA貢獻1936條經驗 獲得超7個贊
嘗試這個!
var pictures = [];
function Init(){
pictures = document.querySelectorAll("img");
setInterval (mover, 0);
}
function mover(){
pictures.forEach(img => {
var spaceH =screen.height - img.height;
var spaceW =screen.width - img.width;
img.style.top=Math.round(Math.random()* spaceH) + "px";
img.style.left=Math.round(Math.random()* spaceW) + "px";
});
}
</script>
<body onload="Init()">
<img src="myImage">
<img src="myImage">
</body>
- 1 回答
- 0 關注
- 161 瀏覽
添加回答
舉報
0/150
提交
取消