4

我正在进入 CSS3 动画和过渡的世界,所以请原谅我的无知。这是我正在尝试做的简化版本:

  • 我有一个通过 CSS3 关键帧无限“跳动”的球
  • 我希望球变大并在我悬停在它上面时保持这种状态
  • 当我将鼠标移开并保持脉动时,我希望球再次变小(当然,所有过渡都需要平滑)。

这是我使用 CSS3 动画和过渡的组合(到目前为止在 Chrome 上测试它,因此使用 webkit 前缀):

@-webkit-keyframes pulsate {
    from {
        -webkit-transform: scale(0.7);
    }    
    to {
        -webkit-transform: scale(0.8);
    }
}

.ball {
    background: blue;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    transition: all 1s;

    -webkit-transform: scale(0.7);
    -webkit-transform-origin: center center; 

    -webkit-animation-duration: 800ms;
    -webkit-animation-name: pulsate;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-direction: alternate;
    -webkit-animation-timing-function: ease-in-out;
}

.ball:hover {
    -webkit-transform: scale(2);
    -webkit-animation-play-state: paused; /* transition works but gets reset at the end*/
    /*-webkit-animation: 0;*/ /* transition works only one time, and no smooth transition on mouse out */
}

jsFiddle 演示

结果非常接近,但是一旦球在悬停时完成膨胀,它突然又变小了(不明白为什么)。我也尝试通过禁用动画-webkit-animation: 0;而不是暂停它,但它也不能很好地工作。

我尝试了一种仅使用关键帧(无过渡)的不同方法,方法是尝试在悬停时调用不同的关键帧集:

@-webkit-keyframes pulsate {
    from {
        -webkit-transform: scale(0.7);
    }    
    to {
        -webkit-transform: scale(0.8);
    }
}

@-webkit-keyframes expand {
    to {
        -webkit-transform: scale(2);
    }
}

@-webkit-keyframes shrink {
    to {
        -webkit-transform: scale(0.7);
    }
}

.ball {
    background: blue;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    transition: all 2s;

    -webkit-transform: scale(0.7);
    -webkit-transform-origin: center center; 

    -webkit-animation-duration: 800ms, 800ms;
    -webkit-animation-name: shrink, pulsate;
    -webkit-animation-iteration-count: 1, infinite;
    -webkit-animation-direction: normal, alternate;
    -webkit-animation-timing-function: ease-in-out, ease-in-out;
}

.ball:hover {
    -webkit-animation-name: expand;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-direction: normal;
    -webkit-animation-fill-mode: forwards;    
}

jsFiddle 演示

只要鼠标悬停在球上,球就会保持大,但是当鼠标离开球时仍然没有平滑的过渡。我希望它播放收缩动画,但事实并非如此。

我是否遗漏了什么,或者目前仅使用纯 CSS 是不可能实现的?

// 相关线程但对我不起作用:停止动画并在悬停时开始过渡

4

2 回答 2

3

您需要添加动画延迟以允许过渡完成,因为它会恢复到scale(.7)动画开始时。更新了 jsFiddle

-webkit-animation-delay:1s;

编辑

我意识到我在这里发布的答案并不完全正确。确实,延迟动画了从大到小的过渡,但是如果你将鼠标悬停在脉动球上,当它展开时它会跳回到它的 0 值 0.7,然后再进行大规模动画。

更新的演示

根据这篇文章,我想出了一个只使用一些 javascript 来修复它的修复程序。您确实必须稍微更改 CSS,但在结果中并不是很明显。这是更新的代码

/* CSS */
body {margin: 100px;}

@-webkit-keyframes pulsate {
    0% {
        -webkit-transform: scale(0.7);
    }    
    50% {
        -webkit-transform: scale(0.8);
    }
    100% {
        -webkit-transform: scale(0.7);
    }
}

.ball {
    background: blue;
    width: 100px;
    height: 100px;
    border-radius: 50%;    
    -webkit-transform: scale(0.7);
    -webkit-transform-origin: center center; 
    transition: all 1s;
}
.ball.animated {
  -webkit-animation-duration: 1600ms; 
  -webkit-animation-name: pulsate;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-direction: alternate;
  -webkit-animation-timing-function: ease-in-out;
}

/* Javascript */
var ball = document.getElementsByClassName('ball')[0],
    pfx = ["webkit", "moz", "MS", "o", ""],
    hovered = false;

function AnimationListener() {
    if(hovered)
    { 
      ball.classList.remove('animated');     
      ball.style.webkitTransform = 'scale(2)';
      ball.style.transform = 'scale(2)';
    }
}

function TransitionListener() {
  if(!hovered)
  {
    ball.classList.add('animated');
  }
}

function PrefixedEvent(element, type, callback) {
    for (var p = 0; p < pfx.length; p++) {
        if (!pfx[p]) type = type.toLowerCase();
        element.addEventListener(pfx[p]+type, callback, false);
    }
}

PrefixedEvent(ball, "AnimationIteration", AnimationListener);

ball.onmouseover = function() {
  hovered = true;
}
ball.onmouseout = function() {
  hovered = false;
  PrefixedEvent(ball, "TransitionEnd", TransitionListener);
  ball.style.webkitTransform = 'scale(.7)';
  ball.style.transform = 'scale(.7)';  
}
于 2013-08-20T14:12:56.133 回答
1

只需更新此 CSS 规则,我在 Expand & Shrink 中添加了 From & To -:

@-webkit-keyframes expand {
    from {
        -webkit-transform: scale(1);
    }    
    to {
        -webkit-transform: scale(2);
    }
}

@-webkit-keyframes shrink {
    from {
        -webkit-transform: scale(2);
    }    
    to {
        -webkit-transform: scale(1);
    }
}
于 2013-08-20T11:32:15.170 回答