首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在悬停时创建反弹效果

问在悬停时创建反弹效果
EN

Stack Overflow用户
提问于 2012-11-28 16:52:10
回答 2查看 5.5K关注 0票数 0

我必须开发一个类似的网站,如http://www.unlocknrepair.com/在这个网站上,当你的鼠标悬停在解锁或手机维修按钮,一个下拉菜单出现。有没有办法让这个下拉列表以一种有弹性的方式出现?就像我想让它在稳定之前反弹一下。这在jQuery中是可能的,但是仅仅使用css和javascript就可以做到吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-28 17:38:31

如果可以选择实验性的css3,那么即使没有javascript,也可以使用css动画和@keyframes规则来实现。

​

代码语言:javascript
复制
#parent {
    position:relative;
     height: 40px;
}

#onhover {
    display: none;
    position: absolute;
    top: 0;
    
}

#parent:hover #onhover {
    display: block;
    top: 30px;
    animation:mymove 0.8s linear;
    -moz-animation:mymove 0.8s linear; /* Firefox */
    -webkit-animation:mymove 0.8s linear; /* Safari and Chrome */
    -o-animation:mymove 0.8s linear; /* Opera */
    -ms-animation:mymove 0.8s linear; /* IE */
}

@keyframes mymove
{
0%   {top:0px;}
10%  {top:3px;}
40%  {top:40px;}
60%  {top:25px;}
80%  {top:35px;}
100% {top:30px;}
}

@-moz-keyframes mymove /* Firefox */
{
0%   {top:0px;}
10%  {top:3px;}
40%  {top:40px;}
60%  {top:25px;}
80%  {top:35px;}
100% {top:30px;}
}

@-webkit-keyframes mymove /* Safari and Chrome */
{
0%   {top:0px;}
10%  {top:3px;}
40%  {top:40px;}
60%  {top:25px;}
80%  {top:35px;}
100% {top:30px;}
}

@-o-keyframes mymove /* Opera */
{
0%   {top:0px;}
10%  {top:3px;}
40%  {top:40px;}
60%  {top:25px;}
80%  {top:35px;}
100% {top:30px;}
}

@-ms-keyframes mymove /* IE */
{
0%   {top:0px;}
10%  {top:3px;}
40%  {top:40px;}
60%  {top:25px;}
80%  {top:35px;}
100% {top:30px;}
}
代码语言:javascript
复制
<div id="parent">hover me<div id="onhover">hovering</div></div>

​

另一个“反弹”动画:

​

代码语言:javascript
复制
$(function() {

$(document.body).delegate( "img", "mouseenter", function() {
    var $this = $(this).addClass("right");
    setTimeout(function() {
        $this.removeClass("right");
    }, 2000);
});
    
});
代码语言:javascript
复制
body { font-size: .7em; font-family: Arial, Helvetica, "Liberation Sans", sans-serif; padding: 0 !important; }
img {
    -moz-transition: -moz-transform 1s ease-in;
    -webkit-transition: -webkit-transform 1s ease-in;
    -o-transition: -o-transform 1s ease-in;
    -ms-transition: -ms-transform 1s ease-in;
}
#anim.right {
    -moz-animation-name: bounce;
    -moz-animation-duration: 1s;
    -moz-animation-iteration-count: 1;
    -moz-transform: translate(400px);
    -moz-transition: none;

    -webkit-animation-name: bounce;
    -webkit-animation-duration: 1s;
    -webkit-animation-iteration-count: 1;
    -webkit-transform: translate(400px);
    -webkit-transition: none;
}

@-moz-keyframes bounce {
  from {
    -moz-transform: translate(0px);
    -moz-animation-timing-function: ease-in;
  }
  60% {
    -moz-transform: translate(400px);
    -moz-animation-timing-function: ease-out;
  }
  73% {
    -moz-transform: translate(360px);
    -moz-animation-timing-function: ease-in;
  }
  86% {
    -moz-transform: translate(400px);
    -moz-animation-timing-function: ease-out;
  }
  93% {
    -moz-transform: translate(380px);
    -moz-animation-timing-function: ease-in;
  }
  to {
    -moz-transform: translate(400px);
    -moz-animation-timing-function: ease-out;
  }
}

@-webkit-keyframes bounce {
  from {
    -webkit-transform: translate(0px);
    -webkit-animation-timing-function: ease-in;
  }
  60% {
    -webkit-transform: translate(400px);
    -webkit-animation-timing-function: ease-out;
  }
  73% {
    -webkit-transform: translate(360px);
    -webkit-animation-timing-function: ease-in;
  }
  86% {
    -webkit-transform: translate(400px);
    -webkit-animation-timing-function: ease-out;
  }
  93% {
    -webkit-transform: translate(380px);
    -webkit-animation-timing-function: ease-in;
  }
  to {
    -webkit-transform: translate(400px);
    -webkit-animation-timing-function: ease-out;
  }
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<img id="anim" src="http://hacks.mozilla.org/wp-content/uploads/2011/04/75px-Aurora210.png" width="75" height="75" />

​

有关更多详细信息和浏览器兼容性,请参阅Mozilla Developer Network。

票数 1
EN

Stack Overflow用户

发布于 2012-11-28 17:03:30

是的,使用原生javascript是可能的。Take a look at this document

请注意,我链接到"easeOut“部分,因为我认为这代表了一个球的弹跳比他们的”弹跳“要好一点。

Here's a good example, further down the same page.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13601169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档