当前位置:网站首页>简易表白小页面
简易表白小页面
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的时候,尽量放在后面,以防被其他样式覆盖。
边栏推荐
- 1782. Count the number of point pairs Double pointer
- 太阳能板最大面积 od js
- 1.非类型模板参数 2.模板的特化 3.继承讲解
- Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
- 孩子的编程启蒙好伙伴,自己动手打造小世界,长毛象教育AI百变编程积木套件上手
- 进程间通信学习笔记
- What have I experienced when I won the offer of BAT and TMD technical experts?
- ShardingSphere's public table combat (7)
- 无线模块的参数介绍和选型要点
- 822. Walk the Grid
猜你喜欢

一万了解 Gateway 知识点

【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)

MySql的安装配置超详细教程与简单的建库建表方法

内网渗透——提权

The client series of the DOM series

1782. 统计点对的数目 双指针

typescript17 - function optional parameters

蓝牙mesh系统开发二 mesh节点开发

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

C语言_结构体指针数组函数选票系统
随机推荐
ros2知识:在单个进程中布置多个节点
《实战》基于电商领域的词性提取及其决策树模型建模
Analyze the capabilities and scenarios of the cloud native message flow system Apache Pulsar
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
ROS Action通信
观察者(observer)模式(一)
VS warning LNK4099: No solution found for PDB
Problem record in the use of TypeScript
查看zabbix-release-5.0-1.el8.noarch.rpm包内容
打印任务排序 js od华为
MySQL——数据库的查,增,删
Image processing tool design
tensorflow与GPU版本对应安装问题
Chi-square distribution of digital image steganography
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
小黑leetcode之旅:117. 填充每个节点的下一个右侧节点指针 II
Google官方控件ShapeableImageView使用
Set the browser scrollbar style
【网络安全】文件上传靶场通关(1-11关)
rpm安装postgresql12