2D变形:transform
移动translate
1、 transform:translate(x,y):沿X轴Y轴方向移动元素
transform:translateX(x):沿X轴方向移动元素
transform:translateY(y):沿Y轴方向移动元素
2、translate优点:不会影响其他元素的位置
3、translate中的百分比单位是相对于自身元素的translate(50%,50%),如果不写百分比则写像素
4、对行内标签没有效果
旋转rotate
1、格式:transform:rotate(度数)
transform:rotateX(度数):沿X轴方向(向右)旋转多少度(变成3D的了,但是在2D平面看不出3D效果,只能看到元素比原来矮)
transform:rotateY(度数):沿Y轴方向(向下)旋转多少度(变成3D的了,但是在2D平面看不出3D效果,只能看到元素比原来窄)
2、 rotate里面跟度数,单位为deg,例如rotate(45deg)
3、 角度为正时,顺时针,负时,逆时针
4、默认旋转的中心点为元素的中心点
5、旋转不影响其他位置
中心点
在2D变形中,可以根据标签设置一个点,根据这个点进行变形,旋转,缩放,倾斜支持中心点属性
1、设置中心点要写在要设置的标签里,别写在那个标签的hover里,不然效果是先以原来的中心点变形,再改变为后来设置的中心点
2、参数x和y用空格隔开
3、设置百分比:transform-origin: (50% 50%):xy默认的中心点是元素的中心点(50% 50%)
4、给xy设置像素或者方位名词(top bottom left right center)
例子:transform-origin: left buttom;(如果只写一个方位名词,则以这个方位的边的中点为中心点)
5、给xy设置像素:
例子: transform-origin: 50px 50px;
缩放(scale:范围)
格式:transform: scale(x,y):整体缩放
transform: scaleX(x):沿X轴缩放
transform: scaleY(y):沿Y轴缩放
1、xy是放大的倍数,默认都为1,即没有放大
2、transform: scale(2,2);放大两倍
3、transform: scale(0.5,0.5);缩小一半
4、只写一个参数,则第二个参数默认与第一个一样,即(2)等价于(2,2),所以只想设置一条轴时,另一条轴要设置为1
5、scale缩放的最大优势:可以设置中心点缩放,默认以中心点缩放,并且不影响其他位置
注意:只能写倍数,像素无用
倾斜skew
transform:skew(x,y);(如果只写一个值,默认为x)
transform:skewX(x):沿x轴方向倾斜
transform:skewY(y):沿Y轴方向倾斜
1、可以设置中心点,围绕中心点倾斜
2、与缩放相似
3、transform: skewy(30deg,20deg);:横坐标倾斜30度,纵坐标倾斜20度
2D综合写法
1、顺序:transform:translate() rotate() scale()…等
2、顺序会改变转换效果(先旋转会改变坐标轴方向)
3、当我们同时设置位移和其他属性时,记得将位移放到最前面
总结
1、2D变形都不会影响其他元素的位置
2、对行内标签都没有效果
3、如果想看的整个过程,要用过渡(transition)
4、旋转,缩放,倾斜支持中心点属性
2D练习
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.div1{
background-image: url(img/02.jpg);
background-size: cover;
background-repeat: no-repeat;
background-position: 0 -20px;
width: 500px;
height: 325px;
margin: 100px auto;
transition: all .5s ease;
border: 1px dashed red;
position: relative;
}
.div1:hover{
background-position: 0 0;
}
.div2{
width: 100%;
height: 325px;
position: absolute;
left: 0px;
top: 0px;
z-index: 999;
}
.div1:hover .div2{
background-color: rgba(0,0,0,0.1);
}
.h{
width: 400px;
height: 265px;
}
.h1{
position: absolute;
left: 70px;
top: 30px;
}
.div2:hover .h1{
border-left: 1px solid white;
}
.h2{
position: absolute;
left: 50px;
top: 45px;
}
.div2:hover .h2{
border-left: 1px solid white;
}
.h3{
position: absolute;
right: 70px;
top: 30px;
}
.div2:hover .h3{
border-left: 1px solid white;
}
.h4{
position: absolute;
left: 50px;
bottom: 45px;
}
.div2:hover .h4{
border-left: 1px solid white;
}
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
<div class="h h1"></div>
<div class="h h2"></div>
<div class="h h3"></div>
<div class="h h4"></div>
</div>
</body>
</html>
结果:
动画animation
动画的基本使用
1、定义动画
@keyframes move{
0%{
transform: translateX(0%);
}
100%{
transform: translateX(150%);
}
注意:
(1)%0是动画的开始,100%是动画的结束,这样的规则就是动画序列
(2)在@keyframes中规定某项css样式,就能创建由当前样式逐渐改为新样式的动画效果
(3)动画是使元素从一种样式逐渐变化为另一种样式的效果,你可以改变任意多的样式任意多的次数
(4)用百分比来规定变化发生的时间,或用关键字“from”和“to”来代替0%和100%
2、调用动画
#div1{
background-color: skyblue;
height: 200px;
width: 200px;
/*调用动画*/
animation-name: move;
/*持续时间*/
animation-duration: 3s;
}
动画常用属性
属性 | 描述 |
---|---|
@keyframes | 规定动画(关键字) |
animation | 所有动画的简写属性,除了animation-play-state |
animation-name | 规定@keyframes动画的名称(必须的) |
animation-duration | 规定动画完成一个周期所花费的秒或毫秒,默认是0(必须的) |
animation-timing-function | 规定动画的速度曲线,默认是“ease” |
animation-delay | 规定动画何时开始,默认是0 |
animation-iteration-count | 规定动画播放的次数,默认是1,可以设置infinite(无穷大) |
animation-direction | 规定动画在下一周期是否逆向播放(走回来,不是跳回来),默认是”normal”,可以设置为”alternate”逆播放 |
animation-play-state | 规定动画是否正在运行或者暂停,默认是”running”,还有”paused” |
animation-fill-mode | 规定动画结束后状态,停止在终点位置”forwards”,回到起始位置”backwords”(默认值) |
动画简写属性
animation:动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 动画起始或者结束的状态
注意:
(1)简写属性里不包含animation-play-state
(2)暂停动画:animation-play-state:paused;经常和鼠标经过等其他配合使用
(3)animation可以添加多个动画,每个动画之间用逗号分隔
animation-timing-function的值
值 | 描述 |
---|---|
linear | 匀速 |
ease | 低速开始,然后加快,结束前变慢(慢快慢) |
ease-in | 低速开始(慢快快) |
ease-out | 低速结束(快快慢) |
ease-in-out | 低速开始,低速结束(慢快慢) |
steps() | 指定了时间函数中的间隔数量(步长) |
动画练习
1、摩天轮
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
.a1{
background-image: url(img/motianlun/2.jpg);
background-size: 100% 100%;
background-repeat: no-repeat;
width: 100%;
height: 720px;
}
.a2{
margin: 0 auto;
transform: translateY(20%);
width: 400px;
height: 500px;
position: relative;
}
.img1{
position: absolute;
left: 95px;
bottom: 0px;
width: 200px;
height: 300px;
}
.img2{
position: absolute;
left: 67px;
bottom: 0px;
width: 250px;
height: 300px;
}
.lunzi{
width: 400px;
height: 400px;
margin-left:-5px;
margin-top: 10px;
position: relative;
animation-name: lunzi;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
@keyframes lunzi{
from{
transform: rotate(0deg);
}
to{
transform: rotate(360deg);
}
}
.img3{
position: absolute;
left: 0px;
bottom: 0px;
width: 100%;
height: 100%;
}
.img{
position: absolute;
width: 80px;
height: 100px;
animation-name: tupian;
animation-duration: 10s;
animation-timing-function: linear;
animation-iteration-count: infinite;
/*transform-origin: 50% 0%;*/
}
@keyframes tupian{
from{
transform: rotate(360deg);
}
to{
transform: rotate(0deg);
}
}
.img4{
left: 40px;
bottom: 0px;
}
.img5{
left: -20px;
bottom: 120px;
}
.img6{
left: 40px;
bottom: 250px;
}
.img7{
left: 165px;
top: -5px;
}
.img8{
right: 40px;
top: 35px;
}
.img9{
right: -20px;
top: 160px;
}
.img10{
right: 30px;
top: 265px;
}
.img11{
right: 155px;
bottom: -30px;
}
</style>
</head>
<body>
<div class="a1">
<div class="a2">
<img src="img/motianlun/bracketsmall.png" class="img1"/>
<img src="img/motianlun/bracket.png" class="img2"/>
<div class="lunzi">
<img src="img/motianlun/fsw.png" class="img3"/>
<img src="img/motianlun/dog.png" class="img4 img"/>
<img src="img/motianlun/girl.png" class="img5 img"/>
<img src="img/motianlun/girls.png" class="img6 img"/>
<img src="img/motianlun/hairboy.png" class="img7 img"/>
<img src="img/motianlun/mimi.png" class="img8 img"/>
<img src="img/motianlun/mudog.png" class="img9 img"/>
<img src="img/motianlun/shamegirl.png" class="img10 img"/>
<img src="img/motianlun/boy.png" class="img11 img"/>
</div>
</div>
</div>
</body>
</html>
结果:
2、小熊炮车
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.bg{
background-color: #0093fe;
width: 100%;
height: 720px;
}
/*天空*/
.head{
width: 100%;
height: 300px;
overflow: hidden;
position: relative;
}
.square{
width: 300px;
height: 300px;
/*背景渐变*/
background:linear-gradient(to left bottom, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.1));
transform: rotate(70deg);
position: absolute;
left: 200px;
top: 150px;
}
.square2{
width: 200px;
height: 200px;
/*背景渐变*/
background:linear-gradient(to left bottom, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.1));
/*旋转45度*/
transform: rotate(40deg);
position: absolute;
right: 50px;
top: 0px;
}
.circular{
width: 200px;
height: 200px;
border-radius: 50%;
background:linear-gradient(to left bottom, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.1));
position: absolute;
right: 530px;
top: 50px;
}
.cir{
width: 30px;
height: 30px;
border-radius: 50%;
background:linear-gradient(to left bottom, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.1));
position: absolute;
}
.cir1{
left: 50px;
top: 150px;
}
.cir2{
left: 150px;
top: 100px;
}
.cir3{
left: 300px;
top: 50px;
}
.cir4{
left: 500px;
top: 80px;
}
.cir5{
left: 600px;
top: 180px;
}
.cir6{
left: 800px;
top: 30px;
}
.cir7{
left: 1000px;
top: 50px;
}
.cir8{
left: 1200px;
top: 150px;
}
.cir9{
right: 50px;
top: 180px;
}
/*马路*/
.walk{
width: 100%;
height: 200px;
background-color: black;
position: relative;
/*防止绝对定位right: -200px;出现横向滚动条效果*/
overflow: hidden;
}
.rectangle{
width: 200px;
height: 20px;
background-color: white;
position: absolute;
right: -200px;
top: 90px;
}
.rect1{
animation: road 9s linear infinite;
}
.rect2{
animation: road 9s 3s linear infinite;
}
.rect3{
animation: road 9s 6s linear infinite;
}
@keyframes road{
from{
left: 100%;
transform: skew(30deg);
}
to{
left: -15%;
transform: skew(-30deg);
}
}
.car{
width: 300px;
height: 200px;
position: relative;
left: 550px;
top: -315px;
}
.wheel1{
display: inline-block;
width: 50px;
height: 50px;
border:3px solid orange;
border-radius: 50%;
background-color: black;
position: relative;
left: 20px;
top: 140px;
z-index: 99;
}
.wheel2{
display: inline-block;
width: 50px;
height: 50px;
border:3px solid orange;
border-radius: 50%;
background-color: black;
position: relative;
left: 100px;
top: 140px;
z-index: 99;
}
.wheel3{
width: 30px;
height: 30px;
border-radius: 5px;
background-color: white;
position: absolute;
left: 10px;
top: 10px;
animation: roll 2s linear infinite;
}
@keyframes roll{
from{
transform: rotate(0deg);
}
to{
transform: rotate(360deg);
}
}
.cheShen{
display: inline-block;
width: 200px;
height: 100px;
background-color: orange;
position: absolute;
left: 0px;
top: 60px;
z-index: 9;
}
.cheZuo{
display: inline-block;
width: 100px;
height: 55px;
background-color: orange;
margin-left: -15px;
transform: skewX(-10deg);
position: absolute;
left: 205px;
top: 100px;
}
.jianduan{
display: inline-block;
width: 50px;
height: 40px;
background-color: orange;
transform: skewY(10deg);
position: absolute;
right: 7px;
top: 70px;
}
/*炮弹*/
.yuan{
width: 30px;
height: 30px;
border-radius: 50%;
background-color: white;
position: absolute;
left: 0px;
top: 15px;
}
.bullet{
width: 200px;
height: 30px;
background-color: white;
position: absolute;
left: 15px;
top: 15px;
transform: rotate(-20deg);
/*设置以左边的中点为中心逆时针旋转20度*/
transform-origin: left;
animation: run 1s linear infinite;
}
@keyframes run{
from{
width: 200px;
}
to{
width: 120px;
}
}
.gun{
width: 30px;
height: 30px;
background-color: red;
border-radius: 50%;
position: absolute;
left: 190px;
top: -54px;
animation: gun 1s linear infinite;
}
@keyframes gun{
from{
transform: translate(0px,0px);
}
to{
transform: translate(600px,-220px);
}
}
.zhicheng{
width: 15px;
height: 60px;
background-color: white;
position: absolute;
left: 80px;
top: 10px;
transform-origin: bottom;
animation: xuanzhuan 0.5s linear infinite alternate;
}
@keyframes xuanzhuan{
from{
transform: rotate(0deg);
}
to{
transform: rotate(-45deg);
}
}
.xiaoxiong{
width: 100px;
height: 100px;
position: absolute;
left: 184px;
top: 17px;
}
</style>
</head>
<body>
<div class="bg">
<!--天空-->
<div class="head">
<div class="square"></div>
<div class="square2"></div>
<div class="circular"></div>
<div class="cir cir1"></div>
<div class="cir cir2"></div>
<div class="cir cir3"></div>
<div class="cir cir4"></div>
<div class="cir cir5"></div>
<div class="cir cir6"></div>
<div class="cir cir7"></div>
<div class="cir cir8"></div>
<div class="cir cir9"></div>
</div>
<!--马路-->
<div class="walk">
<div class="rectangle rect1"></div>
<div class="rectangle rect2"></div>
<div class="rectangle rect3"></div>
</div>
<!--小车-->
<div class="car">
<div class="wheel1">
<div class="wheel3"></div>
</div>
<div class="wheel2">
<div class="wheel3"></div>
</div>
<div class="cheShen"></div>
<div class="cheZuo"></div>
<div class="cheTou"></div>
<div class="jianduan"></div>
<!--炮弹-->
<div class="yuan"></div>
<div class="bullet"></div>
<div class="gun"></div>
<div class="zhicheng"></div>
<img src="img/snowxiong.png" class="xiaoxiong"/>
</div>
</div>
</body>
</html>
结果:
3D移动
Transform:translate3d(x,y,z):其中x,y,z分别指向要移动的轴的方向的距离
透视perspective
要想实现3D效果,必须设置透视值perspective
D:视距:人的眼睛到屏幕的距离
Z:Z轴:物体距离屏幕的距离,Z轴越大(正值),物体距离我们就越近,我们看到的物体就越大,成像也越大
注意:
(1)透视我们也称为视距,视距就是指人的眼睛到屏幕的距离
(2)距离视觉点越近的在电脑平面成像越大,越远成像越小,所以透视值(perspective)设置得越小,看的像越大,设置得越大,看的越小
(3)透视的单位是像素
(4)透视写在被观察元素的父盒子上面
3D旋转
左手准则:左手的手指指向x,y,z轴的正方向,其余手指弯曲方向就是以各个轴为轴线所旋转的正方形
沿X轴旋转(水平旋转):例子:单杠旋转
沿Y轴旋转(垂直旋转):例子:原地转圈
沿Z轴旋转:例子:大转盘抽奖
Transform:rotate3d(1,0,0,45deg)=transform:rotate(45deg):沿X轴正方向旋转45度
Transform:rotate3d(1,1,0,45deg):沿X轴与Y轴对角线正方向旋转45度
Transform:rotate(x,y,z,deg):沿着自定义轴旋转deg为角度
3D转换transfrom-style
3D呈现transfrom-style:控制子元素是否开启三维立体环境
Transform-style:flat:子元素默认不开启3d立体空间
Transform-style:preserve-3d:子元素开启立体空间
代码写给父级,但是影响的是所有子盒子(包括孙子盒子这些)
总结
要想实现3D效果,有两个必不可少的条件
1、设置透视值(给父元素或者父元素以上设置)
2、设置transform-style为preserve-3d
3D练习:旋转图片
自动循环播放图片,类似旋转木马
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body{
/*视距越小,成像越大*/
perspective: 700px;
}
#a1{
width: 250px;
height: 150px;
background-color: gray;
margin: 250px auto;
background: url(img/xuanzhuan/lanren_07.jpg) no-repeat;
background-size: cover;
position: relative;
/*开启三维立体环境*/
transform-style: preserve-3d;
animation: rotate 12s linear infinite;
}
#a1:hover{
animation-play-state: paused;
}
@keyframes rotate{
from{
transform: rotateY(0);
}
to{
transform: rotateY(360deg);
}
}
#a1 div{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
#a1 div:nth-child(1){
background: url(img/xuanzhuan/lanren_01.jpg) no-repeat;
background-size: cover;
transform: translateZ(250px);
}
#a1 div:nth-child(2){
background: url(img/xuanzhuan/lanren_02.jpg) no-repeat;
background-size: cover;
/*先旋转再移动*/
transform: rotateY(60deg) translateZ(250px);
}
#a1 div:nth-child(3){
background: url(img/xuanzhuan/lanren_03.jpg) no-repeat;
background-size: cover;
/*先旋转再移动*/
transform: rotateY(120deg) translateZ(250px);
}
#a1 div:nth-child(4){
background: url(img/xuanzhuan/lanren_04.jpg) no-repeat;
background-size: cover;
/*先旋转再移动*/
transform: rotateY(180deg) translateZ(250px);
}
#a1 div:nth-child(5){
background: url(img/xuanzhuan/lanren_05.jpg) no-repeat;
background-size: cover;
/*先旋转再移动*/
transform: rotateY(240deg) translateZ(250px);
}
#a1 div:nth-child(6){
background: url(img/xuanzhuan/lanren_06.jpg) no-repeat;
background-size: cover;
/*先旋转再移动*/
transform: rotateY(300deg) translateZ(250px);
}
</style>
</head>
<body>
<div id="a1">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>
结果:
浏览器私有前缀
-moz-:代表firebox浏览器私有属性
-ms-:代表ie,浏览器私有属性
-webkit-:代表safari、chrome私有属性
-o:代表Opera私有属性
提倡的写法:
-moz-border-radius:10px
-ms-border-radius:10px
-webkit-border-radius:10px(谷歌可省略-webkit-)
-o-border-radius:10px