css中过渡与动画的使用

css中过渡与动画的使用

1.css实现过渡效果

CSS3中新增的transform属性,可以实现元素在变换过程中的过渡效果,实现了基本的动画。

transition的语法:transition: transition-property || transition-duration ||transition-timing-funciton || transition-delay ;

其参数的取值说明如下:

<transition-property>:定义用于过渡的属性;

<transition-duration>:定义过渡过程需要的时间(必选,否则不会产生过渡效果)

<transition-timing-function>:定义过渡的方式;

<transition-delay>:定义开始过渡的延迟时间;


使用transition属性定义一组过渡效果,需要以上四个参数。transition属性可以同时定义

两组或两组以上的过渡效果,组与组之间用逗号分隔。


基于webkit内核的私有属性是:-webkit-transition;

基于gecko内核的私有属性是:-moz-transition;
基于prestot内核的私有属性是:-o-transition;

例子:
HTML部分

<div :style="{zIndex:zIndex,height:height,width:width}" class="pan-item">
    <div class="pan-info">
      <div class="pan-info-roles-container">
        <slot />
      </div>
    </div>
    <!-- eslint-disable-next-line -->
    <div :style="{backgroundImage: `url(${image})`}" class="pan-thumb"></div>
  </div>

CSS部分

.pan-thumb {
  width: 100%;
  height: 100%;
  background-position: center center;
  background-size: cover;
  border-radius: 50%;
  overflow: hidden;
  position: absolute;
  transform-origin: 95% 40%;   //设置旋转元素的基点位置
  transition: all 0.3s ease-in-out;
}
.pan-item:hover .pan-thumb {
  transform: rotate(-110deg);
}

详细讲解一下各个属性的设置

(1)指定过渡的属性 ( transition-property属性 )
transition-property的语法:transition-property:none   |   all   |   <property>   ;

其取值说明如下:
none:表示没有任何CSS属性有过渡效果;

all:为默认值,表示所有的CSS属性都有过渡效果;

property:指定一个用逗号分隔的多个属性,针对指定的这些属性有过渡效果。
例子:

.pan-thumb {
  width: 100%;
  height: 100%;
  background-position: center center;
  background-size: cover;
  border-radius: 50%;
  overflow: hidden;
  position: absolute;
  transform-origin: 95% 40%;   //设置旋转元素的基点位置
  transition: transform 0.3s ease-in-out;  
}

注意 : 这里不是rotate,而是transform

(2)指定过渡的时间(transition-duration)

transition-duration属性:用于定义过渡过程所需要的时间。
transition-duration语法:transition-duration: time;
其取值说明:
time指定一个用逗号分隔的多个时间值,单位是秒(s)或毫秒(ms)。默认值是0,即是看不到过渡效果。

(3)指定过渡延迟时间(transition-delay):

transition-delay:用于定义过渡的延迟时间;

transition-delay语法:transition-delay:time;

其取值说明如下:time的取值可以为秒(s)或毫秒(ms)。默认值为0,即是没有时间延迟,立即开始过渡效果。

(4)指定过渡方式(transition-timing-function)

transition-timing-function属性用于定义过渡的速度曲线,即是过渡方式。

transition-timing-function语法:transition-timing-function: ease | linear | ease-in | ease-out | ease-in-out | cubiz-bezier(n,n,n,n);

其取值的说明如下:

linear: 表示匀速过渡;
ease: 默认值,表示过渡的速度先慢,再快,最后非常慢;
ease-in: 表示过渡的速度先慢,后越来越快,直至结束;
ease-out: 表示过渡的速度先快,后越来越慢,直至结束;
ease-in-out: 表示过渡的速度在开始和结束时都很慢;
cubic-bezier(n,n,n,n): 自定义贝塞尔曲线的效果,其中的四个参数为从0到1的数字。

2.CSS3 animation实现逐帧动画效果
动画属性

动画属性包括:①animation-name,②animation-duration,③animation-timing-function,④animation-delay,⑤animation-iteration-count,⑥animation-direction,⑦animation-fill-mode,⑧animation-play-state
用例子说明:

(1)animation-name:指定要绑定到选择器的关键帧的名称。
(2)animation-duration:定义动画完成一个周期需要多少秒或毫秒
(3)animation-timing-function:指定动画将如何完成一个周期。
说明
linear动画从头到尾的速度是相同的。
ease默认。动画以低速开始,然后加快,在结束前变慢。
ease-in动画以低速开始。
ease-out动画以低速开始和结束。
ease-in-out动画以低速结束。
cubic-bezier(n,n,n,n)在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。
step-start在变化过程中,都是以下一帧的显示效果来填充间隔动画
step-end在变化过程中,都是以上一帧的显示效果来填充间隔动画
steps()可以传入两个参数,第一个是一个大于0的整数,他是将间隔动画等分成指定数目的小间隔动画,然后根据第二个参数来决定显示效果。第二个参数设置后其实和step-start,step-end同义,在分成的小间隔动画中判断显示效果。
(4)animation-delay:属性定义动画什么时候开始。

4.1 单位可以是秒(s)或毫秒(ms)。
4.2 单位可以是负值,-2s表示动画立马开始,但跳过 2 秒进入动画,即前2秒的动画不执行,直接跳过前2秒进入动画。
栗子:
HTML部分

<div class="spinner">
	<div class="line1"></div>
	<div class="line2">
	</div><div class="line3">
	</div><div class="line4">
	</div><div class="line5">
</div></div>

css部分

.spinner{
  width:60px;
  height:50px;
  margin:100px auto;
  text-align:center;
}
.spinner>div{
  display:inline-block;
  width:6px;
  margin-left:3px;
  margin-right:3px;
  height:100%;
  background:green;
  animation:strechdelay 1.2s infinite ease-in-out;
  -webkit-animation:strechdelay 1.2s infinite ease-in-out;
}
.spinner .line2{
  animation-delay:-1.1s;
  -webkit-animation-delay:-1.1s;
}
.spinner .line3{
  animation-delay:-1.0s;
  -webkit-animation-delay:-1.0s;
}
.spinner .line4{
  -webkit-animation-delay:-0.9s;
}
.spinner .line5{
  animation-delay:-0.8s;
  -webkit-animation-delay:-0.8s;
}
@keyframes strechdelay {
  0%,40%,100%{
    transform:scaleY(.4);
    -webkit-transform:scaleY(.4);
  }
  20%{
    transform:scaleY(1);
    -webkit-transform:scaleY(1);
  }
}
@-webkit-keyframes strechdelay {
  0%,40%,100%{
    -webkit-transform:scaleY(.4);
  }
  20%{
    -webkit-transform:scaleY(1);
  }
}

效果图
在这里插入图片描述

(5)animation-iteration-count :定义动画应该播放多少次。
说明
n一个数字,定义应该播放多少次动画
infinite指定动画应该播放无限次(永远)
(6)animation-direction:定义是否循环交替反向播放动画。
说明
normal默认值。动画按正常播放。
reverse动画反向播放。
alternate动画在奇数次(1、3、5…)正向播放,在偶数次(2、4、6…)反向播放。
alternate-reverse动画在奇数次(1、3、5…)反向播放,在偶数次(2、4、6…)正向播放。
initial设置该属性为它的默认值。
inherit从父元素继承该属性。
(7)animation-fill-mode:规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式。
说明
none默认值。动画在动画执行之前和之后不会应用任何样式到目标元素。
forwards在动画结束后(由 animation-iteration-count 决定),动画将应用该属性值。
backwards动画将应用在 animation-delay 定义期间启动动画的第一次迭代的关键帧中定义的属性值。这些都是 from 关键帧中的值(当 animation-direction 为 “normal” 或 “alternate” 时)或 to 关键帧中的值(当 animation-direction 为 “reverse” 或 “alternate-reverse” 时)。
both动画遵循 forwards 和 backwards 的规则。也就是说,动画会在两个方向上扩展动画属性。
initial设置该属性为它的默认值。
inherit从父元素继承该属性。

7.1 默认情况下,CSS 动画在第一个关键帧播放完之前不会影响元素,在最后一个关键帧完成后停止影响元素。animation-fill-mode 属性可重写该行为。
7.2 forwads表示让动画停留在结束状态,即停留在最后一帧。
7.3 backwords:
7.3.1 当 animation-direction 为 “normal” 或 “alternate” 时,回到第一帧。
7.3.2 当 animation-direction 为 “reverse” 或 “alternate-reverse” 时,停留在最后一帧。

(8)animation-play-state:指定动画是否正在运行或已暂停。

在JavaScript中使用此属性在一个周期中暂停动画。

说明
paused指定暂停动画。
running指定正在运行的动画。

栗子
HTML部分

<div id="div1"></div>
<div>鼠标悬浮在线条上看看</div>

CSS部分

#div1 {
  width:10px;
  height:80px;
  background-color:#ff9137;
  margin:100px auto;
  animation: spin 1s linear infinite;
  animation-play-state: paused;
}
#div1:hover {
  animation-play-state: running;
}
@keyframes spin {
  0%{
    transform:rotate(0deg);
    -webkit-transform:rotate(0deg);
  }
  50%{
    transform:rotate(180deg);
    -webkit-transform:rotate(180deg);
  }
  100%{
    transform:rotate(360deg);
    -webkit-transform:rotate(360deg);
  }
}
@-webkit-keyframes spin {
  0%{
    transform:rotate(0deg);
    -webkit-transform:rotate(0deg);
  }
  50%{
    transform:rotate(180deg);
    -webkit-transform:rotate(180deg);
  }
  100%{
    transform:rotate(360deg);
    -webkit-transform:rotate(360deg);
  }
}

效果图就不放了,大家可以把代码复制下来,自己看效果.

动画相关
(1)、animation的简写(即上述属性的介绍顺序)
animation: name duration timing-function delay iteration-count direction fill-mode play-state;

具体化可以记成一下形式:

animation:myAnim 1s linear 1s infinite alternate both running;

(2)、keyframes:定义动画规则,关键帧。
说明
animationname必需的。定义animation的名称。
keyframes-selector必需的。动画持续时间的百分比
css-styles必需的。一个或多个合法的CSS样式属性
指定的变化时发生时使用%,或关键字"from"和"to",这是和0%到100%相同。以下两段代码效果相同。
@keyframes myAnim{
  from { background: #f00; }
  50% { background: #0f0; }
  to { background: yellowgreen; }
}
@keyframes myAnim{
  0% { background: #f00; }
  50% { background: #0f0; }
  100% { background: yellowgreen; }
}
如果省略某个状态,浏览器会自动推算中间状态。但是,为了获得最佳的浏览器支持,应该始终定义0%和100%的选择器。
@keyframes rainbow {
  50% { background: orange }
  to { background: yellowgreen }
}

@keyframes rainbow {
  to { background: yellowgreen }
}

可以将多个状态写在一行
@keyframes myAnim{
  0%,100% { background: #f00; }
  50% { background: #0f0; 
}
浏览器前缀
div{
	animation:myAnim 1s;
	-webkit-animation:myAnim 1s;
}
@keyframes myAnim{
  0% { background: #f00; }
  50% { background: #0f0; }
  100% { background: yellowgreen; }
}
@-webkit-keyframes myAnim{
  0% { background: #f00; }
  50% { background: #0f0; }
  100% { background: yellowgreen; }
}

注意:定义动画时,必须定义动画的名称和动画的持续时间。如果省略持续时间,动画将无法运行,因为默认值是0。
下面直接写个应用animation属性的小例子:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>04心跳</title>
		<style>
			img{
				width: 400px;
				height: auto;
				/* animation:动画名称 花费时间 运动曲线 何时开始 播放次数 是否反方向 */
				animation: heart 0.5s infinite;
			}
			
			div{
				width: 100px;
				height: 100px;
				background-color: pink;
				margin: 100px auto;
				animation: heart 0.5s infinite;//一个叫heart的动画 每隔0.5s执行动画 无限次
			}
			@keyframes heart{
				0%{
					transform: scale(1);
				}
				50%{
					transform: scale(1.1);
				}
				100%{
					transform: scale(1);
				} 
			}
		</style>
	</head>
	<body>
		<img src="../images/1.jpg" height="744" width="800" alt="loading" />
		<div></div>
	</body>
</html>

效果
在这里插入图片描述
在vue框架中有单独transition标签可实现简单地动画,一般会用在路由切换时,其详细使用请查看此文章.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值