您好,欢迎来到皓特汽车网。
搜索
您的当前位置:首页使用CSS实现乒乓球对打动画

使用CSS实现乒乓球对打动画

来源:皓特汽车网


这次给大家带来使用CSS实现乒乓球对打动画,使用CSS实现乒乓球对打动画的注意事项有哪些,下面就是实战案例,一起来看一下。

代码解读

定义 dom,容器中包含左拍、小球和右拍:

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

居中显示:

body {
 height: 100vh;
 display: flex;
 align-items: center;
 justify-content: center;
 background: linear-gradient(silver, dimgray);
}

调整盒模型:

* {
 box-sizing: border-box;
}

画出球案:

.court {
 width: 20em;
 height: 20em;
 color: white;
 border: 1em solid currentColor;
}

画出左拍:

.court {
 position: relative;
}
.left-paddle
 width: 1em;
 height: calc(50% - 1em);
 background-color: currentColor;
 position: absolute;
 top: 1em;
 left: 1em;
}

让左拍动起来:

.left-paddle {
 animation: left-moving 1s linear infinite alternate;
}
@keyframes left-moving {
 to {
 transform: translateY(100%);
 }
}

类似地,画出右拍:

.right-paddle
 width: 1em;
 height: calc(50% - 1em);
 background-color: currentColor;
 position: absolute;
 top: 1em;
 left: 1em;
 bottom: 1em;
 right: 1em;
}

类似地,让右拍动起来:

.right-paddle {
 animation: right-moving 1s linear infinite alternate;
}
@keyframes right-moving {
 to {
 transform: translateY(-100%);
 }
}

画出小球:

.ball {
 width: 100%;
 height: 1em;
 border-left: 1em solid currentColor;
 position: absolute;
 left: 2em;
 top: calc(50% - 1.5em);
}

让小球动起来:

.ball {
 animation: bounce 1s linear infinite alternate;
}
@keyframes bounce {
 to {
 left: calc(100% - 3em);
 }
}

最后,重构一下左右拍的代码,合并共有属性:

.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;
}
.right-paddle {
 bottom: 1em;
 right: 1em;
 animation-name: right-moving;
}

大功告成!

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

Chart.js轻量级图表库使用案例解析

Node.js中https使用案例解析

Copyright © 2019- howto1234.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务