gpt4 book ai didi

Drag and drop 3 different images inside a div(将3个不同的图像拖放到一个div中)

转载 作者:bug小助手 更新时间:2023-10-25 13:56:08 27 4
gpt4 key购买 nike



I am able to try to drag and drop multiple images into a same div which is under a .
There will be an initial value for each image and those many images will be present inside the div. When we drag and drop any image it should add that image with the existing images inside the div.

我能够尝试拖放多个图像到同一个div下。每个图像都有一个初始值,这些图像将出现在div中。当我们拖放任何图像时,它应该将该图像与div中的现有图像一起添加。


I tried drag and drop for 2 images.
First image is shown in div with the initial value and when I drag drop the second image it works fine. Again if i Try to drop first image all the 2nd images dropped in the div changes to 1st image.
I want to save the number of different images dropped inside a div without replacing the existing images with the current image that has been dropped.

我试着拖放了2张图片。第一个图像以初始值显示在div中,当我拖放第二个图像时,它工作正常。同样,如果我尝试删除第一个图像,div中所有的第二个图像都会变为第一个图像。我希望保存放置在div中的不同图像的数量,而不用已放置的当前图像替换现有图像。


I tried the following condition

我尝试了以下条件


(condition1 ? img1 : (condition2 ? img1 : img2));

(条件1?图片1:(条件2?Img1:img2));


I am trying to do with conditional statement.

我正在尝试使用条件语句。


Also I am saving the number of images dropped into a state.

此外,我正在保存图像的数量下降到一个状态。


更多回答
优秀答案推荐



<!DOCTYPE html>
<html>
<head>
<style>
.droppable {
width: 300px;
height: 300px;
border: 2px dashed #ccc;
}
img {
width: 50px;
height: 50px;
margin: 5px;
}
</style>
</head>
<body>
<div class="droppable" id="dropArea">
<!-- Images will be added here -->
</div>

<script>
const dropArea = document.getElementById('dropArea');
const state = {};

// Add a dragover event listener to allow dropping.
dropArea.addEventListener('dragover', (e) => {
e.preventDefault();
});

// Add a drop event listener to handle the dropped items.
dropArea.addEventListener('drop', (e) => {
e.preventDefault();
const file = e.dataTransfer.files[0];

// Check if the file is an image.
if (file.type.startsWith('image/')) {
const imgUrl = URL.createObjectURL(file);

// Check if the image URL is already in the state.
if (!state[imgUrl]) {
state[imgUrl] = 1; // Initialize count for this image URL.
} else {
state[imgUrl]++; // Increment the count for this image URL.
}

// Update the HTML content of the div based on the state.
updateDivContent();
}
});

// Function to update the HTML content of the div based on the state.
function updateDivContent() {
dropArea.innerHTML = '';

for (const imgUrl in state) {
const imgCount = state[imgUrl];
const img = new Image();
img.src = imgUrl;
img.alt = `Image ${imgCount}`;
dropArea.appendChild(img);
}
}
</script>
</body>
</html>




To achieve your goal of allowing users to drag and drop multiple images into a while maintaining the count of different images dropped without replacing existing images with the current image that has been dropped, you can follow these steps:

要实现您的目标,即允许用户将多个图像拖放到中,同时保持已拖放的不同图像的数量,而不用已拖放的当前图像替换现有图像,您可以执行以下步骤:


Maintain a state to keep track of the images dropped and their counts.
When a new image is dropped, check if it's already in the . If it's not present, add it to the state and update the count. If it's already present, do nothing.
Update the HTML content of the based on the state to display the images and their counts.

维护一种状态以跟踪丢弃的图像及其计数。当一个新图像被删除时,检查它是否已经在。如果不存在,则将其添加到状态并更新计数。如果它已经存在,什么都不做。根据状态更新的HTML内容以显示图像及其计数。


更多回答

OP is using react

操作员正在使用REACT

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com