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

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

使用 JavaScript 在隨機位置加載時顯示多個元素(div 和 img)

使用 JavaScript 在隨機位置加載時顯示多個元素(div 和 img)

開心每一天1111 2024-01-03 17:01:54
我剛剛開始學習如何使用 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>


查看完整回答
反對 回復 2024-01-03
  • 1 回答
  • 0 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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