简体   繁体   中英

Transform: TranslateY with Javascript

So i know this is so easy for most of people here but i'm learning and i realy hope someone can help me;

I'm making a responsive nav for my website so i want to click the menu buttom so the menu drops from top of the screen. So i did the transform: translateY(-100%), but i have a function to make my menu (classic 3 lines) to turn into an "X" so what can i add to this function to make at the same time the menu appears?. And i don't know why but the transition from 3 hirizontal lines to X works nice but from X to the 3 lines is not working the same way.

DOM

<div id="mobile_menu">
    <div class="mobile_menu_lines1"></div>
    <div class="mobile_menu_lines2"></div>
    <div class="mobile_menu_lines3"></div>
  </div>
  <nav id="mobile_nav">
      <ul id="mobile_links">
        <li><a href="">INICIO</a></li>
        <li><a href="">PORTAFOLIO</a></li>
        <li><a href="">NOSOTROS</a></li>
        <li><a href="">CONTACTO</a></li>
      </ul>
      <ul id="mobile_links_social">
          <li><a href=""><img src="assets/img/svgs/facebook_icon.svg" alt="Facebook logo"/></a></li>
          <li><a href=""><img src="assets/img/svgs/twitter_icon.svg" alt="Twitter Logo"/></a></li>
          <li><a href=""><img src="assets/img/svgs/instagram_icon.svg" alt="Instagram logo"/></a></li>
      </ul>
  </nav>

CSS

#mobile_menu{
    position: absolute;
    display: block;
    width: 50px;
    height: 60px;
    float: right;
    right: 5px;
    cursor: pointer;
    transition: all 0.3s ease-in-out;
  }

  .mobile_menu_lines1{
    max-width: 70%;
    align-items: center;
    height: 2px;
    background-color: white;
    margin-top: 18px;
    border-radius: 2px;
  }

  .mobile_menu_lines2, .mobile_menu_lines3{
    max-width: 70%;
    align-items: center;
    height: 2px;
    background-color: white;
    margin-top: 9px;
    border-radius: 2px;
  }

  .show .mobile_menu_lines1 {
    transform: rotate(45deg) translate(11px, 5px);
    transition: all 0.3s ease-in-out;
  }

  .show .mobile_menu_lines2 {
    opacity: 0;
    transition: all 0.3s ease-in-out;
  }

  .show .mobile_menu_lines3 {
    transform: rotate(-45deg) translate(10px, -5px);
    transition: all 0.3s ease-in-out;
  }

  #mobile_nav{
    display: block;
    text-align: center;
    padding-top:60px;
    background-color: rgba(0, 0, 0, 0.7);
    width: 100%;
    transform: translateY(-100%);
    transition: all 0.3s ease-in-out;
  }

  #mobile_nav #mobile_links li{
    padding: 15px 0;
  }

  #mobile_nav #mobile_links li a{
    list-style: none;
    text-decoration: none;
    color: white;
    font-family: "josefin sans";
    font-size: 12px;
    letter-spacing: 7px;
    align-items: center;
    text-align: center;
    padding: 15px 0;
    width:600px;
    max-width: 100%
  }

  #mobile_nav #mobile_links_social li{
    padding: 10px 20px;
  }

JS

var mobileNavMostrar = document.getElementById('mobile_menu');
mobileNavMostrar.addEventListener('click',function(){
  mobileNavMostrar.classList.toggle('show');
})

Thanks in advance.

我尝试了您的代码,但我认为您缺少两件事-用于定义mobile_nav在显示时应如何更改的CSS,以及用于在单击时应用该CSS的JavaScript。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM