使用CSS3让按钮弹跳

7
我正在尝试用CSS3使这个按钮弹跳。

.order {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 75px;
  line-height: 75px;
  text-align:center;
  opacity: 1;
  background: green;
  color:#fff;
  border-radius:50%;
}
<div class="order">Order</div>

我希望它在Z轴上向屏幕弹跳,上下移动。


尝试这个例子 https://jsfiddle.net/stanze/yqsayLq8/3/ - stanze
1
更新你的链接 - 它现在跳转到了404页面。 - Lee
4个回答

12

您可以使用关键帧动画来对比例尺进行动画处理,并使您的按钮弹起:

You can use a keyframe animation to animate the scale ratio and make your button bounce:


.order {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 75px;
  line-height: 75px;
  text-align:center;
  opacity: 1;
  background: green;
  color:#fff;
  border-radius:50%;
  animation: bounce .3s infinite alternate;
}
@keyframes bounce {
  to { transform: scale(1.2); }
}
<div class="order">Order</div>

迭代次数:

如果您想在一定数量的“反弹”后停止动画,则可以使用 animation-iteration-count(否则动画将在最后一次迭代时突然停止,建议使用偶数次迭代):

.order {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 75px;
  line-height: 75px;
  text-align:center;
  opacity: 1;
  background: green;
  color:#fff;
  border-radius:50%;
  animation: bounce .3s infinite alternate;
  animation-iteration-count: 8;
}
@keyframes bounce {
  to { transform: scale(1.2); }
}
<div class="order">Order</div>


1
尝试使用这个 CSS。
.order {
    background:url("http://onestudio.id-staging.com/_BUILD/Dominos/BANNERS/C3%20Digital%20Midweek%20Rescue/Wide%20Skyscraper/images/order.png");
      background-size: cover;
      position: absolute;
      top:50px;
      left:50px;
      width: 75px;
      height: 75px;
      z-index:1;
      opacity:1;

}

@keyframes fade {
    from { top:40px;
      left:40px;
      width: 100px;
      height: 100px; }
    50% { top:50px;
      left:50px;
      width: 75px;
      height: 75px; }
    to { top:40px;
      left:40px;
      width: 100px;
      height: 100px; }
}

@-webkit-keyframes fade {
    from { top:40px;
      left:40px;
      width: 100px;
      height: 100px; }
    50% { top:50px;
      left:50px;
      width: 75px;
      height: 75px; }
    to { top:40px;
      left:40px;
      width: 100px;
      height: 100px; }
}

.blink {
    animation:fade 1000ms infinite;
    -webkit-animation:fade 1000ms infinite;
}

尝试一下这个 HTML。
<div class="order blink"></div>

1

web-tiki发布的答案是最好使用的,但我有一个不同的方法,因为您已经使用了position:absolute

看看这个FIDDLE,您需要使用关键帧动画来动画化按钮的高度和宽度。

.order {
  background: url("http://onestudio.id-staging.com/_BUILD/Dominos/BANNERS/C3%20Digital%20Midweek%20Rescue/Wide%20Skyscraper/images/order.png") no-repeat;
  position: absolute;
  background-size: cover;
  top: 50px;
  left: 50px;
  width: 75px;
  height: 75px;
  z-index: 1;
  opacity: 1;
  -webkit-animation: mymove 1s infinite;
  /* Chrome, Safari, Opera */
  animation: mymove 1s infinite;
}
/* Chrome, Safari, Opera */

@-webkit-keyframes mymove {
  0% {
    height: 75px;
    width: 75px;
  }
  50% {
    height: 100px;
    width: 100px;
  }
  100% {
    height: 75px;
    width: 75px;
  }
}
/* Standard syntax */

@keyframes mymove {
  0% {
    height: 75px;
    width: 75px;
  }
  50% {
    height: 100px;
    width: 100px;
  }
  100% {
    height: 75px;
    width: 75px;
  }
}
<div class="order"></div>

编辑:

此外,你还可以使左和上移动38像素以使按钮不会看起来偏离原始位置。可以参考这个Fiddle

.order {
  background: url("http://onestudio.id-staging.com/_BUILD/Dominos/BANNERS/C3%20Digital%20Midweek%20Rescue/Wide%20Skyscraper/images/order.png") no-repeat;
  position: absolute;
  background-size: cover;
  top: 50px;
  left: 50px;
  width: 75px;
  height: 75px;
  z-index: 1;
  opacity: 1;
  -webkit-animation: mymove 1s infinite;
  /* Chrome, Safari, Opera */
  animation: mymove 0.5s 2;
}
/* Standard syntax */

@keyframes mymove {
  0% {
    height: 75px;
    width: 75px;
    left: 50px;
    top: 50px;
  }
  50% {
    height: 100px;
    width: 100px;
    left: 38px;
    top: 38px;
  }
  100% {
    height: 75px;
    width: 75px;
    left: 50px;
    top: 50px;
  }
}
<div class="order"></div>


0
你可以像下面这样使用动画(弹跳):
CSS:
.order {
    background:url("http://onestudio.id-staging.com/_BUILD/Dominos/BANNERS/C3%20Digital%20Midweek%20Rescue/Wide%20Skyscraper/images/order.png");
      position: absolute;
      top:50px;
      left:50px;
      width: 75px;
      height: 75px;
      z-index:1;
      opacity:1;
      animation: myfirst 2s infinite;
      -webkit-animation: myfirst 2s infinite; 
}

    @-webkit-@keyframes myfirst {

    0% {
    transform: scale(1);
  }
  50% {
   transform: scale(1.5);
  }
  100% {
   transform: scale(1);
  }
}

@keyframes myfirst {

    0% {
    transform: scale(1);
  }
  50% {
   transform: scale(1.5);
  }
  100% {
   transform: scale(1);
  }
}

请检查Fiddle


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接