【问题标题】:CSS3 Keyframe Animation on page load only?仅页面加载时的 CSS3 关键帧动画?
【发布时间】:2017-11-15 05:00:01
【问题描述】:

我这里有一个配置文件按钮,它可以在其中进行动画处理,并在悬停时执行不同的动画。然而,当我的鼠标离开元素时,入口动画会重复。有没有办法确保每次页面加载只触发一次动画?

.profile {
width: 125px;
height: 125px;
background-image: url(graphics/profile1.jpg);
border-style: solid;
border-color: white;
border-radius: 50%;
margin-left: 80px;
outline: 1px solid transparent;
float: left;
animation: popIn 1s;
}

.profile:hover{
width: 125px;
height: 125px;
border-style: solid;
border-color: red;
box-shadow: 0 0 0 0 rgba(155, 0, 2, 0.6);
border-radius: 50%;
-webkit-animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
-moz-animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
-ms-animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
}

@keyframes popIn{
0% {
transform: scale(0.1);
opacity: 0;
}
60% {
transform: scale(1.2);
opacity: 1;
}
100% {
transform: scale(1);
}
}

@-webkit-keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}
@-moz-keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}
@-ms-keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}
@keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}

【问题讨论】:

标签: html css animation


【解决方案1】:

这是一个类似问题的链接,可能会对您有所帮助。

Run CSS3 animation only once (at page loading)

我个人会在 div 中添加一个额外的类来处理所有入口动画,然后在光标在 div 内移动时使用 javascript 删除该类。我会做一个 js fiddle 给你看一个例子,给我几分钟。

修改小提琴链接:https://jsfiddle.net/0uaktv10/

html:

<div class="profile load-in" style="background: blue; width: 100px; height: 100px;"></div>

CSS:

    enter .profile {
width: 125px;
height: 125px;
background-image: url(graphics/profile1.jpg);
border-style: solid;
border-color: white;
border-radius: 50%;
margin-left: 80px;
outline: 1px solid transparent;
float: left;
}

.load-in {
  animation: popIn 1s;
}

.profile:hover{
width: 125px;
height: 125px;
border-style: solid;
border-color: red;
box-shadow: 0 0 0 0 rgba(155, 0, 2, 0.6);
border-radius: 50%;
-webkit-animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
-moz-animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
-ms-animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
animation: pulse 1.25s infinite cubic-bezier(0.66, 0, 0, 1);
}
@keyframes popIn{
0% {
transform: scale(0.1);
opacity: 0;
}
60% {
transform: scale(1.2);
opacity: 1;
}
100% {
transform: scale(1);
}
}

@-webkit-keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}
@-moz-keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}
@-ms-keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}
@keyframes pulse {to {box-shadow: 0 0 0 45px rgba(232, 76, 61, 0);}}

jQuery:

$(".profile").hover(function(){
    $(".profile").removeClass("load-in")
})

【讨论】:

  • 完美的解释谢谢,我的所有按钮现在都按我的意愿工作了,非常感谢!
猜你喜欢
  • 2012-12-22
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2023-03-18
相关资源
最近更新 更多