在浏览器中使CSS3动画更可靠

时间:2014-12-18 20:45:59

标签: html css3 animation

我将这个CSS3动画用于编写代码。

HTML

<div class="heart heart1"></div>
<div class="heart heart2"></div>

CSS3

html, body{
  width: 100%;
  height: 100%;
  min-width: 500px;
  min-height: 500px;
  overflow: hidden;
}
.heart {
  position: absolute;
  width: 100px;
  height: 90px;
  top: 50%;
  left: 50%;
  margin-top: -45px;
  margin-left: -50px;
}
.heart:before,
.heart:after {
  position: absolute;
  content: "";
  left: 50px;
  top: 0;
  width: 50px;
  height: 80px;
  background: #fc2e5a;
  border-radius: 50px 50px 0 0;
  transform: rotate(-45deg);
  transform-origin: 0 100%;
}
.heart:after {
  left: 0;
  transform: rotate(45deg);
  transform-origin :100% 100%;
}
.heart1{
   animation: heart-anim 1s linear .4s infinite;
}
.heart2{
   animation: pounding .5s linear infinite alternate;
}
.heart1:after, .heart1:before{
  background-color: #ff7693;
}

@keyframes pounding{
  0%{ transform: scale(1.5); }
  100%{ transform: scale(1); }
}
@keyframes heart-anim {
  46% {

    transform: scale(1);
  }
  50% {
    transform: scale(1.3);
  }
  52% {
    transform: scale(1.5);
  }
  55% {
    transform: scale(3);
  }
  100% {
    opacity: 0;
    transform: scale(50);
  }
}

在此处查看:http://codepen.io/RadValentin/pen/sfnCE

正如你所看到的那样工作正常,但是,如果我将确切的代码发布到我的本地服务器或jsfiddle,它将不再起作用:http://jsfiddle.net/40aydbfr/

我认为动画不是根据最佳实践制作的,因为它很容易打破。

那么,为什么它不能在codepen之外工作,如何让它更兼容浏览器兼容性。

PS:我正在使用Chrome。

1 个答案:

答案 0 :(得分:1)

它不起作用,因为您缺少-webkit-浏览器的供应商前缀。

它在codepen上工作的原因是,如果你单击CSS窗口上方的设置按钮,你会看到 -prefix-free 已启用,这意味着它会添加前缀自动。

Always check browser support, if something doesn't work

<强> Updated Codepen

<强> Updated Fiddle

html,
body {
  width: 100%;
  height: 100%;
  min-width: 500px;
  min-height: 500px;
  overflow: hidden;
}
.heart {
  position: absolute;
  width: 100px;
  height: 90px;
  top: 50%;
  left: 50%;
  margin-top: -45px;
  margin-left: -50px;
}
.heart:before,
.heart:after {
  position: absolute;
  content: "";
  left: 50px;
  top: 0;
  width: 50px;
  height: 80px;
  background: #fc2e5a;
  border-radius: 50px 50px 0 0;
  transform: rotate(-45deg);
  transform-origin: 0 100%;
}
.heart:after {
  left: 0;
  transform: rotate(45deg);
  transform-origin: 100% 100%;
}
.heart1 {
  -webkit-animation: heart-anim 1s linear .4s infinite;
  animation: heart-anim 1s linear .4s infinite;
}
.heart2 {
  -webkit-animation: pounding .5s linear infinite alternate;
  animation: pounding .5s linear infinite alternate;
}
.heart1:after,
.heart1:before {
  background-color: #ff7693;
}
@-webkit-keyframes pounding {
  0% {
    transform: scale(1.5);
  }
  100% {
    transform: scale(1);
  }
}
@keyframes pounding {
  0% {
    transform: scale(1.5);
  }
  100% {
    transform: scale(1);
  }
}
@-webkit-keyframes heart-anim {
  46% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.3);
  }
  52% {
    transform: scale(1.5);
  }
  55% {
    transform: scale(3);
  }
  100% {
    opacity: 0;
    transform: scale(50);
  }
}
@keyframes heart-anim {
  46% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.3);
  }
  52% {
    transform: scale(1.5);
  }
  55% {
    transform: scale(3);
  }
  100% {
    opacity: 0;
    transform: scale(50);
  }
}
<div class="heart heart1"></div>
<div class="heart heart2"></div>