目录
HTML5 第二天
一、rotate
2d旋转指的是让元素在2维平面内顺时针旋转或者逆时针旋转
使用步骤:
-
给元素添加转换属性
transform
-
属性值为
rotate(角度)
如transform:rotate(30deg)
顺时针方向旋转30度
div{ transform: rotate(0deg); }
案例代码
<style>
img {
width: 150px;
/* 顺时针旋转45度 */
/* transform: rotate(45deg); */
border-radius: 50%;
border: 5px solid pink;
/* 过渡写到本身上,谁做动画给谁加 */
transition: all 0.3s;
}
img:hover {
transform: rotate(360deg);
}
</style>
</head>
<body>
<img src="media/pic.jpg" alt="">
</body>
二、三角
<style>
div {
position: relative;
width: 249px;
height: 35px;
border: 1px solid #000;
}
div::after {
content: "";
position: absolute;
top: 8px;
right: 15px;
width: 10px;
height: 10px;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
transform: rotate(45deg);
transition: all 0.2s;
}
/* 鼠标经过div 里面的三角旋转 */
div:hover::after {
transform: rotate(225deg);
}
</style>
</head>
<body>
<div></div>
</body>
二、设置元素旋转中心点(transform-origin)
-
transform-origin
基础语法transform-origin: x y;
-
重要知识点
-
注意后面的参数 x 和 y 用空格隔开
-
x y 默认旋转的中心点是元素的中心 (50% 50%),等价于
center
center
-
还可以给 x y 设置像素或者方位名词(
top
、bottom
、left
、right
、center
)
-
三、旋转中心案例
<style>
div {
overflow: hidden;
width: 200px;
height: 200px;
border: 1px solid pink;
margin: 10px;
float: left;
}
div::before {
content: "黑马";
display: block;
width: 100%;
height: 100%;
background-color: hotpink;
transform: rotate(180deg);
transform-origin: left bottom;
transition: all 0.4s;
}
/* 鼠标经过div 里面的before 复原 */
div:hover::before {
transform: rotate(0deg);
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
</body>
四、2D
转换之 scale
-
scale
的作用-
用来控制元素的放大与缩小
-
-
语法
transform: scale(x, y)
-
知识要点
-
注意,x 与 y 之间使用逗号进行分隔
-
transform: scale(1, 1)
: 宽高都放大一倍,相当于没有放大 -
transform: scale(2, 2)
: 宽和高都放大了二倍 -
transform: scale(2)
: 如果只写了一个参数,第二个参数就和第一个参数一致 -
transform:scale(0.5, 0.5)
: 缩小 -
scale
最大的优势:可以设置转换中心点缩放,默认以中心点缩放,而且不影响其他盒子
-
-
代码演示
div:hover { /* 注意,数字是倍数的含义,所以不需要加单位 */ /* transform: scale(2, 2) */ /* 实现等比缩放,同时修改宽与高 */ /* transform: scale(2) */ /* 小于 1 就等于缩放*/ transform: scale(0.5, 0.5) }
五、图片放大案例
<style>
div {
overflow: hidden;
float: left;
margin: 10px;
}
div img {
transition: all .4s;
}
div img:hover {
transform: scale(1.1);
}
</style>
</head>
<body>
<div>
<a href="#"><img src="media/scale.jpg" alt=""></a>
</div>
<div>
<a href="#"><img src="media/scale.jpg" alt=""></a>
</div>
<div>
<a href="#"><img src="media/scale.jpg" alt=""></a>
</div>
</body>
六、分页按钮案例
-
代码演示
七、 2D
转换综合写法以及顺序问题
-
知识要点
-
同时使用多个转换,其格式为
transform: translate() rotate() scale()
-
顺序会影响到转换的效果(先旋转会改变坐标轴方向)
-
但我们同时有位置或者其他属性的时候,要将位移放到最前面
-
-
代码演示
div:hover { transform: translate(200px, 0) rotate(360deg) scale(1.2) }
八、 动画(animation)
-
什么是动画
-
动画是
CSS3
中最具颠覆性的特征之一,可通过设置多个节点来精确的控制一个或者一组动画,从而实现复杂的动画效果
-
-
动画的基本使用
-
先定义动画
-
在调用定义好的动画
-
-
语法格式(定义动画)
@keyframes 动画名称 { 0% { width: 100px; } 100% { width: 200px } }
-
语法格式(使用动画)
div { /* 调用动画 */ animation-name: 动画名称; /* 持续时间 */ animation-duration: 持续时间; }
-
动画序列
-
0% 是动画的开始,100 % 是动画的完成,这样的规则就是动画序列
-
在 @keyframs 中规定某项 CSS 样式,就由创建当前样式逐渐改为新样式的动画效果
-
动画是使元素从一个样式逐渐变化为另一个样式的效果,可以改变任意多的样式任意多的次数
-
用百分比来规定变化发生的时间,或用
from
和to
,等同于 0% 和 100%
-
-
代码演示
<style> /* 我们想页面一打开,一个盒子就从左边走到右边 */ /* 1. 定义动画 */ @keyframes move { /* 开始状态 */ 0% { transform: translateX(0px); } /* 结束状态 */ 100% { transform: translateX(1000px); } } div { width: 200px; height: 200px; background-color: pink; /* 2. 调用动画 */ /* 动画名称 */ animation-name: move; /* 持续时间 */ animation-duration: 2s; } </style> </head> <body> <div></div> </body>
九、动画序列
<style>
/* from to 等价于 0% 和 100% */
/* 动画序列 */
/* 1. 可以做多个状态的变化 keyframe 关键帧 */
/* 2. 里面的百分比要是整数 */
/* 3. 里面的百分比就是 总的时间(我们这个案例10s)的划分 25% * 10 = 2.5s */
@keyframes move {
0% {
transform: translate(0, 0);
}
25% {
transform: translate(1000px, 0)
}
50% {
transform: translate(1000px, 500px);
}
75% {
transform: translate(0, 500px);
}
100% {
transform: translate(0, 0);
}
}
div {
width: 100px;
height: 100px;
background-color: pink;
animation-name: move;
animation-duration: 10s;
}
</style>
</head>
<body>
<div>
</div>
</body>
十、动画常见属性
-
常见的属性
-
代码演示
div { width: 100px; height: 100px; background-color: aquamarine; /* 动画名称 */ animation-name: move; /* 动画花费时长 */ animation-duration: 2s; /* 动画速度曲线 */ animation-timing-function: ease-in-out; /* 动画等待多长时间执行 */ animation-delay: 2s; /* 规定动画播放次数 infinite: 无限循环 */ animation-iteration-count: infinite; /* 是否逆行播放 */ animation-direction: alternate; /* 动画结束之后的状态 */ animation-fill-mode: forwards; } div:hover { /* 规定动画是否暂停或者播放 */ animation-play-state: paused; }
十一、 动画简写方式
-
动画简写方式
/* animation: 动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 起始与结束状态 */ animation: name duration timing-function delay iteration-count direction fill-mode
-
知识要点
-
简写属性里面不包含
animation-paly-state
-
暂停动画
animation-paly-state: paused
; 经常和鼠标经过等其他配合使用 -
要想动画走回来,而不是直接调回来:
animation-direction: alternate
-
盒子动画结束后,停在结束位置:
animation-fill-mode: forwards
-
-
代码演示
animation: move 2s linear 1s infinite alternate forwards;
十二、速度曲线细节
-
速度曲线细节
-
animation-timing-function
: 规定动画的速度曲线,默认是ease
-
-
代码演示
<style> div { overflow: hidden; font-size: 20px; width: 0; height: 30px; background-color: pink; /* 让我们的文字强制一行内显示 */ white-space: nowrap; /* steps 就是分几步来完成我们的动画 有了steps 就不要在写 ease 或者linear 了 */ animation: w 4s steps(10) forwards; } @keyframes w { 0% { width: 0; } 100% { width: 200px; } } </style> </head> <body> <div>世纪佳缘我在这里等你</div> </body>
效果为:逐字显示世纪佳缘我在这里等你
十三、案例
1.奔跑的熊大
<style>
body {
background-color: #ccc;
}
div {
position: absolute;
width: 200px;
height: 100px;
background: url(media/bear.png) no-repeat;
/* 我们元素可以添加多个动画, 用逗号分隔 */
/* animation: 动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 起始与结束状态 */
/*盒子动画结束后,停在结束位置:`animation-fill-mode: forwards` */
animation: bear .4s steps(8) infinite, move 3s forwards;
}
@keyframes bear {
0% {
background-position: 0 0;
}
100% {
background-position: -1600px 0;
}
}
@keyframes move {
0% {
left: 0;
}
100% {
left: 50%;
/* margin-left: -100px; */
transform: translateX(-50%);
}
}
</style>
</head>
<body>
<div></div>
</body>
2.阴阳师动画行走案例
<body>
<div class="f4_box">
<div class="walk01">
<div class="west-01"></div>
</div>
<div class="walk02">
<div class="west-02"></div>
</div>
<div class="walk03">
<div class="west-03"></div>
</div>
<div class="walk04">
<div class="west-04"></div>
</div>
</div>
</body>
.f4_box {
position: fixed;
width: 100%;
height: 175px;
left: 0;
bottom: 0;
z-index: 99;
-webkit-transition: all 3.2s ease;
-moz-transition: all 3.2s ease;
-ms-transition: all 3.2s ease;
transition: all 3.2s ease;
}
.walk01 {
position: absolute;
left: 0;
bottom: 0;
width: 154px;
height: 121px;
opacity: 0;
-webkit-animation: move01 15s linear infinite;
-moz-animation: move01 15s linear infinite;
-ms-animation: move01 15s linear infinite;
animation: move01 15s linear infinite;
}
.west-01 {
width: 154px;
height: 121px;
background-image: url(../images/txgc_6d0e9e5.png);
background-repeat: no-repeat;
-webkit-animation: play01 .4s steps(2) infinite;
-moz-animation: play01 .4s steps(2) infinite;
-ms-animation: play01 .4s steps(2) infinite;
animation: play01 .4s steps(2) infinite;
}
@keyframes move01 {
0% {
-webkit-transform: translate(-200px, 0);
-moz-transform: translate(-200px, 0);
-ms-transform: translate(-200px, 0);
transform: translate(-200px, 0);
opacity: 1;
}
100% {
-webkit-transform: translate(1920px, 0);
-moz-transform: translate(1920px, 0);
-ms-transform: translate(1920px, 0);
transform: translate(1920px, 0);
opacity: 1;
}
}
@keyframes play01 {
0% {
background-position: 0 0;
}
100% {
/* background-position(x,y) x表示向右 y表示向下 */
/*表示向左偏移308px,上下不偏移*/
background-position: -308px 0;
}
}
.walk02 {
position: absolute;
left: 0;
bottom: 0;
width: 167px;
height: 176px;
opacity: 0;
-webkit-animation: move01 15s linear 2s infinite;
-moz-animation: move01 15s linear 2s infinite;
-ms-animation: move01 15s linear 2s infinite;
animation: move01 15s linear 2s infinite;
}
.west-02 {
width: 167px;
height: 176px;
background-image: url(../images/txgh_fee2457.png);
background-repeat: no-repeat;
-webkit-animation: play02 .4s steps(2) infinite;
-moz-animation: play02 .4s steps(2) infinite;
-ms-animation: play02 .4s steps(2) infinite;
animation: play02 .4s steps(2) infinite;
}
@keyframes play02 {
0% {
background-position: 0 0;
}
100% {
background-position: -334px 0;
}
}
.walk03 {
position: absolute;
left: 0;
bottom: 0;
width: 161px;
height: 149px;
opacity: 0;
-webkit-animation: move01 15s linear 4s infinite;
-moz-animation: move01 15s linear 4s infinite;
-ms-animation: move01 15s linear 4s infinite;
animation: move01 15s linear 4s infinite;
}
.west-03 {
width: 161px;
height: 149px;
background-image: url(../images/txgl_c15cfd8.png);
background-repeat: no-repeat;
-webkit-animation: play03 .4s steps(2) infinite;
-moz-animation: play03 .4s steps(2) infinite;
-ms-animation: play03 .4s steps(2) infinite;
animation: play03 .4s steps(2) infinite;
}
@keyframes play03 {
0% {
background-position: 0 0;
}
100% {
background-position: -322px 0;
}
}
.walk04 {
position: absolute;
left: 0;
bottom: 0;
width: 171px;
height: 157px;
opacity: 0;
-webkit-animation: move01 15s linear 6s infinite;
-moz-animation: move01 15s linear 6s infinite;
-ms-animation: move01 15s linear 6s infinite;
animation: move01 15s linear 6s infinite;
}
.west-04 {
width: 171px;
height: 157px;
background-image: url(../images/txgq_d281c9b.png);
background-repeat: no-repeat;
-webkit-animation: play04 .4s steps(2) infinite;
-moz-animation: play04 .4s steps(2) infinite;
-ms-animation: play04 .4s steps(2) infinite;
animation: play04 .4s steps(2) infinite;
}
@keyframes play04 {
0% {
background-position: 0 0;
}
100% {
background-position: -342px 0;
}
}