当前位置:网站首页>简易表白小页面
简易表白小页面
2022-07-31 01:31:00 【盖玥希圈外男友(섭소우)】
目录
需求
又是没有活的一下午......做个简单的css动画玩玩!
很简单,把若干行不同文字的“我爱你”放在页面中,杂乱无章地动起来即可。
思路和代码
对于结构,需要一个父元素container,里面放许多item,这些item就是各种文字的“我爱你”。给这些item加上动画,随机设置不同的延迟时间和持续时间,这样就能产生杂乱无章的效果了。
过于简单,直接上代码:
html部分
<div class="container">
<div class="item">我爱你</div>
<div class="item">I Love You</div>
<div class="item">사랑해요</div>
<div class="item">愛する</div>
<div class="item">EU te amo</div>
<div class="item">Je t' aime</div>
<div class="item">Ich liebe dich</div>
</div>
css部分
.container {
width: 500px;
height: 700px;
margin: auto;
background-color: pink;
}
.item {
width: 300px;
height: 50px;
font-size: 30px;
font-family: 'Courier New', Courier, monospace;
color: white;
}
.item:nth-child(1) {
animation: move 10s 0.5s alternate-reverse linear infinite both;
}
.item:nth-child(2) {
animation: move 8s 1s alternate-reverse linear infinite both;
}
.item:nth-child(3) {
animation: move 7s 0.8s alternate-reverse linear infinite both;
}
.item:nth-child(4) {
animation: move 6s 0.7s alternate-reverse linear infinite both;
}
.item:nth-child(5) {
animation: move 10s 1.1s alternate-reverse linear infinite both;
}
.item:nth-child(6) {
animation: move 9s 1.3s alternate-reverse linear infinite both;
}
.item:nth-child(7) {
animation: move 5s 1s alternate-reverse linear infinite both;
}
@keyframes move {
from {
transform: translate(0);
}
10% {
transform: translate(300px, 40px);
}
20% {
transform: translate(0px, 80px);
}
30% {
transform: translate(300px, 120px);
}
40% {
transform: translate(0px, 160px);
}
50% {
transform: translate(300px, 200px);
}
60% {
transform: translate(0px, 240px);
}
70% {
transform: translate(300px, 280px);
}
80% {
transform: translate(0px, 320px);
}
90% {
transform: translate(300px, 360px);
}
100% {
transform: translate(0px, 400px);
}
}
这样基本就能搭出来了。看效果。
实现效果
效果还算可以,毕竟只是弄着玩。
进一步
如果现在我要求,鼠标hover某个item的时候,它就变色,同时暂停动画,应该怎么办?
很简单,直接添加一个hover伪类即可。修改后的css:
.container {
width: 500px;
height: 700px;
margin: auto;
background-color: pink;
}
.item {
width: 300px;
height: 50px;
font-size: 30px;
font-family: 'Courier New', Courier, monospace;
color: white;
}
.item:hover {
color: antiquewhite;
cursor: pointer;
animation-play-state: paused;
}
.item:nth-child(1) {
animation: move 10s 0.5s alternate-reverse linear infinite both;
}
.item:nth-child(2) {
animation: move 8s 1s alternate-reverse linear infinite both;
}
.item:nth-child(3) {
animation: move 7s 0.8s alternate-reverse linear infinite both;
}
.item:nth-child(4) {
animation: move 6s 0.7s alternate-reverse linear infinite both;
}
.item:nth-child(5) {
animation: move 10s 1.1s alternate-reverse linear infinite both;
}
.item:nth-child(6) {
animation: move 9s 1.3s alternate-reverse linear infinite both;
}
.item:nth-child(7) {
animation: move 5s 1s alternate-reverse linear infinite both;
}
@keyframes move {
from {
transform: translate(0);
}
10% {
transform: translate(300px, 40px);
}
20% {
transform: translate(0px, 80px);
}
30% {
transform: translate(300px, 120px);
}
40% {
transform: translate(0px, 160px);
}
50% {
transform: translate(300px, 200px);
}
60% {
transform: translate(0px, 240px);
}
70% {
transform: translate(300px, 280px);
}
80% {
transform: translate(0px, 320px);
}
90% {
transform: translate(300px, 360px);
}
100% {
transform: translate(0px, 400px);
}
}
本来兴高采烈去试试,结果发现,hover的时候,文字颜色确实变了,但动画没有暂停。
这是为什么呢?我以为是代码写错了,但仔细检查,并没有错误。
后来灵光一动,发现了问题。我把hover写在了item:nth-child的前面。在hover中设置动画的状态是暂停,但这子元素选择器中设置的状态是running,覆盖了前面的暂停。因此,必须把hover放在子元素伪类选择器的后面。
修改代码:
.container {
width: 500px;
height: 700px;
margin: auto;
background-color: pink;
}
.item {
width: 300px;
height: 50px;
font-size: 30px;
font-family: 'Courier New', Courier, monospace;
color: white;
}
.item:nth-child(1) {
animation: move 10s 0.5s alternate-reverse linear infinite both;
}
.item:nth-child(2) {
animation: move 8s 1s alternate-reverse linear infinite both;
}
.item:nth-child(3) {
animation: move 7s 0.8s alternate-reverse linear infinite both;
}
.item:nth-child(4) {
animation: move 6s 0.7s alternate-reverse linear infinite both;
}
.item:nth-child(5) {
animation: move 10s 1.1s alternate-reverse linear infinite both;
}
.item:nth-child(6) {
animation: move 9s 1.3s alternate-reverse linear infinite both;
}
.item:nth-child(7) {
animation: move 5s 1s alternate-reverse linear infinite both;
}
@keyframes move {
from {
transform: translate(0);
}
10% {
transform: translate(300px, 40px);
}
20% {
transform: translate(0px, 80px);
}
30% {
transform: translate(300px, 120px);
}
40% {
transform: translate(0px, 160px);
}
50% {
transform: translate(300px, 200px);
}
60% {
transform: translate(0px, 240px);
}
70% {
transform: translate(300px, 280px);
}
80% {
transform: translate(0px, 320px);
}
90% {
transform: translate(300px, 360px);
}
100% {
transform: translate(0px, 400px);
}
}
.item:hover {
color: antiquewhite;
cursor: pointer;
animation-play-state: paused;
}
最终效果
很好!
总结
一个非常非常简单的小案例。以后记住,在加hover的时候,尽量放在后面,以防被其他样式覆盖。
边栏推荐
- PDF split/merge
- System design. Short chain system design
- 权限管理怎么做的?
- ROS Action通信
- typescript12 - union types
- MySql的安装配置超详细教程与简单的建库建表方法
- The difference between 4G communication module CAT1 and CAT4
- Parameter introduction and selection points of wireless module
- 进程间通信学习笔记
- MySql data recovery method personal summary
猜你喜欢
TiCDC 架构和数据同步链路解析
【Mysql】——索引的深度理解
斩获BAT、TMD技术专家Offer,我都经历了什么?
ShardingSphere read-write separation (8)
射频器件的基本参数1
Yolov7实战,实现网页端的实时目标检测
MySQL (6)
VSCode插件:嵌套注释
The Meta Metaverse Division lost 2.8 billion in the second quarter, still want to continue to bet?Metaverse development has yet to see a way out
数字图像隐写术之JPEG 隐写分析
随机推荐
权限管理怎么做的?
Teach you how to configure Jenkins automated email notifications
Yolov7实战,实现网页端的实时目标检测
tkinter模块高级操作(二)—— 界面切换效果、立体阴影字效果及gif动图的实现
打印任务排序 js od华为
C language _ structure pointer array function voting system
力扣每日一题-第46天-704. 二分查找
黄东旭:TiDB的优势是什么?
TiDB之rawkv升级之路v5.0.4--&gt;v6.1.0
系统设计.短链系统设计
Rocky/GNU之Zabbix部署(1)
MySQL的安装教程(嗷嗷详细,包教包会~)
数字图像隐写术之JPEG 隐写分析
蓝牙mesh系统开发二 mesh节点开发
分布式.分布式锁
聚簇索引和非聚簇索引到底有什么区别
手把手教你配置Jenkins自动化邮件通知
关于Redis相关内容的基础学习
822. Walk the Grid
软件测试要达到一个什么水平才能找到一份9K的工作?