私のウェブページには青い四角形が3つあります。各スクエアのサイズは50 x 50ピクセルです。私はゆっくりと各正方形のサイズを80pxずつ80pxずつ増やしたいと考えています。 上記の予想された結果の代わりに、最後の正方形のサイズのみが変更されました。プレーンJavaScriptを使用して複数のdivをループでアニメーション化する
以下のコードをどのように動作させるにはどうすればよいですか。
<!DOCTYPE html>
<html>
<head>
<title>zoom box</title>
<script>
function zoom() {
var box;
const height = 50;
var boxes = document.getElementsByClassName('box');
var id;
for (i = 0; i < boxes.length; i++) {
box = boxes[i];
h = height;
frame();
}
function frame() {
h++;
if (h >= 80) {
return;
}
box.style.height = h + 'px';
box.style.width = h + 'px';
setTimeout(frame, 100);
}
}
</script>
<head>
<body width="100%" height="100%" onload="zoom()">
<div style="width: 20em; height: 20em; margin:auto; border-style:solid;display:flex; justify-content: space-between;">
<div class="box" style="background-color: blue; width:50px; height: 50px; margin:auto">
</div>
<div class="box" style="background-color: blue; width:50px; height: 50px; margin:auto">
</div>
<div class="box" style="background-color: blue; width:50px; height: 50px; margin:auto">
</div>
</div>
</body>
</html>
フレーム()関数にボックスオブジェクトを渡します。 –