i try to change css but not work for text short text is ok but i have to put long paragraph when click it not smooth while transition working i use template from w3school, i want text show up smooth like button or short text please advise me how I should use it to remember for next time i can help in community if i saw someone ask like me thank you.
JavaScript
x
9
1
/* Set the width of the side navigation to 250px */
2
function openNav() {
3
document.getElementById("mySidenav").style.width = "400px";
4
}
5
6
/* Set the width of the side navigation to 0 */
7
function closeNav() {
8
document.getElementById("mySidenav").style.width = "0";
9
}
JavaScript
1
50
50
1
/* The side navigation menu */
2
.sidenav {
3
height: 100%; /* 100% Full-height */
4
width: 0; /* 0 width - change this with JavaScript */
5
position: fixed; /* Stay in place */
6
z-index: 1; /* Stay on top */
7
top: 0;
8
left: 0;
9
background-color: #111; /* Black*/
10
overflow-x: hidden; /* Disable horizontal scroll */
11
padding-top: 60px; /* Place content 60px from the top */
12
transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
13
}
14
15
/* The navigation menu links */
16
.sidenav a {
17
padding: 8px 8px 8px 32px;
18
text-decoration: none;
19
font-size: 25px;
20
font-family: Gotham;
21
color: #818181;
22
display: block;
23
transition: 0.3s
24
}
25
26
/* When you mouse over the navigation links, change their color */
27
.sidenav a:hover, .offcanvas a:focus{
28
color: #f1f1f1;
29
}
30
31
/* Position and style the close button (top right corner) */
32
.sidenav .closebtn {
33
position: absolute;
34
top: 0;
35
right: 25px;
36
font-size: 36px;
37
margin-left: 50px;
38
}
39
40
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
41
#main {
42
transition: margin-left .5s;
43
padding: 20px;
44
}
45
46
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
47
@media screen and (max-height: 450px) {
48
.sidenav {padding-top: 15px;}
49
.sidenav a {font-size: 18px;}
50
}
JavaScript
1
16
16
1
<body bgcolor="#E6E6FA">
2
<div id="mySidenav" class="sidenav">
3
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
4
5
<p style=color:white>TEST TEST TEST TEST TEST TEST TEST TEST
6
TEST TEST TEST TEST
7
TEST TEST TEST TEST
8
TEST TEST TEST TEST
9
TEST TEST TEST TEST</p>
10
11
</div>
12
13
<span onclick="openNav()"><img src="https://cdn4.iconfinder.com/data/icons/wirecons-free-vector-icons/32/menu-alt-512.png" width="40px" style="padding-top: 40px; padding-left: 40px;"></span>
14
15
<div id="main">
16
</div>
Advertisement
Answer
- You don’t need two separate JavaScript functions. Only one called
toggleNav
- Use Element.classList.toggle() to toggle a CSS class
- Don’t animate the
width
. Usetransform: translateX(-100%)
to hide the Nav, andtranslateX(0%)
to show (open). - Don’t use links if you actually want buttons. Links (Anchors) are used to navigate
- Don’t use inline
on*
JS attributes andstyle
attributes
JavaScript
1
7
1
const toggleNav = () => {
2
document.querySelector("#mySidenav").classList.toggle("is-open");
3
};
4
5
document.querySelectorAll(".toggleNav").forEach(el => {
6
el.addEventListener("click", toggleNav)
7
});
JavaScript
1
40
40
1
/* QuickReset */ * {margin:0; box-sizing: border-box;}
2
3
#mySidenav {
4
position: fixed;
5
overflow-x: hidden;
6
width: calc(100vw - 200px); /* try not to use fixed px */
7
height: 100vh;
8
z-index: 200;
9
top: 0;
10
left: 0;
11
padding: 60px;
12
background-color: #111;
13
color: #fff;
14
transition: 0.5s;
15
transform: translateX(-100%); /* hide it by minus own width */
16
}
17
18
#mySidenav.is-open {
19
transform: translateX(0%); /* show it */
20
}
21
22
.toggleNav {
23
padding: 1rem 1.3rem;
24
background: none;
25
border: none;
26
font-family: Gotham;
27
font-size: 2rem;
28
cursor: pointer;
29
}
30
31
.toggleNav:hover {
32
color: #999;
33
}
34
35
#mySidenav .toggleNav {
36
position: absolute;
37
top: 0;
38
right: 0;
39
color: #777;
40
}
JavaScript
1
13
13
1
<div id="mySidenav">
2
<button type="button" class="toggleNav">✕</button>
3
<p>
4
TEST TEST TEST TEST TEST TEST
5
TEST TEST TEST TEST TEST TEST
6
TEST TEST TEST TEST TEST TEST
7
TEST TEST TEST TEST TEST TEST
8
</p>
9
</div>
10
11
<button type="button" class="toggleNav">☰</button>
12
13
<div id="main"></div>