Responsive Web Dedign(十四)
freeCodeCamp —- Responsive Web Dedign
Learn CSS Animation by Building a Ferris Wheel
Begin with the standard boilerplate. Add your
DOCTYPEdeclaration, yourhtmlelement with the language set to English, yourheadandbodyelements.Add your
metaelement for the correctcharset, yourtitleelement, and alinkelement for the./styles.cssfile.Set the
titletoFerris Wheel.Add a
divwithin yourbodyelement and give it aclassofwheel.Inside your new
div, add sixspanelements with aclassset toline, and sixdivelements with aclassset tocabin.Create a selector for your
.wheelelement. Start by setting theborderto2px solid black, theborder-radiusto50%, and themargin-leftto50px.Set the
positionproperty of the.wheelselector toabsolute. Set theheightandwidthboth to55vw.Give your
.wheelselector amax-heightandmax-widthproperty both set to500px.Create a selector for your
.lineelements. Start by setting thebackground-colortoblack, thewidthto50%, and theheightto2px.Set the
.lineselector’spositionproperty toabsolute, theleftproperty to50%, and thetopproperty to50%.The
transform-originproperty 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-origindetermines around which point the element is rotated.Give the
.lineselector atransform-originproperty 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
.lineelement. Set thetransformproperty torotate(60deg).Remember that the
transformproperty allows you to manipulate the shape of an element. In this case, using therotate(60deg)value will rotate the element around itstransform-originpoint 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
transformproperty for the third.linetorotate(120deg), the fourth torotate(180deg), the fifth torotate(240deg), and the sixth torotate(300deg).Create a
.cabinselector. Set thebackground-colortored, thewidthto20%, and theheightto20%.Give the
.cabinapositionofabsolute, and aborderof2px solid.Set the
.cabinto have atransform-originproperty 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
.cabinelement. Set therightproperty to-8.5%and thetopproperty to50%.Continuing the pattern, select the following
.cabinelements and apply the specific rules to them:- The second
.cabinshould have therightproperty set to17%and thetopproperty set to93.5%. - The third
.cabinshould have therightproperty set to67%and thetopproperty set to93.5%. - The fourth
.cabinshould have theleftproperty set to-8.5%and thetopproperty set to50%. - The fifth
.cabinshould have theleftproperty set to17%and thetopproperty set to7%. - The sixth
.cabinshould have therightproperty set to17%and thetopproperty set to7%.
- The second
The
@keyframesat-rule is used to define the flow of a CSS animation. Within the@keyframesrule, you can create selectors for specific points in the animation sequence, such as0%or25%, or usefromandtoto define the start and end of the sequence.@keyframesrules 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
@keyframesrule namedwheel.You now need to define how your animation should start. To do this, create a
0%rule within your@keyframes wheelrule. 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 atransformproperty set torotate(0deg). This will start the animation with no rotation.Now give the
@keyframes wheelrule a100%selector. Within that, set thetransformtorotate(360deg). By doing this, your animation will now complete a full rotation.The
animation-nameproperty is used to link a@keyframesrule to a CSS selector. The value of this property should match the name of the@keyframesrule. Give your.wheelselector ananimation-nameproperty set towheel.The
animation-durationproperty 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.wheelselector to have ananimation-durationproperty of10s.The
animation-iteration-countproperty sets how many times your animation should repeat. This can be set to a number, or toinfiniteto indefinitely repeat the animation. Your Ferris wheel should never stop, so set the.wheelselector to have ananimation-iteration-countofinfinite.The
animation-timing-functionproperty 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-functiontolinearin your.wheelselector.Create another
@keyframesrule with the namecabins. Use the same properties as your@keyframes wheel, copying both the0%and100%rules, but set thetransformproperty of the100%selector torotate(-360deg).With your
.wheelselector, you created four different properties to control the animation. For your.cabinselector, you can use theanimationproperty to set these all at once.Set the
animationproperty of the.cabinrule tocabins 10s linear infinite. This will set theanimation-name,animation-duration,animation-timing-function, andanimation-iteration-countproperties in that order.To make your cabin animation seem more like a natural swinging motion, you can use the
ease-in-outtiming 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
lineartoease-in-outin the.cabinselector.You can use
@keyframesrules to control more than just the transformation of an element. In the0%selector of your@keyframes cabins, set thebackground-colortoyellow.Between the
0%and100%selectors, add a50%selector. This will apply in the middle of the animation cycle. Set thebackground-colortopurple.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-colorfrom your0%selector.Create a new
25%selector between your0%and50%selectors. Give this new selector thebackground-colorproperty set toyellow.Finally, create a new
75%selector between your50%and100%selectors. Give this new selector abackground-colorproperty set toyellow.With that, your animation is much smoother and your Ferris wheel is complete.
1 |
|
1 | .wheel { |