31.用 CSS 的动画原理,创作一个乒乓球对打动画

原文地址:https://segmentfault.com/a/1190000015002553

感想:纯属动画

HTML代码:

<div class="court">
    <div class="left-paddle"></div>
    <div class="ball"></div>
    <div class="right-paddle"></div>
</div>

CSS代码:

html, body {
    margin: 0;
    padding: 0;
    height: 100vh;
    display:flex;
    justify-content: center;
    align-items: center;
    /* silver: 银色; dimgray: 暗灰色 */
    background: linear-gradient(silver, dimgray);
}
/* 调整盒模型 */
*{
    box-sizing: border-box;
}
/* 画出球案 */
.court{
    position: relative;
    width: 20em;
    height: 20em;
    color: white;
    border: 1em solid currentColor;
}
.left-paddle,
.right-paddle {
    width: 1em;
    height: calc(50% - 1em);
    background-color: currentColor;
    position: absolute;
    animation: 1s linear infinite alternate;
}
/* 画出左拍 */
.left-paddle{
    top: 1em;
    left: 1em;
    animation-name: left-moving;
}
@keyframes left-moving{
    to{
        transform: translateY(100%);
    }
}
.right-paddle{
    bottom: 1em;
    right: 1em;
    animation-name: right-moving;
}
@keyframes right-moving {
    to {
        transform: translateY(-100%);
    }
}
/* 画出小球 */
.ball{
    position: absolute;
    left: 2em;
    top: calc(50% - 1.5em);
    width: 100%;
    height: 1em;
    border-left: 1em solid currentColor;
    animation: bounce 1s linear infinite alternate;
}
@keyframes bounce{
    to{
        left: calc(100% - 3em);
    }
}

 

posted on 2019-01-30 01:18  人生与戏 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/FlyingLiao/p/10336537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值