过渡可以在不使用 Flash 动画,不使用 JavaScript 的情况下,让元素从一种样式,平滑过渡为另一种样式。
1、transition-property
作用:定义哪个属性需要过渡,只有在该属性中定义的属性(比如宽、高、颜色等) 才会以有过渡效果。默认过渡时间为 0.
常用值:
1. none : 不过渡任何属性
2. a11: 过渡所有能过渡的属性。
3.具体某个属性名,例如: width、 heigth ,若有多个以逗号分隔。
2、transition-duration
作用:设置过渡的持续时间,即:一个状态过渡到另外一个状态耗时多久。
常用值:
1. 0:没有任何过渡时间-- 默认值
2.s或ms:秒或毫秒。
3,列表:
如果想让所有属性都持续一个时间,那就写一个值。
如果想让每个属性持续不同的时间那就写一个时间的列表。
简单实例:
需求:评论框过渡效果,鼠标经过,评论框高度向下过渡
<style>
.wrapper {
width: 700px;
display: flex;
justify-content: flex-end;
}
.avatar {
width: 48px;
height: 48px;
border-radius: 50%;
overflow: hidden;
background: url(./images/Mn_1.jpg) no-repeat center / cover;
margin-right: 20px;
}
.wrapper textarea {
/* transparent 透明的 */
border-color: transparent;
background: #f5f5f5;
border-radius: 4px;
/* 三个属性的缩写:flex-grow: 1; flex-shrink: 1 flex-basis: auto; */
flex: 1;
padding: 10px;
transition: all 0.5s;
height: 30px;
}
.wrapper textarea:hover {
background: #fff;
border-color: #e4e4e4;
height: 50px;
}
.wrapper button {
background: #00aeec;
border: none;
height: 50px;
margin-right: 80px;
color: #fff;
border-radius: 4px;
margin-left: 10px;
width: 70px;
cursor: pointer;
}
</style>
<body>
<div class="wrapper">
<i class="avatar"></i>
<textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
<button>发布</button>
</div>
</body>
3、transition-delay
作用:指定开始过渡的延迟时间,单位:s或ms
4、transition-timing-function
作用:设置过渡的类型
常用值:
1.ease:平滑过渡--默认值
2.linear:线性过渡
3.ease-in:慢一快
4.ease-out:快一慢
5. ease-in-out:慢一快一慢
6.step-start: 等同于 steps(1,start)
7.step-end: 等同于 steps(1,end)
8.steps( integer,?):接受两个参数的步进函数。第一个参数必须为正整数,指定函数的步数。第二个参数取值可以是start或end,指定每一步的值发生变化的时间点。第二个参数默认值为end。
9.cubic-bezie(number,number,number,number): 特定的贝塞尔曲线类型。
在线制作贝赛尔曲线: https://cubic-bezier.com
5、transition 复合属性
如果设置了一个时间,表示duration ;
如果设置了两个时间,第一是 duration,第二个是 delay;其他值没有顺序要求。
transition:1s 1s linear all;
常见案例:
<style>
.outer {
width: 400px;
height: 244px;
position: relative;
overflow: hidden;
}
.mask {
width: 400px;
height: 224px;
background-color: black;
color: white;
position: absolute;
top: 0;
left: 0;
text-align: center;
line-height: 224px;
font-size: 100px;
opacity: 0;
/* 过渡 */
transition: 1s linear;
cursor: pointer;
}
img {
/* 图片过渡 */
transition: 0.5s linear;
}
.outer:hover .mark {
opacity: 0.5;
}
.outer:hover img {
/* 2D缩放 */
transform: scale(1.6) rotate(20deg);
}
</style>
<body>
<div class="outer">
<img src="../images/shanghai.jpg" alt="">
<div class="mask">上海</div>
</div>
</body>