簡體   English   中英

單擊時如何更改漢堡菜單圖標?

[英]How to change hamburger menu icon when clicked?

好的,我有一個使用普通 3 行圖標的漢堡包樣式菜單,但我喜歡它,因此當您單擊它時,圖標會變為十字。 我將如何實現這一目標? 我將如何構建 JavaScript?

HTML:

<ul class="navigation">
    <li class="nav-item"><a href="#">Home</a></li>
    <li class="nav-item"><a href="#">Meet the team</a></li>
    <li class="nav-item"><a href="#">Blog</a></li>
</ul>

<input type="checkbox" id="nav-trigger" class="nav-trigger" onclick="menuChange()" />
<label for="nav-trigger"></label>

CSS:

.navigation {
  /* critical sizing and position styles */
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 0;

  /* non-critical appearance styles */
  list-style: none;
  background: #000;
}

/* Navigation Menu - List items */
.nav-item {
  /* non-critical appearance styles */
  width: 200px;

}

.nav-item a {
  /* non-critical appearance styles */
  display: block;
  padding: 1em;
  background: linear-gradient(135deg, rgba(0,0,0,0) 0%,rgba(0,0,0,0.65) 100%);
  color: #999999;
  font-size: 1.2em;
  text-decoration: none;
  transition: color 0.2s, background 0.5s;
}

.nav-item a:hover {
  color: #ffffff;
}

/* Nav Trigger */
.nav-trigger {
  /* critical styles - hide the checkbox input */
  position: absolute;
  clip: rect(0, 0, 0, 0);
}

label[for="nav-trigger"] {
  /* critical positioning styles */
  position: fixed;
  left: 15px; top: 15px;
  z-index: 2;

  /* non-critical apperance styles */
  height: 30px;
  width: 30px;
  cursor: pointer;
  background-image: url(../images/Menu.png);
  background-size: contain;
}

/* Make the Magic Happen */
.nav-trigger + label, .site-wrap {
  transition: left 0.2s;
}

.nav-trigger:checked + label {
  left: 215px;
}

.nav-trigger:checked ~ .site-wrap {
  left: 200px;
  box-shadow: 0 0 5px 5px rgba(0,0,0,0.5);
}

一探究竟。

我認為更好的解決方案是使用折疊菜單中引起的事件而不是onclick

show.bs.collapse - 在調用 show 方法后觸發。

shown.bs.collapse - 將等待 CSS 轉換完成

hide.bs.collapse - 在調用 hide 實例方法時觸發。

hidden.bs.collapse - 將等待 CSS 轉換完成

代碼如下所示:

$('.navigation').on('show.bs.collapse', function() {
    // set cross as content of menu button
});

$('.navigation').on('hide.bs.collapse', function() {
    // set tree lines as content of menu button
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM