I want the button near the homer class to be invisible. It should be visible only on mouseover, what’s wrong with my code?
By default, it is visible, and when I mouseover for the first time it inserts visibility: visible
. When I mouse-out for the first time, it disappears.
But nothing happens when I mouseover again. I need a solution in Vanilla JS.
Here is the code:
JavaScript
x
15
15
1
var dt = new Date();
2
3
document.getElementById("datetime").innerHTML = dt.toLocaleDateString('default', { day: 'numeric' , month: 'long', year:'numeric' });
4
5
document.getElementById("btn1").onmouseover = function() {mouseOver();};
6
document.getElementById("btn1").onmouseout = function() {mouseOut();};
7
8
function mouseOver() {
9
document.getElementById("btn1").style.visibility = "visible";
10
}
11
12
function mouseOut() {
13
document.getElementById("btn1").style.visibility = "hidden";
14
15
}
JavaScript
1
281
281
1
* {
2
margin: 0;
3
padding: 0;
4
}
5
6
body {
7
background: url(img/bg4.jpg);
8
background-size: cover;
9
}
10
11
img {
12
border-radius: 100%;
13
width: 150px;
14
height: 150px;
15
box-shadow: 8px 8px 13px #222;
16
border: solid 3px #606f46;
17
}
18
19
span {
20
background: #beebb3;
21
border-color: #606f46;
22
border-radius: 22px 0 22px 0;
23
padding: 2px;
24
border-style: solid;
25
margin-top: 5px;
26
box-shadow: 4px 4px 13px #222;
27
width: 145px;
28
color: green;
29
}
30
31
h1 {
32
/* width: 580px; */
33
font-family: 'Sofia', cursive;
34
font-size: 3.4rem;
35
text-align: center;
36
margin: 10px auto;
37
padding-top: 15px;
38
padding-bottom: 15px;
39
}
40
41
h2 {
42
font-family: 'Sofia', cursive;
43
font-size: 2rem;
44
}
45
46
#btn1 {
47
pointer-events: all;
48
49
}
50
51
.r {
52
background-image: url('img/refresh.png');
53
background-size: contain;
54
55
}
56
57
.refresh {
58
outline: none;
59
width: 50px;
60
height: 50px;
61
border-radius: 100%;
62
position: absolute;
63
right: 3px;
64
top: 108px;
65
}
66
67
68
#no1 {
69
display: flex;
70
justify-content: center;
71
width: 190px;
72
line-height: 16px;
73
margin: auto;
74
}
75
76
#no2 {
77
display: flex;
78
justify-content: center;
79
width: 190px;
80
line-height: 16px;
81
margin: auto;
82
margin-top: 9px;
83
}
84
85
#no3 {
86
display: flex;
87
justify-content: center;
88
width: 190px;
89
line-height: 16px;
90
margin: auto;
91
margin-top: 8px;
92
}
93
94
#no4 {
95
display: flex;
96
justify-content: center;
97
width: 190px;
98
line-height: 16px;
99
margin: auto;
100
margin-top: 9px;
101
}
102
103
.container1 {
104
width: 580px;
105
font-family: verdana, arial, helvetica, sans-serif;
106
font-weight: bold;
107
font-size: 11px;
108
text-align: center;
109
margin: auto;
110
}
111
112
.container2 {
113
width: 700px;
114
font-family: verdana, arial, helvetica, sans-serif;
115
font-size: 11px;
116
text-align: center;
117
margin: auto;
118
font-weight: bold;
119
font-size: 11px;
120
}
121
122
.container3 {
123
width: 1100px;
124
font-family: verdana, arial, helvetica, sans-serif;
125
font-size: 11px;
126
text-align: center;
127
margin: auto;
128
font-weight: bold;
129
font-size: 11px;
130
}
131
132
.container4 {
133
width: 1300px;
134
font-family: verdana, arial, helvetica, sans-serif;
135
font-size: 11px;
136
text-align: center;
137
margin: auto;
138
font-weight: bold;
139
font-size: 11px;
140
}
141
142
.tree {
143
display: flex;
144
flex-direction: column;
145
align-items: center;
146
margin: 10px;
147
position: relative;
148
}
149
150
.white_bg {
151
background-color: white;
152
}
153
154
.container1_row4 {
155
display: flex;
156
justify-content: center;
157
margin-left: 240px;
158
position: relative;
159
right: 10px;
160
}
161
162
.container2_row4 {
163
display: flex;
164
justify-content: center;
165
margin-left: 35px;
166
margin-right: 150px;
167
position: relative;
168
right: 25px;
169
}
170
171
.container3_row4 {
172
display: flex;
173
justify-content: center;
174
margin-right: 240px;
175
}
176
177
.level1 {
178
height: 3px;
179
width: 110px;
180
border-style: hidden solid solid solid;
181
border-width: thick;
182
margin-left: 230px;
183
}
184
185
.oblique1 {
186
width: 88px;
187
transform: rotate(-30deg);
188
border-width: thick;
189
border-style: hidden hidden solid hidden;
190
}
191
192
.oblique2 {
193
width: 88px;
194
transform: rotate(30deg);
195
border-width: thick;
196
border-style: hidden hidden solid hidden;
197
}
198
199
.oblique_row {
200
display: flex;
201
margin-left: 200px;
202
margin-top: 17px;
203
}
204
205
.line_container2 {
206
display: flex;
207
}
208
209
.first_arrow_row2 {
210
position: relative;
211
right: 125px;
212
}
213
214
.second_arrow_row2 {
215
position: relative;
216
right: 150px;
217
}
218
219
.second_arrow_row2>.oblique_row>.oblique1 {
220
border-style: hidden;
221
}
222
223
.line_container3 {
224
display: flex;
225
}
226
227
.line2 {
228
width: 45px;
229
transform: rotate(90deg);
230
border-width: thick;
231
border-style: hidden hidden solid hidden;
232
}
233
234
.first_arrow_row3 {
235
position: relative;
236
right: 165px;
237
}
238
239
.second_arrow_row3 {
240
position: relative;
241
right: 295px;
242
}
243
244
.third_arrow_row3 {
245
position: relative;
246
right: 295px;
247
}
248
249
.third_arrow_row3>.oblique_row>.line2 {
250
position: relative;
251
left: 70px;
252
}
253
254
.second_arrow_row3>.oblique_row>.line2 {
255
position: relative;
256
left: 70px;
257
}
258
259
.first_arrow_row3>.oblique_row {
260
position: relative;
261
right: 25px
262
}
263
264
.first_arrow_row3>.level1 {
265
position: relative;
266
left: 60px
267
}
268
269
.first_arrow_row3>.oblique_row>.oblique1 {
270
width: 155px;
271
transform: rotate(344deg);
272
}
273
274
.first_arrow_row3>.oblique_row>.oblique2 {
275
width: 155px;
276
transform: rotate(16deg);
277
}
278
279
#datetime {
280
text-align: center;
281
}
JavaScript
1
225
225
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<title>Family Tree</title>
6
<link rel="shortcut icon" href="img/family-tree.png" type="image/x-icon">
7
<link rel="stylesheet" type="text/css" href="style.css">
8
<link rel="preconnect" href="https://fonts.googleapis.com">
9
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
10
<link href="https://fonts.googleapis.com/css2?family=Sofia&display=swap" rel="stylesheet">
11
</head>
12
13
<body>
14
15
<div class="container1">
16
<h1>_ _ _ Family tree _ _ _</h1>
17
<div id="no1">
18
<div class="tree homer">
19
<img src="https://entertainment.time.com/wp-content/uploads/sites/3/2013/05/fictioninfluence_list_homersimpson.jpg">
20
<button id="btn1" class="refresh r"> </button>
21
<span>
22
Homer Simpson
23
</span>
24
</div>
25
<div class="tree marge">
26
<img class="white_bg" src="https://static.wikia.nocookie.net/theultimatesimpsons/images/0/0f/Marge-Simpson-icon.png/revision/latest?cb=20180210061653">
27
<button type="button" name="button"></button>
28
<span>
29
Marge Simpson
30
</span>
31
</div>
32
</div>
33
34
<div class="line_container1">
35
<div class="level1"></div>
36
<div class="oblique_row">
37
<div class="oblique1"></div>
38
<div class="oblique2"></div>
39
</div>
40
</div>
41
42
</div>
43
44
<div class="container2">
45
46
47
<div id="no2">
48
<div class="tree">
49
<img src="https://openpsychometrics.org/tests/characters/test-resources/pics/S/2.jpg">
50
<span>
51
Bart Simpson
52
</span>
53
</div>
54
<div class="tree">
55
<img src="https://mella187.github.io/Cartoon-Hero/img/lisa-avatar.jpg">
56
<span>
57
Lisa Simpson
58
</span>
59
</div>
60
<div class="tree">
61
<img src="https://i.pinimg.com/originals/e5/4a/bc/e54abc44b68d6b2770696b789b20dafa.png">
62
<span>
63
Abraham Simpson
64
</span>
65
</div>
66
<div class="tree">
67
<img class="white_bg" src="https://icons.iconarchive.com/icons/jonathan-rey/simpsons/256/Maggie-Simpson-icon.png">
68
<span>
69
Maggie Simpson
70
</span>
71
</div>
72
73
</div>
74
75
<div class="line_container2">
76
77
<div class="first_arrow_row2">
78
<div class="level1"> </div>
79
<div class="oblique_row">
80
<div class="oblique1"></div>
81
<div class="oblique2"></div>
82
</div>
83
</div>
84
<div class="second_arrow_row2">
85
<div class="level1"> </div>
86
<div class="oblique_row">
87
<div class="oblique1"></div>
88
<div class="oblique2"></div>
89
</div>
90
</div>
91
92
93
</div>
94
</div>
95
96
97
<div class="container3">
98
99
100
<div id="no3">
101
<div class="tree">
102
<img class="white_bg" src="https://cdn.quotesgram.com/img/7/98/1464708000-Ned_Flanders.png">
103
<span>
104
Ned Flanders
105
</span>
106
</div>
107
<div class="tree">
108
<img class="white_bg" src="https://i.pinimg.com/originals/b7/4f/ae/b74faea8de35d22b703b6ae32f891a92.png">
109
<span>
110
Elizabeth Hoover
111
</span>
112
</div>
113
<div class="tree">
114
<img class="white_bg" src="https://static.wikia.nocookie.net/p__/images/3/3a/Seymour_Skinner.png/revision/latest/top-crop/width/360/height/360?cb=20200804144332&path-prefix=protagonist">
115
<span>
116
Seymour Skinner
117
</span>
118
</div>
119
<div class="tree">
120
<img class="white_bg" src="https://www.personality-database.com/profile_images/20160.png">
121
<span> Edna Krabappel
122
</span>
123
</div>
124
125
126
<div class="tree">
127
<img class="white_bg" src="https://static.wikia.nocookie.net/p__/images/b/b4/Barnild_Gumble.png/revision/latest/top-crop/width/360/height/360?cb=20160402113506&path-prefix=protagonist">
128
<span>
129
Barney Gumble
130
</span>
131
</div>
132
<div class="tree">
133
<img class="white_bg" src="https://static.wikia.nocookie.net/simpsons/images/d/d5/Patty_Bouvier1.png/revision/latest/top-crop/width/360/height/360?cb=20201222215313">
134
<span>
135
Selma Bouvier
136
</span>
137
</div>
138
</div>
139
140
<div class="line_container3">
141
142
<div class="first_arrow_row3">
143
<div class="level1"> </div>
144
<div class="oblique_row">
145
<div class="oblique1"></div>
146
<div class="line2"></div>
147
<div class="oblique2"></div>
148
</div>
149
</div>
150
<div class="second_arrow_row3">
151
<div class="level1"> </div>
152
<div class="oblique_row">
153
<div class="line2"></div>
154
</div>
155
</div>
156
<div class="third_arrow_row3">
157
<div class="level1"> </div>
158
<div class="oblique_row">
159
<div class="line2"></div>
160
</div>
161
</div>
162
163
</div>
164
165
</div>
166
167
<div class="container4">
168
169
170
<div id="no4">
171
<div class="container1_row4">
172
<div class="tree">
173
<img src="https://www.canncentral.com/wp-content/uploads/2019/09/Kent_Brockman-Cannabis-1.jpg">
174
<span>
175
Kent Brockman
176
</span>
177
</div>
178
<div class="tree">
179
<img src="https://untappd.akamaized.net/site/beer_logos_hd/beer-1158277_7220b_hd.jpeg">
180
<span>
181
Mayor Quimby
182
</span>
183
</div>
184
<div class="tree">
185
<img src="https://i.pinimg.com/474x/8e/a1/2b/8ea12bd79870a03994fbf65f63baaa92--school-pictures-sideshow.jpg">
186
<span>
187
Sideshow Bob
188
</span>
189
</div>
190
</div>
191
<div class="container2_row4">
192
<div class="tree">
193
<img class="white_bg" src="https://static.wikia.nocookie.net/p__/images/8/81/Wiggum_with_coffee.png/revision/latest/top-crop/width/360/height/360?cb=20160322214733&path-prefix=protagonist">
194
<span>
195
Clancy Wiggum
196
</span>
197
</div>
198
</div>
199
<div class="container3_row4">
200
<div class="tree">
201
<img src="https://www.kindpng.com/picc/m/394-3940977_los-simpson-nikki-mckenna-hd-png-download.png">
202
<span>
203
Nikki McKenna
204
</span>
205
</div>
206
<div class="tree">
207
<img src="https://topicimages.mrowl.com/large/owl/thesimpsons/characters/milhousevanhou_1.jpg">
208
<span>
209
Milhouse Van Houten
210
</span>
211
</div>
212
213
</div>
214
</div>
215
</div>
216
217
<div class="datetime">
218
<h2 id="datetime"></h2>
219
</div>
220
221
<script src="index.js" charset="utf-8"></script>
222
223
</body>
224
225
</html>
Advertisement
Answer
It can’t react to the event being hidden
(not present), here is an option using opacity:
JavaScript
1
23
23
1
var dt = new Date();
2
3
document.getElementById("datetime").innerHTML = dt.toLocaleDateString(
4
"default",
5
{ day: "numeric", month: "long", year: "numeric" }
6
);
7
document.getElementById("btn1").style.opacity = 0;
8
document.getElementById("btn1").onmouseover = function () {
9
mouseOver();
10
};
11
document.getElementById("btn1").onmouseout = function () {
12
mouseOut();
13
};
14
15
function mouseOut() {
16
document.getElementById("btn1").style.opacity = 0;
17
}
18
19
function mouseOver() {
20
document.getElementById("btn1").style.opacity = 1;
21
}
22
23