<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
div{
width: 200px;
height: 200px;
background-color: red;
position: absolute;
left: 100px;
top: 100px;
/* transition-property: left;
transition-duration: 2s; */
/* transition-timing-function: linear; */
/* transition-delay: 0.5s; */
/* 多个效果添加transition */
transition: left 2s linear 0s, background-color 5s linear 0s;
/* 为所有样式添加过渡效果 */
/* transitio:all */
}
div:active
{
left: 1200px;
background-color: blue;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
2.过渡手风琴菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
.menu{
width: 200px;
height: auto;
margin: 100px auto;
}
.item{
width: 100%;
height: auto;
background-color: #7DFFE7;
color: orange;
}
.item>h3{
height: 40px;
line-height:40px;
border-bottom: 2px solid #ccc;
padding-left: 10px;
}
.item>.itembox{
width: 100%;
height: 0;
overflow: hidden;
/* 注意!!过渡效果只能对具体的值生效,对display的none block无效 */
/* display: none; */
/*无效代码 transition: display 1s ; */
/* 添加过渡效果 */
transition: height 1s ;
}
.item>.itembox>ul{
list-style: none;
background-color: #eaffb6;
padding: 10px;
}
/* 为item添加hover伪类 */
.item:hover>.itembox{
/* display: block; */
height: 110px;
}
</style>
</head>
<body>
<div class="menu">
<div class="item">
<h3> news</h3>
<div class="itembox">
<ul>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
</ul>
</div>
</div>
<div class="item">
<h3>news</h3>
<div class="itembox">
<ul>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
</ul>
</div>
</div>
<div class="item">
<h3>news</h3>
<div class="itembox">
<ul>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
</ul>
</div>
</div>
<div class="item">
<h3>news</h3>
<div class="itembox">
<ul>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
<li>文件查看编辑选择运行</li>
</ul>
</div>
</div>
</div>
</body>
</html>
3.transform2D转换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0cm;
}
div{
width: 100px;
height: 100px;
margin: 10px auto;
background-color: red;
transition: transform 2s,background-color 2s;
}
div:first-of-type:active{
/* 如果只有一个参数X方向,如果两个参数X。Y方向
移动参照元素的左上角
*/
transform:translate(500px,400px);
background-color:salmon
}
div:nth-of-type(2):active{
transform: scale(2);
background-color:sandybrown;
}
div:nth-of-type(3):active{
/* 围绕Z轴转的 */
transform: rotate(90deg);
background-color:seagreen;
/* 同样的可以设置默认圆心不在中心的 */
transform-origin: 0 0;
}
div:nth-of-type(4):active{
/* 单个参数XY方向同,双参数XY方向不同 */
/* 如果角度为正,则往当前轴负方向斜切 */
transform: skew(-50deg,-30deg);
background-color:slategray;
}
</style>
</head>
<body>
<div>移动</div>
<div>缩放</div>
<div>旋转</div>
<div>斜切</div>
</body>
</html>
4.transform旋转轴心案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0cm;
}
.poker{
width: 100px;
height: 260px;
position: relative;
margin: 340px auto;
}
div{
width: 100%;
height: 100%;
position: absolute;
left: 0 ;
top: 0;
background-color: springgreen;
margin-top: 5px;
/* 添加过渡效果 */
transition: transform 2s,background-color 2s;
transform-origin: right top;
}
/* 添加鼠标上移效果 */
.poker:hover>div:nth-of-type(1)
{
transform: rotate(60deg);
background-color: steelblue;
}
.poker:hover>div:nth-of-type(2)
{
transform: rotate(120deg);
background-color:teal;
}
.poker:hover>div:nth-of-type(3)
{
transform: rotate(180deg);
background-color:violet;
}
.poker:hover>div:nth-of-type(4)
{
transform: rotate(240deg);
background-color:yellow;
}
.poker:hover>div:nth-of-type(5)
{
transform: rotate(300deg);
background-color:red;
}
.poker:hover>div:nth-of-type(6)
{
transform: rotate(360deg);
background-color:saddlebrown;
}
</style>
</head>
<body>
<section class="poker">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>
5.同时添加多个transform属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0cm;
}
div{
width: 100px;
height: 100px;
margin: 10px auto;
background-color: red;
transition: transform 2s,background-color 2s;
}
div:first-of-type:active{
/* 如果只有一个参数X方向,如果两个参数X。Y方向
移动参照元素的左上角
*/
transform:translate(500px,400px);
background-color:salmon
}
div:nth-of-type(2):active{
transform: scale(2);
background-color:sandybrown;
}
div:nth-of-type(3):active{
/* 围绕Z轴转的 */
transform: rotate(90deg);
background-color:seagreen;
/* 同样的可以设置默认圆心不在中心的 */
transform-origin: 0 0;
}
div:nth-of-type(4):active{
/* 单个参数XY方向同,双参数XY方向不同 */
/* 如果角度为正,则往当前轴负方向斜切 */
transform: skew(-50deg,-30deg);
background-color:slategray;
}
div:nth-of-type(5):active
{
/* 注意先旋转会旋转坐标系 */
transform: rotate(90deg) translate(60px,70px);
background-color:slategray;
}
div:nth-of-type(6):active
{
/* 注意先旋转会旋转坐标系 */
transform: translate(60px,70px) rotate(90deg);
background-color:slategray;
}
</style>
</head>
<body>
<div>移动</div>
<div>缩放</div>
<div>旋转</div>
<div>斜切</div>
<div>旋转加移动</div>
<div>移动加旋转</div>
</body>
</html>
6.利用transform实现任意元素居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0em;
}
.box{
width: 400px;
height: 400px;
border-radius: 200px;
background-color: #cccccc;
margin: 200px auto;
position: relative;
}
.rect{
width: 100px;
height: 100px;
background-color: red;
margin: 0 auto;
position: absolute;
left: 50%;
top: 50%;
/* 下面这个百分比参照的是元素本身 */
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="box">
<div class="rect">
</div>
</div>
</body>
</html>
7.3D变换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
div
{
width: 100px;
height: 100px;
background-color:red;
margin-left: 200px;
margin-top: 10px;
transition: transform 2s;
}
div:first-of-type:active{
/* XYZ Z轴是与屏幕垂直的轴,看不出来移动的效果 */
/* rotate3d(x,y,z,angel) 其中xyz是向量矢量,只要大于一都一样的效果*/
transform: translate3d(400px,200px,30px) scale3d(2,2,2) rotate3d(3,1,1,60deg);
}
</style>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</body>
</html>