javascript - 如何将 css 幻灯片过渡添加到 sidenav menu?

我有一个 sidenav menu,它在单击按钮时出现和消失。一切正常,但我不明白为什么无法过渡到 css。正如您在下面的示例中看到的那样,menu 横向出现和消失,但我想在 css 中添加一个过渡。我究竟做错了什么 ?

对不起,我是新手,正在学习。我感谢任何帮助。谢谢。

var menu = document.querySelector(".mob_menu_button");

function mobile_menu(e) {
  e.stopPropagation();
  var x = document.getElementById("mts_mobile_menu");
  if (!x.classList.contains("active")) {
    x.classList.add("active");
    x.classList.remove("hide");
    menu.innerHTML = "<span>Close Menu<span>";
  } else {
    x.classList.add("hide");
    menu.innerHTML = "<span>Open menu</span>";
  }
}

document.addEventListener("click", function(e) {
  var x = document.getElementById("mts_mobile_menu");
  if (e.target.id !== "mts_mobile_menu" && x.classList.contains("active")) {
    x.classList.add("hide");
    x.classList.remove("active");
    menu.innerHTML = "<span>Open menu</span>";
  }
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}


/*Items menu*/

.user_menu {
  display: flex;
  flex-direction: column;
}


/*Menu header info*/

.display.name {
  font-size: 15px;
  font-weight: 500;
  color: #303238;
}

.display.mail {
  font-size: 13px;
  color: #3d5afe;
}

hr.solid {
  border-top: 1px solid #e0e0e0;
  margin: 10px 0px 10px 0px;
}


/*Text Link css*/

.user_menu.item>a {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 8px 0;
  font-size: 13px;
  color: #75777d;
}

.user_menu.item:hover>a {
  color: #2e323a;
}


/*Icon Button Toggle Menu*/

.mob_menu_button {
  display: flex;
  align-content: flex-end;
  justify-content: center;
  align-items: flex-end;
  width: 20%;
  background: #282c33!important;
  font-weight: 500!important;
  position: absolute;
  top: 20px;
  right: 20px;
}

.icn_button {
  margin: 0;
  font-size: 14px;
}

.icn_button:before {
  margin: 0;
}

.icn_button:after {
  margin: 0;
}


/*Icon Items Menu*/

.icn_menu:before,
.icon_menu:after {
  margin: 0px;
  padding: 0px;
  font-size: 16px;
}

.icn_menu {
  margin-right: 10px;
  display: flex !important;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
}


/* User Menu For header website */

.mts_mob_container {
  display: flex;
  position: fixed;
  z-index: 999;
  top: 0;
  left: 0;
  width: 100%;
  background: #000000d6;
}

.mts_sidenav_box {
  display: block;
  width: 100%;
}

.mts_sidenav_content {
  display: none;
  padding: 20px;
  background-color: #fff;
  min-width: 160px;
  overflow-x: hidden;
  overflow-y: auto;
  z-index: 999;
  position: relative;
  width: 75%;
  height: 100vh;
}

.mts_sidenav_content.active {
  display: block!important;
  left: 0px;
}

.mts_sidenav_content.hide {
  left: -100%;
}
<button onclick="mobile_menu(event)" class="mob_menu_button">Open menu</button>

<div class="mts_mob_container">
  <div id="mts_mobile_menu" class="mts_sidenav_content">
    <div class="mts_sidenav_box">

      <div class="user_menu header">
        <span class="display name">Ciao [display_name]</span>
        <span class="display mail">[display_email]</span>
      </div>

      <hr class="solid" />

      <div class="user_menu item">
        <a href="/account">
          <i class="icn_menu fa-regular fa-user"></i>
          <span class="link_text">Dashboard</span>
        </a>
      </div>

      <div class="user_menu item">
        <a href="ordini">
          <i class="icn_menu fa-regular fa-basket-shopping"></i>
          <span class="link_text">I miei ordini</span>
        </a>
      </div>

      <div class="user_menu item">
        <a href="libreria">
          <i class="icn_menu fa-regular fa-cloud-arrow-down"></i>
          <span class="link_text">Downloads</span>
        </a>
      </div>

      <div class="user_menu item">
        <a href="impostazioni">
          <i class="icn_menu fa-regular fa-gear"></i>
          <span class="link_text">Impostazioni</span>
        </a>
      </div>

      <div class="user_menu item">
        <a href="wp-login.php?action=logout">
          <i class="icn_menu fa-regular fa-arrow-right-from-bracket"></i>
          <span class="link_text">Logout</span>
        </a>
      </div>
    </div>
  </div>
</div>

回答1

看起来您正在通过使用 display: nonedisplay: block 的活动类切换元素,您无法自行设置动画。

您可以使用 transform: translateX 和 opacity 在 menu 中滑动和淡入淡出,而不是显示。您还需要设置指针事件以防止在“隐藏”时意外点击。你可能需要 overflow: hidden 在父母/身体上。

.your-class {
    transition: all 0.5s ease;
    transform: translateX(300px);
    opacity: 0;
    pointer-events: none;
}

.your-class.active {
    transform: translateX(0);
    opacity: 1;
    pointer-events: all;
}

回答2

我找到了一个解决方案:我为此稍微更改了 Js 和 Css,我将工作代码留给遇到相同问题的任何人。

var menu = document.querySelector(".mob_menu_button");
      function mobile_menu(e) {
        e.stopPropagation();
        var x = document.getElementById("mts_mobile_menu");
        var y = document.getElementById("overlayx");
        
        // For var x
        if (!x.classList.contains("active")) {
          x.classList.toggle("active");
          menu.innerHTML = "<span>Close Menu</span>";
        } else {
          x.classList.remove("active");
          menu.innerHTML = "<span>Open Menu</span>";
        }
      // For var y
      if (!y.classList.contains("over")) {
          y.classList.toggle("over");
        } else {
          y.classList.remove("over");
        }
      }
      
     // Permette la chiusura del menu cliccando fuori dall'area
      document.addEventListener("click", function (e) {
        var x = document.getElementById("mts_mobile_menu");
        var y = document.getElementById("overlayx");
        
        // For var x
        if (e.target.id !== "mts_mobile_menu" && x.classList.contains("active")) {
          x.classList.toggle("active");
          menu.innerHTML = "<span>Open Menu</span>";
        }
        // For var y
        if (e.target.id !== "mts_mobile_menu" && y.classList.contains("over")) {
          y.classList.toggle("over");
        }
        
      });
/*Items menu*/
      .user_menu {
        display: flex;
        flex-direction: column;
      }

      /*Menu header info*/
      .display.name {
        font-size: 15px;
        font-weight: 500;
        color: #303238;
      }

      .display.mail {
        font-size: 13px;
        color: #3d5afe;
      }

      hr.solid {
        border-top: 1px solid #e0e0e0;
        margin: 10px 0px 10px 0px;
      }

      /*Text Link css*/
      .user_menu.item > a {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding: 8px 0;
        font-size: 13px;
        color: #75777d;
      }

      .user_menu.item:hover > a {
        color: #2e323a;
      }

      /*Icon Button Toggle Menu*/
    button.mob_menu_button {
       display: flex;
       align-content: center;
       justify-content: center;
       align-items: center;
       width: 20%;
       background: #282c33!important;
       color: #fff;
       font-weight: 500!important;
       top: 40px;
       right: 40px;
       position: absolute;
      }

      .mob_menu_button > span {
      z-index: 1000;
      }

      /*Icon Items Menu*/
      .icn_menu:before,
      .icon_menu:after {
        margin: 0px;
        padding: 0px;
        font-size: 16px;
      }

      .icn_menu {
        margin-right: 10px;
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 22px;
        height: 22px;
      }

      /* User Menu For header website */
      .mts_mob_container {
        display: flex;
        position: fixed;
        z-index: 999;
        top: 0;
        left: 0;
        width: 100%;
        background: #000000d6;
        opacity: 0;
        transition: 0.3s;
      }
      
       .mts_mob_container.over {
           opacity: 1;
       }
      
     .mts_sidenav_box {
        display: block;
        width: 100%;
      }

    .mts_sidenav_content {
        left: -100%;
        padding: 20px;
        background-color: #fff;
        min-width: 160px;
        overflow-x: hidden;
        overflow-y: auto;
        z-index: 999;
        position: relative;
        width: 75%;
        height: 100vh;
        transition: .3s;
      }

    .mts_sidenav_content.active {
    left: 0;
   }

    .mts_sidenav_content.hide {
    left: -100%;
      }
<button onclick="mobile_menu(event)" class="mob_menu_button"><span class="btn">Open Menu</span></button>

      <div id="overlayx" class="mts_mob_container">
         <div id="mts_mobile_menu" class="mts_sidenav_content"> 
         
          <div class="mts_sidenav_box">

            <div class="user_menu header">
              <span class="display name">Ciao [display_name]</span>
              <span class="display mail">[display_email]</span>
            </div>

            <hr class="solid" />

            <div class="user_menu item">
              <a href="/account">
                <i class="icn_menu fa-regular fa-user"></i>
                <span class="link_text">Dashboard</span>
              </a>
            </div>

            <div class="user_menu item">
              <a href="ordini">
                <i class="icn_menu fa-regular fa-basket-shopping"></i>
                <span class="link_text">I miei ordini</span>
              </a>
            </div>

            <div class="user_menu item">
              <a href="libreria">
                <i class="icn_menu fa-regular fa-cloud-arrow-down"></i>
                <span class="link_text">Downloads</span>
              </a>
            </div>

            <div class="user_menu item">
              <a href="impostazioni">
                <i class="icn_menu fa-regular fa-gear"></i>
                <span class="link_text">Impostazioni</span>
              </a>
            </div>

            <div class="user_menu item">
              <a href="wp-login.php?action=logout">
                <i class="icn_menu fa-regular fa-arrow-right-from-bracket"></i>
                <span class="link_text">Logout</span>
              </a>
            </div>
          </div>
        </div>
      </div>

相似文章

最新文章