将点击转换为悬停事件javascript [复制]

前端之家收集整理的这篇文章主要介绍了将点击转换为悬停事件javascript [复制]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > how to change click to mouseover or hover through jquery?4个
我试图在悬停汉堡棒时动画,我在网上找到了一个例子并设法让它在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,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,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相关文章