Responsive Web Dedign(十四)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Animation by Building a Ferris Wheel
Begin with the standard boilerplate. Add your
DOCTYPE
declaration, yourhtml
element with the language set to English, yourhead
andbody
elements.Add your
meta
element for the correctcharset
, yourtitle
element, and alink
element for the./styles.css
file.Set the
title
toFerris Wheel
.Add a
div
within yourbody
element and give it aclass
ofwheel
.Inside your new
div
, add sixspan
elements with aclass
set toline
, and sixdiv
elements with aclass
set tocabin
.Create a selector for your
.wheel
element. Start by setting theborder
to2px solid black
, theborder-radius
to50%
, and themargin-left
to50px
.Set the
position
property of the.wheel
selector toabsolute
. Set theheight
andwidth
both to55vw
.Give your
.wheel
selector amax-height
andmax-width
property both set to500px
.Create a selector for your
.line
elements. Start by setting thebackground-color
toblack
, thewidth
to50%
, and theheight
to2px
.Set the
.line
selector’sposition
property toabsolute
, theleft
property to50%
, and thetop
property to50%
.The
transform-origin
property is used to set the point around which a CSS transformation is applied. For example, when performing arotate
(which you will do later in this project), thetransform-origin
determines around which point the element is rotated.Give the
.line
selector atransform-origin
property of0% 0%
. This will offset the origin point by0%
from the left and0%
from the top, setting it to the top left corner of the element.Create a selector to target your second
.line
element. Set thetransform
property torotate(60deg)
.Remember that the
transform
property allows you to manipulate the shape of an element. In this case, using therotate(60deg)
value will rotate the element around itstransform-origin
point by 60 degrees clockwise.Using the same pattern, create a separate selector for the third
.line
, the fourth.line
, the fifth.line
, and the sixth.line
.Set the
transform
property for the third.line
torotate(120deg)
, the fourth torotate(180deg)
, the fifth torotate(240deg)
, and the sixth torotate(300deg)
.Create a
.cabin
selector. Set thebackground-color
tored
, thewidth
to20%
, and theheight
to20%
.Give the
.cabin
aposition
ofabsolute
, and aborder
of2px solid
.Set the
.cabin
to have atransform-origin
property of50% 0%
. This will set the origin point to be offset50%
from the left and0%
from the top, placing it in the middle of the top edge of the element.Time to position the cabins around the wheel. Select the first
.cabin
element. Set theright
property to-8.5%
and thetop
property to50%
.Continuing the pattern, select the following
.cabin
elements and apply the specific rules to them:- The second
.cabin
should have theright
property set to17%
and thetop
property set to93.5%
. - The third
.cabin
should have theright
property set to67%
and thetop
property set to93.5%
. - The fourth
.cabin
should have theleft
property set to-8.5%
and thetop
property set to50%
. - The fifth
.cabin
should have theleft
property set to17%
and thetop
property set to7%
. - The sixth
.cabin
should have theright
property set to17%
and thetop
property set to7%
.
- The second
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 as0%
or25%
, or usefrom
andto
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 namedfreeCodeCamp
.Time to start animating. Create a
@keyframes
rule namedwheel
.You now need to define how your animation should start. To do this, create a
0%
rule within your@keyframes wheel
rule. The properties you set in this nested selector will apply at the beginning of your animation.As an example, this would be a
12%
rule:1
2
3
4
5@keyframes freecodecamp {
12% {
color: green;
}
}Give the
0%
rule atransform
property set torotate(0deg)
. This will start the animation with no rotation.Now give the
@keyframes wheel
rule a100%
selector. Within that, set thetransform
torotate(360deg)
. By doing this, your animation will now complete a full rotation.The
animation-name
property is used to link a@keyframes
rule to a CSS selector. The value of this property should match the name of the@keyframes
rule. Give your.wheel
selector ananimation-name
property set towheel
.The
animation-duration
property is used to set how long the animation should sequence to complete. The time should be specified in either seconds (s
) or milliseconds (ms
). Set your.wheel
selector to have ananimation-duration
property of10s
.The
animation-iteration-count
property sets how many times your animation should repeat. This can be set to a number, or toinfinite
to indefinitely repeat the animation. Your Ferris wheel should never stop, so set the.wheel
selector to have ananimation-iteration-count
ofinfinite
.The
animation-timing-function
property sets how the animation should progress over time. There are a few different values for this property, but you want the Ferris wheel animation to run at the same rate from start to finish. Set theanimation-timing-function
tolinear
in your.wheel
selector.Create another
@keyframes
rule with the namecabins
. Use the same properties as your@keyframes wheel
, copying both the0%
and100%
rules, but set thetransform
property of the100%
selector torotate(-360deg)
.With your
.wheel
selector, you created four different properties to control the animation. For your.cabin
selector, you can use theanimation
property to set these all at once.Set the
animation
property of the.cabin
rule tocabins 10s linear infinite
. This will set theanimation-name
,animation-duration
,animation-timing-function
, andanimation-iteration-count
properties in that order.To make your cabin animation seem more like a natural swinging motion, you can use the
ease-in-out
timing function. This setting will tell the animation to start and end at a slower pace, but move more quickly in the middle of the cycle.Replace
linear
toease-in-out
in the.cabin
selector.You can use
@keyframes
rules to control more than just the transformation of an element. In the0%
selector of your@keyframes cabins
, set thebackground-color
toyellow
.Between the
0%
and100%
selectors, add a50%
selector. This will apply in the middle of the animation cycle. Set thebackground-color
topurple
.Because the animation is on an infinite loop and the start and end colors are not the same, the transition appears jerky when it switches back to yellow from red.
To start fixing this, remove the
background-color
from your0%
selector.Create a new
25%
selector between your0%
and50%
selectors. Give this new selector thebackground-color
property set toyellow
.Finally, create a new
75%
selector between your50%
and100%
selectors. Give this new selector abackground-color
property set toyellow
.With that, your animation is much smoother and your Ferris wheel is complete.
1 |
|
1 | .wheel { |