用Css3 实现React 动画的三种方法

html-css012

用Css3 实现React 动画的三种方法,第1张

中级:

Css3中的帧动画

这次用react-transition-group做一个togglebutton控制div显示和隐藏的例子,首先我们需要安装react-transition-group,输入

组件中引入CSSTransition模块:

一旦动画入场,插件将会自动的在包裹住的标签上添加很多css样式,默认class名是boss-text,所以我们需要给CSSTransition标签加上classNames='boss-text',然后去css文件进行配置:

如果页面上一组dom都需要添加动画效果时我们需要在最外面再加一个TransitionGroup

简单的动画”复选框

设置HTML创建一个标准的无序列表(为了方便测试,特别复制出一份代码)

<ul>

<li>

<input type="checkbox" name="manager" id="manager" />

<label for="manager">Project Manager</label>

</li>

<li>

<input type="checkbox" name="webdesigner" id="webdesigner" />

<label for="webdesigner">Web Designer</label>

</li>

<li>

<input type="checkbox" name="webdev" id="webdev" />

<label for="webdev">Web Developer</label>

</li>

<li>

<input type="checkbox" name="seo" id="seo" />

<label for="seo">SEO</label>

</li>

<li>

<input type="checkbox" name="itstaff" id="itstaff" />

<label for="itstaff">IT Staff</label>

</li>

<li>

<input type="checkbox" name="csr" id="csr" />

<label for="csr">Customer Service Representative</label>

</li>

</ul>

首先,隐藏复选框

/* Hide the Ordinary Checkbox */

input[type="checkbox"] {

display: none

}

然后需要在我们的列表和标签标记的相对位置和填充设置一些样式。

下一步需要使用伪代码在标签之前和之后设置样式。对于这部分,我们将设置复选框Font Awesome,用一个矢量图标。

/* Checkbox Icons */

label {

position: relative

padding-left: 30px

font-size: 30px

cursor: pointer

color: #fff

padding: 16px 28px 0 0

}

label:before, label:after {

font-family: FontAwesome

font-size: 50px

/*absolutely positioned*/

position: absolutetop: 0left: -49pxright: 10px

}

现在我们需要设置图标步骤之前和之后的复选框。

label:before {

content: '\f096'/*checkbox unchecked */

}

label:after {

content: '\f00c'/*checkbox checked*/

max-width: 0

overflow: hidden

opacity: 0.5

font-size: 27px

top: 16px

left: -42px

color: #f2ca27

-webkit-transition: all 0.50s

-moz-transition: all 0.50s

-o-transition: all 0.50s

transition: all 0.50s

}

最后一步是设定一个目标,文本框和复选框后的伪代码,并给它一个最大宽度25像素之间和不透明度1。

/* Animating the Checkbox Icon */

input[type="checkbox"]:checked + label:after {

max-width: 25px

opacity: 1

margin-right: 90px

}

CSS3 animation动画

1、@keyframes 定义关键帧动画

2、animation-name 动画名称

3、animation-duration 动画时间

4、animation-timing-function 动画曲线

linear 匀速

ease 开始和结束慢速

ease-in 开始是慢速

ease-out 结束时慢速

ease-in-out 开始和结束时慢速

steps 动画步数

5、animation-delay 动画延迟

6、animation-iteration-count 动画播放次数 n|infinite

7、animation-direction

normal 默认动画结束不返回

Alternate 动画结束后返回

8、animation-play-state 动画状态

paused 停止

running 运动

9、animation-fill-mode 动画前后的状态

none 不改变默认行为

forwards 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)

backwards 在 animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)

both 向前和向后填充模式都被应用

10、animation:name duration timing-function delay iteration-count direction同时设置多个属性

举例 等待条:

效果: