Vanilla JS setTimeout():辨識錯誤行為
P粉797855790
P粉797855790 2024-04-02 20:44:40
0
1
418

我的漢堡式選單上有一個用 HTML、CSS 和 Vanilla JavaScript 寫的動畫。

預期結果

當您點擊漢堡選單按鈕時,應該發生以下事件:

點擊漢堡選單圖示時:

  1. main 旋轉 180° 並在 2000ms 上沿高度縮小
  2. 2000ms 通過後,選單出現,漢堡圖示變成 X

點擊退出按鈕時:

  1. 將頁面再旋轉 180° 覆蓋選單
  2. 隱藏選單:display: none;
  3. 將退出按鈕轉換回漢堡圖示

實際發生的情況

主要問題是由於第 12-14 行的 settimeout 而無法退出按鈕。 這是為了更好地理解的程式碼片段。

var exitButton = document.getElementById("exit-button");


var exitButtonOnClick = function() {
  mobileMenu.classList.add("hidden");
}
var mobileMenuButton = document.getElementById("mobile-menu-enter");
var mobileMenu = document.getElementById("mobile-menu-id");
var mainContent = document.getElementById("main-content")

var mobileMenuButtonOnClick = function() {
  mainContent.classList.toggle("moved-away")
  setTimeout(function() {
    mobileMenu.classList.toggle("hidden");
  }, 2000);




  if (mobileMenu.classList.contains("hidden")) {
    mobileMenuButton.innerHTML = "<div class='line'></div><br><div class='line'></div><br><div class='line'></div>";

  }


};

mobileMenuButton.addEventListener("click", mobileMenuButtonOnClick);
* {
  margin: 0;
  box-sizing: border-box
}

body {
  overflow-x: hidden;
  font-size: large;
  margin: 0;
}

main {
  z-index: 99;
}

.mr-0 {
  margin-right: 0;
}

.ml-auto {
  margin-left: auto;
}

.d-block {
  display: block;
}

.nav-bar {
  z-index: 99;
  background-color: rgba(204, 204, 204, 0.9);
  padding: 10px;
  position: sticky;
  top: 0;
  height: 110px !important;
}

.nav-img {
  height: 150px;
  position: relative;
  bottom: 30px;
}

.nav-options {
  float: right;
  padding-right: 50px;
  position: relative;
  top: 15px;
}

.hidden {
  display: none !important;
  opacity: 0;
  transition: all 3000ms ease-in-out;
}

.line {
  width: 50px;
  background-color: white;
  z-index: 99;
  height: 0.5px;
}

.moved-away {
  transform: rotate(180deg);
  height: 0 !important;
  transition: all 2000ms ease-in-out;
}

.mobile-nav {
  position: sticky;
  top: 0;
}

.hamburger-menu {
  background-color: transparent;
  border: none;
  position: relative;
  left: 50px;
  z-index: 99;
  top: 10px;
}

.mobile-menu {
  display: flex;
  justify-content: center;
  padding-right: 40px !important;
  min-height: fit-content !important;
  line-height: 20vh;
  background-image: url("/resources/img_5.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  width: 100%;
  height: 84vh;
  position: absolute;
  top: 95px;
  right: 0;
}

.mobile-menu li {
  width: 100%;
  display: inline-block;
}

.mobile-options {
  position: relative;
  top: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.mobile-option {
  width: 90vw;
  height: 30px;
  font-family: 'Montserrat' sans-serif;
  display: inline-block;
  background-color: rgba(204, 204, 204, 0.8);
  color: black;
  border: none;
  border-radius: 15px;
  font-size: large;
  text-align: center;
}

.exit-btn {
  min-width: 50px;
  background-color: transparent;
  border: none;
  font-size: 4rem;
  color: white;
  font-family: 'Montserrat', sans-serif;
  font-weight: lighter;
  position: relative !important;
  bottom: 20px !important;
  z-index: 999999 !important;
}
<div class="nav-bar">
  <nav class="mobile-nav">
    <a href="index.html"><img src="/resources/Copy of The Coders Club.png" class="nav-img"></a>
    <div class="nav-options">
      <button class="d-block mr-0 ml-auto hamburger-menu" id="mobile-menu-enter">
                  <div class="line"></div><br>
                   <div class="line"></div><br>
                  <div class="line"></div>
                </button>
    </div>
  </nav>

  <div class="mobile-menu hidden" id="mobile-menu-id">
    <ul class="mobile-options">
      <a href="/about.html">
        <li><button class="mobile-option">About</button></li>
      </a>
      <a href="/classes.html">
        <li><button class="mobile-option" style="margin-top: 15%; margin-bottom: 15%;">Classes</button></li>
      </a>
      <a href="/contact.html">
        <li><button class="mobile-option">Contact</button></li>
      </a>
    </ul>
  </div>
</div>
<main id="main-content">
  Some content. Lorem ipsum... Hello, World!

</main>

P粉797855790
P粉797855790

全部回覆(1)
P粉032900484

如果你的意思是退出點擊只是恢復原始狀態,沒有動畫,那是因為你已經在.move-away 類別上定義了transition 屬性,並且在退出點擊時立即將其刪除。

您應該將此transition: all 2000ms escape-in​​-out; 移至main 樣式(CSS 檔案中的第14 行)以使動畫雙向工作

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板