CSS3干货9:做一个弹跳动画

先看效果

步骤1:准备一个 div 标签

<div class="ball">
</div>

分析一下:

这个效果上有两个内容:球 , 影子。

球是这个div的 ::before 伪标签,影子 是这个球的 ::after 伪标签。

步骤2:制作小球和影子

 *{
            margin: 0;
            padding: 0;
 }
.ball{
            width: 200px;
            height: 200px;
            background: #eee;
            margin-left: auto;
            margin-right: auto;
            position: relative;   /* 绝对定位,让小球和影子可以任意在范围内定位 */
}
.ball:before{
            content: ""; 
            width: 100px;
            height: 100px;
            position: absolute;
            border-radius: 100%;
            bottom: 0;

            left:50%;    /* 让小球居中 */
            margin-left: -50px;

            background: #cb2b36;
            z-index: 2;   /* 让小球能盖住影子 */
 }
.ball:after{
            content: "";
            width: 70px;   /* 影子要扁点 */
            height: 5px;
            background: #000;
            position: absolute;
            border-radius: 100%;

            bottom: 0;
            left:50%;   /* 让影子居中 */
            margin-left: -35px;
            filter:blur(3px);   /* 影子模糊 */
            z-index: 1; 
}

步骤3:让小球和影子动起来

小球上下移动,影子放大缩小且做透明度变化。

两个物体的动画都是反复执行,且次数不限。

/* 接前面的代码 */

/* 分别定义动画  */
@keyframes ballUpAndDown {
    0%{
        transform: translateY(0);
    }
    100%{
        transform: translateY(-40px);
    }
}

@keyframes shadowAni {
    0%{
        transform: scale(1,1);
        opacity: 1;
    }
    100%{
        transform: scale(1.2,1.2);
        opacity: 0.5;
    }
}

.ball:before{
   animation: ballUpAndDown 0.5s infinite alternate;
}
.ball:after{
   animation: shadowAni 0.5s infinite alternate;
}

完工。

这个动画可以用在什么地方呢?暂时想不到,不过可以从这个案例中了解:

1. CSS3动画

2. 伪标签的使用

能学到知识,才是重要的~

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值