-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoverimagediv.html
More file actions
105 lines (86 loc) · 2.56 KB
/
hoverimagediv.html
File metadata and controls
105 lines (86 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*,
*::after,
*::before {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
height: 100vh;
background: whitesmoke;
}
#main1 {
position: relative;
transform: translateY(50%);
background: slateblue;
height: 60vh;
}
#main1 .img-div {
margin-bottom: 40px;
height: 15vh;
background: mediumpurple;
}
/* .img {
position: fixed;
transform: translate(150%, -100%);
display: none;
} */
#fixed-img {
position: fixed;
z-index: 99;
height: 35vh;
width: 23vh;
left: 50%;
top: 23%;
border-radius: 10px;
display: none;
background-size: cover;
background-position: center;
transition: 250ms ease-in-out;
}
.space {
height: 100vh;
background-color: whitesmoke;
}
</style>
</head>
<body>
<div id="fixed-img"></div>
<div id="main1">
<div class="img-div" data-img="images/animals-and-nature/animal1.jpg">
<h1>Text1</h1>
</div>
<div class="img-div" data-img="images/animals-and-nature/animal4.jpg">
<h1>Text2</h1>
</div>
<div class="img-div" data-img="images/animals-and-nature/animal3.jpg">
<h1>Text3</h1>
</div>
</div>
<div class="space"></div>
<script>
let ele = document.querySelector("#main1")
let fixed = document.querySelector("#fixed-img")
ele.addEventListener("mouseenter", function () {
fixed.style.display = "block";
})
ele.addEventListener("mouseleave", function () {
fixed.style.display = "none";
})
let eles = document.querySelectorAll(".img-div")
eles.forEach(function (e) {
e.addEventListener("mouseenter", function () {
let imgs = e.getAttribute("data-img");
fixed.style.backgroundImage = `url(${imgs})`
})
})
</script>
</body>
</html>