抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

freeCodeCamp 响应式网页设计的认证课程第十五章。你可以使用 CSS 动画将注意力吸引到网页的特定部分并使其更具吸引力。在通过构建摩天轮学习 CSS 动画的课程中,建造一个摩天轮,学习如何使用 CSS 为元素设置动画、转换它们并调整它们的速度。以下为我在学习和实战练习过程中所做的笔记,可供参考。

一、重点 CSS 代码

Draw a wheel:

1
2
3
4
5
6
7
8
9
10
.wheel {
border: 2px solid black;
border-radius: 50%;
margin-left: 50px;
position: absolute;
height: 55vw;
width: 55vw;
max-width: 500px;
max-height: 500px;
}

The transform-origin property is used to set the point around which a CSS transformation is applied:

1
2
3
4
5
6
7
8
9
.line {
background-color: black;
width: 50%;
height: 2px;
position: absolute;
top: 50%;
left: 50%;
transform-origin: 0% 0%;
}

The transform property allows you to manipulate the shape of an element:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.line:nth-of-type(2) {
transform: rotate(60deg);
}
.line:nth-of-type(3) {
transform: rotate(120deg);
}
.line:nth-of-type(4) {
transform: rotate(180deg);
}
.line:nth-of-type(5) {
transform: rotate(240deg);
}
.line:nth-of-type(6) {
transform: rotate(300deg);
}

Set the origin point to be offset 50% from the left and 0% from the top, placing it in the middle of the top edge of the element:

1
2
3
4
5
6
7
8
.cabin {
background-color: red;
width: 20%;
height: 20%;
position: absolute;
border: 2px solid;
transform-origin: 50% 0%;
}

The @keyframes at-rule is used to define the flow of a CSS animation. Within the @keyframes rule, you can create selectors for specific points in the animation sequence, such as 0% or 25%, or use from and to to define the start and end of the sequence;

@keyframes rules require a name to be assigned to them, which you use in other rules to reference. For example, the @keyframes freeCodeCamp { } rule would be named freeCodeCamp

1
2
3
4
5
6
7
8
@keyframes wheel {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}

The animation-name property is used to link a @keyframes rule to a CSS selector;

The animation-duration property is used to set how long the animation should sequence to complete;

The animation-iteration-count property sets how many times your animation should repeat;

The animation-timing-function property sets how the animation should progress over time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
.wheel {
border: 2px solid black;
border-radius: 50%;
margin-left: 50px;
position: absolute;
height: 55vw;
width: 55vw;
max-width: 500px;
max-height: 500px;
animation-name: wheel;
animation-duration: 10s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}

Use the animation property to set these all at once:

1
2
3
4
5
6
7
8
9
.cabin {
background-color: red;
width: 20%;
height: 20%;
position: absolute;
border: 2px solid;
transform-origin: 50% 0%;
animation: cabins 10s ease-in-out infinite;
}

@keyframes cabins:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@keyframes cabins {
0% {
transform: rotate(0deg);
}
25% {
background-color: yellow;
}
50% {
background-color: purple;
}
75% {
background-color: yellow;
}
100% {
transform: rotate(-360deg);
}
}

二、页面展示

评论



Copyright © 2020 - 2022 Zhihao Zhuang. All rights reserved

本站访客数: 人,
总访问量: