单击时使用“切换"按钮更改图标. [英] Change icon on clicking using a "toggle"

查看:55
本文介绍了单击时使用“切换"按钮更改图标.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有与此处相同的HTML,CSS和Javascript:如何关闭单击此菜单后.请让我知道如何在单击并打开topnav导航栏后将汉堡包图标更改为关闭(x)图标(不使用图标旋转).

I have the same HTML, CSS and Javascript as here: How to close this menu after click Please let me know how to change the hamburger icon to a close (x) icon (without using rotation of the icon) after clicking and opening the topnav Nav bar.

推荐答案

喜欢吗?

$(function() {
  $('.links').on("click", function() {
    $("#myTopnav").removeClass("responsive");
  });

  $("#toggle").on("click", function() {
    const $nav = $("#myTopnav");
    $nav.toggleClass("responsive");
    $(this).html(
      $nav.hasClass("responsive") ?  "×" : "☰"
    )
  });

});

ul.topnav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

ul.topnav li {
  float: left;
}

ul.topnav li a {
  display: inline-block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  transition: 0.3s;
  font-size: 17px;
}

ul.topnav li a:hover {
  background-color: #555;
}

ul.topnav li.icon {
  display: none;
}

@media screen and (max-width:1180px) {
  ul.topnav li:not(:first-child) {
    display: none;
  }
  ul.topnav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.topnav.responsive {
    position: relative;
  }
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="topnav" id="myTopnav">
  <li class="links"><a href="#home">Home</a></li>
  <li class="links"><a href="#news">News</a></li>
  <li class="links"><a href="#contact">Contact</a></li>
  <li class="links"><a href="#about">About</a></li>
  <li class="icon">
    <a href="#" id="toggle">&#9776;</a>
  </li>
</ul>

这篇关于单击时使用“切换"按钮更改图标.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆