简体   繁体   English

如何使用 javascript 无限循环 div 中的图像?

[英]How do I loop images in a div downwards infinitely using javascript?

I am working on an assignment with the description of:我正在处理一项描述如下的任务:

Create a JavaScript loop using images to get the output as picture below.使用图像创建 JavaScript 循环以获得 output,如下图所示。 You can use any images and it is requiring to use maximum 5 images.您可以使用任何图像,并且最多需要使用 5 张图像。 The images should be loop infinitely.图像应该无限循环。 Use your own creativity to design the layout.使用您自己的创造力来设计布局。

2

But I have no idea how to work on that, I only created a div with three images in it and stuck on the javascript part.但我不知道如何处理,我只创建了一个包含三个图像的 div 并停留在 javascript 部分。

This is my code:这是我的代码:

 .container { width: 100%; height: 27vh; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; }.box { width: 200px; height: 200px; margin: 1px; } img { width: 200px; height: 200px; }
 <div class="container"> <div class="box"> <img class="car" src="images/car1.jpg"> </div> <div class="box"> <img class="car" src="images/car2.jpg"> </div> <div class="box"> <img class="car" src="images/car3.jpg"> </div> </div>
This is what I've got now: 这就是我现在得到的:

1

Here is something to get you started.这里有一些东西可以帮助您入门。 I changed <div class="container"> to <div id="container"> and added another container within that one: .box-container .我将<div class="container">更改为<div id="container">并在其中添加了另一个容器: .box-container

The basic idea is to clone a container with the initial images, and then append (add) them to your original container.基本思想是使用初始图像克隆一个容器,然后将 append(添加)它们到您的原始容器中。

 let numberOfRows = 3; const containerDiv = document.getElementById('container'); // get first element in #container, which is just one child: .box-container let boxContainerDiv = containerDiv.children[0]; while (numberOfRows) { numberOfRows--; // clone the node let clonedChild = boxContainerDiv.cloneNode(true); // add the clone node to #container containerDiv.appendChild(clonedChild); }
 .box-container { width: 100%; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; }.box { width: 200px; height: 200px; margin: 1px; } img { width: 200px; height: 200px; border: 1px solid; }
 <div id="container"> <div class="box-container"> <div class="box"> <img class="car" src="https://via.placeholder.com/200.png?text=1"> </div> <div class="box"> <img class="car" src="https://via.placeholder.com/200.png?text=2"> </div> <div class="box"> <img class="car" src="https://via.placeholder.com/200.png?text=3"> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM