javascript - 将点击转换为悬停事件javascript

标签 javascript jquery html css

<分区>

我正在尝试在悬停汉堡吧时制作动画,我在网上找到了一个示例并设法让它在 mouseenter 上运行,但我希望它在鼠标离开汉堡吧后返回汉堡吧。

这是代码,您可以看到 mouseenter 有效,但是当我将鼠标移开时,我希望它回到汉堡栏而不是保持为 X。

(function() {"use strict";
  var toggles = document.querySelectorAll(".c-hamburger");
  for (var i = toggles.length - 1; i >= 0; i--) {
    var toggle = toggles[i];
    toggleHandler(toggle);
  };
  function toggleHandler(toggle) {
    toggle.addEventListener("mouseenter", function(e) {
      e.preventDefault();
      (this.classList.contains("is-active") === true) ? this.classList.remove("is-active"): this.classList.add("is-active");
    });
  }
             
})();
.c-hamburger {
  display: block;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
  width: 66px;
  height: 55px;
  font-size: 0;
  text-indent: -9999px;
  appearance: none;
  box-shadow: none;
  border-radius: none;
  border: none;
  cursor: pointer;
  transition: background 0.3s;
}

.c-hamburger:focus {
  outline: none;
}

.c-hamburger span {
  display: block;
  position: absolute;
  left: 18px;
  right: 18px;
  height: 2px;
  background: black;
}

.c-hamburger span::before,
.c-hamburger span::after {
  position: absolute;
  display: block;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: black;
  content: "";
}

.c-hamburger span::before {
  top: -10px;
}

.c-hamburger span::after {
  bottom: -10px;
}
.c-hamburger--htx {
  background-color: white;
}

.c-hamburger--htx span {
  transition: background 0s 0.3s;
}

.c-hamburger--htx span::before,
.c-hamburger--htx span::after {
  transition-duration: 0.3s, 0.3s;
  transition-delay: 0.3s, 0s;
}

.c-hamburger--htx span::before {
  transition-property: top, transform;
}

.c-hamburger--htx span::after {
  transition-property: bottom, transform;
}

/* active state, i.e. menu open */
.c-hamburger--htx.is-active {
  background-color: white;
}

.c-hamburger--htx.is-active span {
  background: none;
}

.c-hamburger--htx.is-active span::before {
  top: 0;
  transform: rotate(45deg);
}

.c-hamburger--htx.is-active span::after {
  bottom: 0;
  transform: rotate(-45deg);
}

.c-hamburger--htx.is-active span::before,
.c-hamburger--htx.is-active span::after {
  transition-delay: 0s, 0.3s;
}
<button class="c-hamburger c-hamburger--htx">
  <span>toggle menu</span>
</button>

最佳答案

您可以在 mouseenter 上添加类,在 mouseleave 上删除它

(function() {"use strict";
  var toggles = document.querySelectorAll(".c-hamburger");
  for (var i = toggles.length - 1; i >= 0; i--) {
    var toggle = toggles[i];
    toggleHandler(toggle);
  };
  function toggleHandler(toggle) {
    toggle.addEventListener("mouseenter", function(e) {
      e.preventDefault();
      this.classList.add('is-active');
    })
    toggle.addEventListener('mouseleave',function(e) {
      this.classList.remove('is-active');
    });
  }
             
})();
.c-hamburger {
  display: block;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
  width: 66px;
  height: 55px;
  font-size: 0;
  text-indent: -9999px;
  appearance: none;
  box-shadow: none;
  border-radius: none;
  border: none;
  cursor: pointer;
  transition: background 0.3s;
}

.c-hamburger:focus {
  outline: none;
}

.c-hamburger span {
  display: block;
  position: absolute;
  left: 18px;
  right: 18px;
  height: 2px;
  background: black;
}

.c-hamburger span::before,
.c-hamburger span::after {
  position: absolute;
  display: block;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: black;
  content: "";
}

.c-hamburger span::before {
  top: -10px;
}

.c-hamburger span::after {
  bottom: -10px;
}
.c-hamburger--htx {
  background-color: white;
}

.c-hamburger--htx span {
  transition: background 0s 0.3s;
}

.c-hamburger--htx span::before,
.c-hamburger--htx span::after {
  transition-duration: 0.3s, 0.3s;
  transition-delay: 0.3s, 0s;
}

.c-hamburger--htx span::before {
  transition-property: top, transform;
}

.c-hamburger--htx span::after {
  transition-property: bottom, transform;
}

/* active state, i.e. menu open */
.c-hamburger--htx.is-active {
  background-color: white;
}

.c-hamburger--htx.is-active span {
  background: none;
}

.c-hamburger--htx.is-active span::before {
  top: 0;
  transform: rotate(45deg);
}

.c-hamburger--htx.is-active span::after {
  bottom: 0;
  transform: rotate(-45deg);
}

.c-hamburger--htx.is-active span::before,
.c-hamburger--htx.is-active span::after {
  transition-delay: 0s, 0.3s;
}
<button class="c-hamburger c-hamburger--htx">
  <span>toggle menu</span>
</button>

您也可以仅使用 CSS 来完成此操作。

.c-hamburger {
  display: block;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
  width: 66px;
  height: 55px;
  font-size: 0;
  text-indent: -9999px;
  appearance: none;
  box-shadow: none;
  border-radius: none;
  border: none;
  cursor: pointer;
  transition: background 0.3s;
}

.c-hamburger:focus {
  outline: none;
}

.c-hamburger span {
  display: block;
  position: absolute;
  left: 18px;
  right: 18px;
  height: 2px;
  background: black;
}

.c-hamburger span::before,
.c-hamburger span::after {
  position: absolute;
  display: block;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: black;
  content: "";
}

.c-hamburger span::before {
  top: -10px;
}

.c-hamburger span::after {
  bottom: -10px;
}
.c-hamburger--htx {
  background-color: white;
}

.c-hamburger--htx span {
  transition: background 0s 0.3s;
}

.c-hamburger--htx span::before,
.c-hamburger--htx span::after {
  transition-duration: 0.3s, 0.3s;
  transition-delay: 0.3s, 0s;
}

.c-hamburger--htx span::before {
  transition-property: top, transform;
}

.c-hamburger--htx span::after {
  transition-property: bottom, transform;
}

/* active state, i.e. menu open */
.c-hamburger--htx.is-active {
  background-color: white;
}

.c-hamburger--htx:hover span {
  background: none;
}

.c-hamburger--htx:hover span::before {
  top: 0;
  transform: rotate(45deg);
}

.c-hamburger--htx:hover span::after {
  bottom: 0;
  transform: rotate(-45deg);
}

.c-hamburger--htx:hover span::before,
.c-hamburger--htx:hover span::after {
  transition-delay: 0s, 0.3s;
}
<button class="c-hamburger c-hamburger--htx">
  <span>toggle menu</span>
</button>

关于javascript - 将点击转换为悬停事件javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42306165/

相关文章:

html - 如何使我的网格模板行适合内容?

javascript - Cloud Code (parse.com) 中的关系查询

javascript - 使用javascript触发输入框的pattern属性?

javascript - Bootstrap Navbar Make Brand 和 Navbar-Right "Stick"到网页的一侧

jquery - 单选按钮在 jquery formbuilder 中不起作用

javascript - fullpage.js 只会在窗口调整大小后滚动溢出

html - CSS:背面可见性不起作用?

javascript - 未通过添加 ajax 逻辑定义函数

javascript - 如何禁用我的网站记住密码?

javascript - CKEditor - 检测按下了哪些按钮