Skip to content
Advertisement

sidebar navigation text not smooth while transition

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.

/* Set the width of the side navigation to 250px */
function openNav() {
    document.getElementById("mySidenav").style.width = "400px";
}

/* Set the width of the side navigation to 0 */
function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
}
/* The side navigation menu */
.sidenav {
    height: 100%; /* 100% Full-height */
    width: 0; /* 0 width - change this with JavaScript */
    position: fixed; /* Stay in place */
    z-index: 1; /* Stay on top */
    top: 0;
    left: 0;
    background-color: #111; /* Black*/
    overflow-x: hidden; /* Disable horizontal scroll */
    padding-top: 60px; /* Place content 60px from the top */
    transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}

/* The navigation menu links */
.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    font-family: Gotham;
    color: #818181;
    display: block;
    transition: 0.3s
}

/* When you mouse over the navigation links, change their color */
.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

/* Position and style the close button (top right corner) */
.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
    transition: margin-left .5s;
    padding: 20px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
    .sidenav {padding-top: 15px;}
    .sidenav a {font-size: 18px;}
}
<body bgcolor="#E6E6FA">
  <div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
     
    <p style=color:white>TEST TEST TEST TEST TEST TEST TEST TEST
    TEST TEST TEST TEST
    TEST TEST TEST TEST
    TEST TEST TEST TEST
    TEST TEST TEST TEST</p>
 
</div>

<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>

<div id="main">
</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. Use transform: translateX(-100%) to hide the Nav, and translateX(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 and style attributes

const toggleNav = () => {
  document.querySelector("#mySidenav").classList.toggle("is-open");
};

document.querySelectorAll(".toggleNav").forEach(el => {
  el.addEventListener("click", toggleNav)
});
/* QuickReset */ * {margin:0; box-sizing: border-box;}

#mySidenav {
  position: fixed;
  overflow-x: hidden;
  width: calc(100vw - 200px); /* try not to use fixed px */
  height: 100vh;
  z-index: 200;
  top: 0;
  left: 0;
  padding: 60px;
  background-color: #111;
  color: #fff;
  transition: 0.5s;
  transform: translateX(-100%); /* hide it by minus own width */
}

#mySidenav.is-open {
  transform: translateX(0%); /* show it */
}

.toggleNav {
  padding: 1rem 1.3rem;
  background: none;
  border: none;
  font-family: Gotham;
  font-size: 2rem;
  cursor: pointer;
}

.toggleNav:hover {
  color: #999;
}

#mySidenav .toggleNav {
  position: absolute;
  top: 0;
  right: 0;
  color: #777;
}
<div id="mySidenav">
  <button type="button" class="toggleNav">✕</button>
  <p>
    TEST TEST TEST TEST TEST TEST 
    TEST TEST TEST TEST TEST TEST
    TEST TEST TEST TEST TEST TEST 
    TEST TEST TEST TEST TEST TEST
  </p>
</div>

<button type="button" class="toggleNav">☰</button>

<div id="main"></div>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement