I have an element that contains a couple of elements and I want that while clicking on the green header of the element it will be dragged with the mouse movement and it will stay in the last position(it’s a simulation for a windows window).
JavaScript
x
39
39
1
* {
2
margin: 0;
3
padding: 0;
4
box-sizing: border-box;
5
}
6
body {
7
overflow: hidden;
8
}
9
.container {
10
height: 100vh;
11
width: 100%;
12
display: flex;
13
justify-content: center;
14
align-items: center;
15
position: relative;
16
}
17
.box {
18
position: absolute;
19
background-color: #000;
20
width: 475px;
21
height: 350px;
22
border: solid 1px rgb(0, 255, 0);
23
background-position: center;
24
background-size: contain;
25
background-repeat: no-repeat;
26
}
27
#header {
28
position: absolute;
29
height: 20px;
30
width: calc(100% + 2px);
31
transform: translateX(-1px);
32
top: -20px;
33
background-color: rgb(0, 255, 0);
34
}
35
.back-chiffre {
36
background: #000 url(/De4G.gif);
37
margin-top: 200px;
38
margin-right: 100px;
39
}
JavaScript
1
11
11
1
<div class="container">
2
<div id="element" class="box back-chiffre">
3
<span id="header">
4
<span class="right">
5
<i class="fa-solid fa-window-minimize"></i>
6
<i class="fa-solid fa-window-restore"></i>
7
<i class="fa-solid fa-xmark"></i>
8
</span>
9
</span>
10
</div>
11
</div>
Advertisement
Answer
Since @User have described it properly i am not going to describe again there is a handy function for the dragable window simulation i have created you can use it easily without going any deep or if you wanted to know you can go through codes as well
JavaScript
1
34
34
1
function makeItMovable(){
2
let all = Array.from(arguments),
3
activeClass = "active_window"
4
all.forEach(el=>{
5
const header = el.querySelector("#header")
6
const rootEl = el.closest(".container")
7
let activeEl = null
8
9
el.addEventListener("mousedown",() => {
10
all.forEach(each=>each.classList.remove(activeClass))
11
el.classList.add(activeClass)
12
})
13
header.addEventListener("mousedown",(e)=>{activeEl = e})
14
document.addEventListener("mouseup",()=>{activeEl = null})
15
rootEl.addEventListener("mousemove",e=>{
16
if(activeEl){
17
const el = activeEl.target.closest(".box")
18
setPos(e.clientX,e.clientY,el)
19
}
20
})
21
function setPos(x,y,el){
22
x -= activeEl.layerX
23
y += activeEl.layerY
24
let newStyle = `left:${x}px !important; top:${y}px !important;`
25
el.setAttribute("style",newStyle)
26
}
27
})
28
}
29
30
// example use
31
window.onload = () => {
32
const boxes = document.querySelectorAll(".box")
33
makeItMovable(boxes)
34
}
JavaScript
1
48
48
1
@import url("./core-utils.css");
2
* {
3
margin: 0;
4
padding: 0;
5
box-sizing: border-box;
6
}
7
body {
8
overflow: hidden;
9
}
10
.container {
11
height: 100vh;
12
width: 100%;
13
display: flex;
14
justify-content: center;
15
align-items: center;
16
position: relative;
17
}
18
.box {
19
position: absolute;
20
background-color: #000;
21
width: 475px;
22
height: 350px;
23
border: solid 1px rgb(0, 255, 0);
24
background-position: center;
25
background-size: contain;
26
background-repeat: no-repeat;
27
user-select: none !important;
28
}
29
.active_window #header {
30
background-color: rgb(0, 255, 0);
31
}
32
#header {
33
position: absolute;
34
height: 20px;
35
width: calc(100% + 2px);
36
transform: translateX(-1px);
37
top: -20px;
38
background-color: rgb(122, 245, 122);
39
}
40
.back-chiffre {
41
background: #000 url(/De4G.gif);
42
/* margin-top: 200px;
43
margin-right: 100px; */
44
}
45
.active_window {
46
z-index: 999;
47
box-shadow: 3px 0px 10px 2px black;
48
}
JavaScript
1
29
29
1
<div class="container">
2
<div class="box back-chiffre">
3
<span id="header">
4
<span class="right">
5
<i class="fa-solid fa-window-minimize"></i>
6
<i class="fa-solid fa-window-restore"></i>
7
<i class="fa-solid fa-xmark"></i>
8
</span>
9
</span>
10
</div>
11
<div class="box back-chiffre">
12
<span id="header">
13
<span class="right">
14
<i class="fa-solid fa-window-minimize"></i>
15
<i class="fa-solid fa-window-restore"></i>
16
<i class="fa-solid fa-xmark"></i>
17
</span>
18
</span>
19
</div>
20
<div class="box back-chiffre">
21
<span id="header">
22
<span class="right">
23
<i class="fa-solid fa-window-minimize"></i>
24
<i class="fa-solid fa-window-restore"></i>
25
<i class="fa-solid fa-xmark"></i>
26
</span>
27
</span>
28
</div>
29
</div>