October 24, 2024
Chicago 12, Melborne City, USA
CSS

I'm having trouble with this CSS animation for my sidebar


I created a webpage with a sidebar that has links to other parts of my website. The sidebar is hidden (not really hidden, just off the page using left: -16vw) but has a button that sticks out for the visitor to see. What I would like is when the visitor clicks on the button the sidebar slides out. Here is what my code looks like:

HMTL:

<div id="sideBar">
    <button id="navButton" type="button">>></button>
    <nav>
      <ul>
        <li><a href="#">Stuff</a></li>
        <li><a href="#">More Stuff</a></li>
        <li><a href="#">Even More Stuff</a></li>
      </ul>
    </nav>
  </div>

CSS:

#sideBar {
  display: flex;
  flex-direction: row-reverse;
  position: fixed;
  height: 84vh;
  min-width: 18vw;
  top: 8vh;
  left: -16vw;
  animation: slide;
  animation-duration: .3s;
  animation-timing-function: ease-in;
  animation-delay: 0s;
  animation-fill-mode: forwards;
  animation-play-state: paused;
}

@keyframes slide {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(16vw);
  }
}

nav {
  background-color: gray;
  width: 100%;
  height: 100%;
}

#navButton {
  min-width:2vw;
  margin-left: -1px;
  background-color: rgb(228, 228, 228);
  border: 2px solid rgb(51, 51, 51);
  border-radius: 0 50px 50px 0;
  font-size: 1.5rem;
  font-weight: bold;
  opacity: .5;
  transition: 
    opacity .3s,
    background-color 0s;
}

#navButton:active {
  background-color: white; 
}

#navButton:active + #sideBar {
  animation-play-state: running;
}

#navButton:hover {
  opacity: 1;
  cursor: pointer;
}

I did read somewhere online that this might need JavaScript in order to make this work. Is there anyway I can do this only using CSS and HTML? I haven’t learned JavaScript yet and I’m just trying to stick with only HTML/CSS. I’m trying to learn as much as I can with these two before moving on. Thank you guys in advance for your help I really appreciate it. Also if you have any tips with my code to make it better please let me know; I want to learn as much as I can.

The animation works when I comment out the "animation-play-state" so I know that it works. I’ve also tried just adding to just the "active" pseudo-class alone and that didn’t work. I tried adding it to "hover" as well and that didn’t work either.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video