单击时如何更改汉堡菜单图标



好吧,我有一个汉堡风格的菜单,它使用普通的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-在调用隐藏实例方法时激发。

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
});

相关内容

  • 没有找到相关文章

最新更新