javascript - 动画背景颜色,脉冲效果

标签 javascript jquery html css jquery-animate

我制作了一个脚本,用于在将鼠标悬停在其中的 a 元素后为我的菜单 li 元素设置动画。

一切正常,但我想要别的东西。我希望效果不会消失,而是只要鼠标悬停在 a 元素上就会一直存在。

使用什么功能?

到目前为止的脚本:

 jQuery(document).ready(function($){
        $(".main-navigation a").mouseover(function() {
        $(this).parent().animate({
    backgroundColor: "green"
    }, "normal"),
    $(this).parent().animate({
    backgroundColor: "transparent"
    })
    .mouseleave(function() {
    $(this).parent().animate({
    backgroundColor: "transparent"
    }, "normal")
    });
    });
});

http://jsfiddle.net/neugu8r9/

最佳答案

您可以在没有 jQuery 的情况下使用 CSS 的 @keyframes 来做到这一点。

ul {
  position: relative;
  width: 250px;
  height: 50px;
  padding: 0;
  margin: 0;
  list-style: none;
}
li a {
  width: 100%;
  height: 100%;
  line-height: 50px;
  text-align: center;
}
li a:before {
  position: absolute;
  content: '';
  width: 100%;
  height: 100%;
  display: block;
  z-index: -1;
}
li {
  position: relative;
  height: 50px;
  width: 250px;
  text-align: center;
  border: 1px solid black;
}
a:hover:before {
  -webkit-animation: pulse 0.8s ease-in-out infinite alternate;
  animation: pulse 0.8s ease-in-out infinite alternate;
}
@-webkit-keyframes pulse {
  0% {
    background: transparent;
  }
  50% {
    background: green;
  }
  100% {
    background: transparent;
  }
}
@keyframes pulse {
  0% {
    background: transparent;
  }
  50% {
    background: green;
  }
  100% {
    background: transparent;
  }
}
<nav class="main-navigation">
  <ul>
    <li><a>Menu-item#1</a></li>
    <li><a>Menu-item#2</a></li>
    <li><a>Menu-item#3</a></li>
    <li><a>Menu-item#4</a></li>
  </ul>
</nav>

关于javascript - 动画背景颜色,脉冲效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28079770/

相关文章:

html - 列表项的高度

javascript - 如何从 URL 正文获取内部文本

javascript - 将应用程序代码与 vendor 代码合并有哪些好处?

javascript - 当我将 KnockoutJS 导入我正在测试的 ViewModel 时,为什么 Jest 错误超过最大调用堆栈大小?

jquery - Bootstrap 移动端菜单瞬间塌陷回去

jquery - 如何使用(jQuery 风格)嵌套对象序列化 GET 参数

android - 如何搜索 HTML 中的内容而不是标签

javascript - 使用 onclick javascript 更改元素的类

javascript - 使用脚本添加两个同名的数组值

jquery - 在 Iphone Safari 浏览器上,在文本框内输入时整个页面跳转?