I have five images
in my code, I want to drop these such that it is dropped in the desired position(circular position
) in the dropped area.
ie,
when the five images are dropped it should form a circle shape rather than a straight line
How to achieve this?
JavaScript
x
13
13
1
function allowDrop(ev) {
2
ev.preventDefault();
3
}
4
5
function drag(ev) {
6
ev.dataTransfer.setData("text", ev.target.id);
7
}
8
9
function drop(ev) {
10
ev.preventDefault();
11
var data = ev.dataTransfer.getData("text");
12
ev.target.appendChild(document.getElementById(data));
13
}
JavaScript
1
6
1
#div1 {
2
width: 350px;
3
height: 150px;
4
padding: 10px;
5
border: 1px solid #aaaaaa;
6
}
JavaScript
1
9
1
<p>how to position the dropped images in a circular position rather than in a straight line on drop:</p>
2
3
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
4
<br>
5
<img id="drag1" src="https://picsum.photos/200/300" draggable="true" ondragstart="drag(event)" width="50" height="50">
6
<img id="drag2" src="https://picsum.photos/200/300?image=0" draggable="true" ondragstart="drag(event)" width="50" height="50">
7
<img id="drag3" src="https://picsum.photos/200/300/?gravity=east" draggable="true" ondragstart="drag(event)" width="50" height="50">
8
<img id="drag4" src="https://picsum.photos/200/300/?blur" draggable="true" ondragstart="drag(event)" width="50" height="50">
9
<img id="drag5" src="https://picsum.photos/200/300/?random" draggable="true" ondragstart="drag(event)" width="50" height="50">
Advertisement
Answer
Use nth-child()
to #div1 img
and set position to each image with top/left
also use position:absolute
to img
and position:relative;
to #div
JavaScript
1
13
13
1
function allowDrop(ev) {
2
ev.preventDefault();
3
}
4
5
function drag(ev) {
6
ev.dataTransfer.setData("text", ev.target.id);
7
}
8
9
function drop(ev) {
10
ev.preventDefault();
11
var data = ev.dataTransfer.getData("text");
12
ev.target.appendChild(document.getElementById(data));
13
}
JavaScript
1
30
30
1
#div1 {
2
width: 350px;
3
height: 150px;
4
padding: 10px;
5
border: 1px solid #aaaaaa;
6
position:relative;
7
}
8
#div1 img{
9
position: absolute;
10
}
11
#div1 img:nth-child(1) {
12
left: 155px;
13
}
14
#div1 img:nth-child(2) {
15
top: 35px;
16
left:215px;
17
}
18
#div1 img:nth-child(3) {
19
top: 95px;
20
left: 185px;
21
22
}
23
#div1 img:nth-child(4) {
24
top: 95px;
25
left: 120px;
26
}
27
#div1 img:nth-child(5) {
28
top: 35px;
29
left:95px;
30
}
JavaScript
1
9
1
<p>how to position the dropped images in a circular postion rather than in a straight line on drop:</p>
2
3
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
4
<br>
5
<img id="drag1" src="https://picsum.photos/200/300" draggable="true" ondragstart="drag(event)" width="50" height="50">
6
<img id="drag2" src="https://picsum.photos/200/300?image=0" draggable="true" ondragstart="drag(event)" width="50" height="50">
7
<img id="drag3" src="https://picsum.photos/200/300/?gravity=east" draggable="true" ondragstart="drag(event)" width="50" height="50">
8
<img id="drag4" src="https://picsum.photos/200/300/?blur" draggable="true" ondragstart="drag(event)" width="50" height="50">
9
<img id="drag5" src="https://picsum.photos/200/300/?random" draggable="true" ondragstart="drag(event)" width="50" height="50">