skip to Main Content

Full code: https://codepen.io/vvaciej/pen/abPdEBN

CSS part

html body main .sidebar-area {
  display: none;
  height: max-content;
  position: fixed;
  background-color: #171717;
  border-radius: 0px 0px 5px 0px;
  box-shadow: 0px 0px 5px #171717;
}

html body main .active {
  display: block;
}

JS

closeButton.addEventListener('click', closingMenuByButton);

const menuButton = document.querySelector('.fa-bars');

function showSidebar () {
  if (!dropdownArea.classList.contains('active')) {
    dropdownArea.classList.add('active');
  } else {
    dropdownArea.classList.remove('active');
  }
};

menuButton.addEventListener('click', showSidebar);

function hideSidebarByClickOutside(e) {
    if (dropdownArea.classList.contains('active') && !menuButton.contains(e.target) && !dropdownArea.contains(e.target)) {
        dropdownArea.classList.remove('active');
    }
};

bodyArea.addEventListener('click', hideSidebarByClickOutside);

Want to add simple animation of displaying dropdown.
I’m counting on your help.

2

Answers


  1. transition is a CSS rule, you follow it with a colon :. Then, you list what you want to transition (width, for example) and how long the transition should take, separated by comma ,. Example:

    transition: width 0.4s, height 0.4s;
    

    Proof-of-concept:

    function changeSize(what, amount) {
        document.querySelector(".playground").style[what] = amount + "px";
    }
    .playground {
        width: 100px;
        height: 100px;
        background-color: green;
        transition: width 0.4s, height 0.4s;
    }
    <div class="playground"></div>
    Width: <input type="numer" value="100" oninput="changeSize('width', this.value)">
    Height: <input type="numer" value="100" oninput="changeSize('width', this.value)">

    Your transition values lack the subject as of whose transition time are we speaking about.

    Login or Signup to reply.
  2. I’ve made some CSS updates. Initially, I removed the ‘display: none’ code, and then I moved the sidebar to the left by -100%. When it has the ‘active’ class, I set it to move to the left at 0, and I added a smooth transition.

    const closeButton = document.querySelector('.fa-x');
    const dropdownArea = document.querySelector('.sidebar-area');
    const bodyArea = document.querySelector('body');
    
    function closingMenuByButton () {
      dropdownArea.classList.remove('active');
    };
    
    closeButton.addEventListener('click', closingMenuByButton);
    
    const menuButton = document.querySelector('.fa-bars');
    
    function showSidebar () {
      if (!dropdownArea.classList.contains('active')) {
        dropdownArea.classList.add('active');
      } else {
        dropdownArea.classList.remove('active');
      }
    };
    
    menuButton.addEventListener('click', showSidebar);
    
    function hideSidebarByClickOutside(e) {
        if (dropdownArea.classList.contains('active') && !menuButton.contains(e.target) && !dropdownArea.contains(e.target)) {
            dropdownArea.classList.remove('active');
        }
    };
    
    bodyArea.addEventListener('click', hideSidebarByClickOutside);
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    html {
      font-size: 16px;
      height: 100%;
      font-family: "Quicksand", sans-serif;
      background-color: #ddd;
    }
    html body {
      height: 100%;
    }
    html body:after {
      position: absolute;
      content: "";
      background: linear-gradient(to right, rgba(0, 0, 0, 0.9) 100%, transparent), url("/w-qjCHPZbeXCQ-unsplash.jpg");
      background-repeat: no-repeat;
      background-size: cover;
      background-color: rgba(11, 11, 11, 0.2);
      z-index: -999;
      inset: 0;
    }
    html body main {
      width: 100%;
    }
    html body main .sidebar-area {
      /* display: none; */
      height: -moz-max-content;
      height: max-content;
      position: fixed;
      background-color: #171717;
      border-radius: 0px 0px 5px 0px;
      box-shadow: 0px 0px 5px #171717;
    }
    html body main .sidebar-area .sidebar-content {
      height: 100%;
      width: 100%;
      padding-top: 30px;
    }
    html body main .sidebar-area .sidebar-content div i {
      font-size: 22px;
      color: rgba(229, 68, 68, 0.8156862745);
      text-shadow: 0px 0px 3px rgba(229, 68, 68, 0.8156862745);
      cursor: pointer;
      padding-right: 28px;
    }
    html body main .sidebar-area .sidebar-content .sidebar-title {
      padding-left: 23px;
      display: flex;
      align-items: center;
      -moz-column-gap: 50px;
           column-gap: 50px;
      width: 100%;
    }
    html body main .sidebar-area .sidebar-content .sidebar-title .title {
      color: #eee;
      font-weight: 500;
      text-shadow: 0px 0px 4px #ddd;
      cursor: pointer;
      transition: 0.4s;
      font-size: 28px;
    }
    html body main .sidebar-area .sidebar-content .sidebar-title .title:hover {
      transform: scale(1.05);
      transition: 0.4s;
    }
    html body main .sidebar-area .sidebar-content div .sidebar-menu {
      width: 100%;
      padding-top: 20px;
      list-style: none;
      color: #ddd;
      letter-spacing: 3px;
      padding: 2;
      font-weight: 500;
    }
    html body main .sidebar-area .sidebar-content div .sidebar-menu li {
      padding-left: 23px;
      cursor: pointer;
    }
    html body main .sidebar-area .sidebar-content div .sidebar-menu li:hover {
      background-color: rgba(56, 56, 56, 0.6823529412);
    }
    html body main .sidebar-area .sidebar-content div .sidebar-menu .contact-li {
      border-radius: 0px 0px 5px 0px;
    }
    html body main .sidebar-area .sidebar-content .social-links {
      padding: 30px 0px 30px 25px;
      display: inline-flex;
    }
    html body main .sidebar-area .sidebar-content .social-links i {
      text-shadow: 0px 0px 5px #ddd;
      color: #ddd;
      display: flex;
      width: 100%;
      transition: 0.4s;
    }
    html body main .sidebar-area .sidebar-content .social-links i:hover {
      transform: scale(1.05);
      transition: 0.4s;
    }
    html body main .main-content {
      width: 100%;
      height: -moz-max-content;
      height: max-content;
      padding: 25px;
    }
    html body main .active {
      display: block;
    }
    html body main .author {
      font-size: 25px;
      color: #ddd;
      position: absolute;
      right: 20px;
      bottom: 20px;
    }
    html body main .content {
      width: 100%;
    }
    html body main .content .fa-bars {
      font-size: 22px;
      float: right;
      margin: 30px;
      color: lightblue;
      text-shadow: 0px 0px 4px lightblue;
      cursor: pointer;
      transition: 0.2s;
      border: 1.3px solid lightblue;
      border-radius: 5px;
      padding: 5px 8px 5px 8px;
    }
    html body main .content .fa-bars:hover {
      border-color: grey;
      color: grey;
      transition: 0.2s;
    }
    html body main .body-area {
      height: 100vh;
      width: 100vw;
    }
    
    @media screen and (min-width: 901px) and (max-width: 9999px) {
      .sidebar-menu li {
        padding: 22px;
        font-size: 20px;
      }
    }
    @media screen and (max-width: 1000px) {
      .fa-x {
        display: none;
      }
      .sidebar-menu li {
        padding: 20px;
        font-size: 20px;
      }
    }/*# sourceMappingURL=sidebar.css.map */
    
    .sidebar-area {
        transform: translateX(-100%);
        transition: .5s all ease;
        opacity: 0;
        visibility: hidden;
        left: 0;
        top: 0;
    }
    
    .sidebar-area.active {
        transform: translateX(0px);
        transition: .5s all ease;
        opacity: 1;
        visibility: visible
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width,initial-scale=1.0">
      <meta name="author" content="">
      <meta name="description" content="">
      <meta name="keywords" content="">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <script src="https://kit.fontawesome.com/b367f4fd12.js" cr  ossorigin="anonymous"></script>
      <link rel="icon" href="">
      <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lato:wght@400;500;600;700;800&family=Open+Sans:wght@400;500;600;700;800&family=Poppins:wght@400;500;600;700;800&family=Quicksand:wght@400;500;600;700;800&family=Roboto:wght@400;500;600;700;800&family=Rubik:wght@400;500;600;700;800&display=swap">
      <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin="anonymous">
      <link rel="preconnect" href="https://fonts.googleapis.com">
      <link rel="stylesheet" href="sidebar.css">
      <title>Sidebar</title>
    </head>
    <body>
      <header></header>
      <main>
        <div class="sidebar-area">
          <div class="sidebar-content">
            <div class="sidebar-title">
              <div>
                <span class="title">Sidebar&nbsp;<span style="color: lightblue;">Project</span>   
              </div>
              <div>
                <i class="fa-solid fa-x"></i>
              </div>
            </div>
            <div>
              <ul class="sidebar-menu">
                <li>Home</li>
                <li>About</li>
                <li>Projects</li>
                <li class="contact-li">Contact</li>
              </ul>
            </div> 
            <div class="social-links">
              <i class="fa-brands fa-instagram"></i>
              <i class="fa-brands fa-twitter"></i>
              <i class="fa-brands fa-linkedin"></i>
              <i class="fa-brands fa-facebook"></i>
            </div>
          </div>
        </div>
        <div class="content">
          <i class="fa-solid fa-bars"></i>
        </div>
        <span class="author">Author: Maciek S.</span>
      </main>
      <footer></footer>
      <script src="sidebar.js"></script>
    </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search