移动Web学习02

二、空间转换、动画

1、空间转换

  • 空间:是从坐标轴定义的 X、Y 和 Z 三条坐标轴构成了一个立体空间,Z 轴位置与视线方向相同
  • 空间转换也叫 3D转换
  • 属性:transform
平移
  • 属性

    transform: translate3d(x, y, z);
    transform: translateX();
    transform: translateY();
    transform: translateZ();
    
  • 取值(正负均可)

    • 像素单位数值
    • 百分比(参照盒子自身尺寸计算结果)

示例:

<!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>
       .box {
           width: 200px;
           height: 200px;
           margin: 100px auto;
           background-color: pink;
           transition: all 0.5s;
       }
       .box:hover {
           /* 电脑是平面,默认无法观察 Z 轴平移的效果 */
           transform: translate3d(100px, 200px, 300px);
           /* 3d 小括号里面必须逗号隔开三个数 */
           /* transform: translate3d(100px, 200px); */
           /* transform: translateX(100px);
           transform: translateY(-100%);
           transform: translateZ(300px); */
       }
   </style>
</head>
<body>
   <div class="box"></div>
</body>
</html>
视距 perspective

作用:指定了观察者Z=0 平面的距离,为元素添加透视效果

透视效果:近大远小、近实远虚

属性:(添加给父级,取值范围 800-1200
perspective: 视距

示例:

<!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>
       /* 视距属性必须直接添加给 直接父级 */
       .father {
           perspective: 1000px;
           /* perspective: 10000px;
           perspective: 100px; */
       }
       .son {
           width: 200px;
           height: 200px;
           margin: 100px auto;
           background-color: pink;
           transition: all 0.5s;
       }
       .son:hover {
           transform: translateZ(-300px);
           transform: translateZ(300px);
       }
   </style>
</head>
<body>
   <div class="father">
       <div class="son"></div>
   </div>
</body>
</html>
旋转
  • transform: rotateZ(值);

示例:

<!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>
       .box {
           width: 300px;
           margin: 100px auto;
       }
       img {
           width: 300px;
           transition: all 2s;
       }
       .box img:hover {
           transform: rotateZ(360deg);
       }
   </style>
</head>
<body>
   <div class="box">
       <img src="./images/hero.jpeg" alt="">
   </div>
</body>
</html>
  • transform: rotateX(值);

示例:

<!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>
       .box {
           width: 300px;
           margin: 100px auto;
       }
       img {
           width: 300px;
           transition: all 2s;
       }
       .box {
           /* 透视效果:近大远小、近实远虚 */
           perspective: 1000px;
       }
       .box img:hover {
           transform: rotateX(60deg);
           transform: rotateX(-60deg);
       }
   </style>
</head>
<body>
   <div class="box">
       <img src="./images/hero.jpeg" alt="">
   </div>
</body>
</html>
  • transform: rotateY(值);

示例:

<!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>
       .box {
           width: 300px;
           margin: 100px auto;
       }
       img {
           width: 300px;
           transition: all 2s;
       }
       .box {
           /* 透视效果:近大远小、近实远虚 */
           perspective: 1000px;
       }
       .box img:hover {
           transform: rotateY(60deg);
           transform: rotateY(-60deg);
       }
   </style>
</head>
<body>
   <div class="box">
       <img src="./images/hero.jpeg" alt="">
   </div>
</body>
</html>
  • 左手法则 - 根据旋转方向确定取值正负
    左手握住旋转轴,拇指指向正值方向,其他四个手指弯曲方向为旋转正值方向
  • 拓展
    • rotate3d(x, y, z, 角度度数):用来设置自定义旋转轴的位置及旋转的角度
    • x, y, z 取值为0-1之间的数字
立体呈现 - transform-style

作用:设置元素的子–元素是位于 3D 空间中还是平面==中

属性名:transform-style

属性值:

  • flat:子级处于平面
  • preserve-3d:子级处于 3D 空间
  • 呈现立体图形的步骤
    1. 父元素添加transform-style: preserve-3d;
    2. 子级定位
    3. 调整子盒子的位置(位移或旋转)

示例:

<!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>
       .cube {
           position: relative;
           width: 200px;
           height: 200px;
           margin: 100px auto;
           /* background-color: pink; */
           transition: all 2s;
           transform-style: preserve-3d;
           /* transform: rotateY(89deg); */
       }
       .cube div {
           position: absolute;
           left: 0;
           top: 0;
           width: 200px;
           height: 200px;
       }
       .front {
           background-color: orange;
           transform: translateZ(100px);
       }
       .back {
           background-color: green;
           transform: translateZ(-100px);
       }
       .cube:hover {
           transform: rotateY(90deg);
       }
   </style>
</head>
<body>
   <div class="cube">
       <div class="front">前面</div>
       <div class="back">后面</div>
   </div>
</body>
</html>
缩放
  • 属性

    transform: scale3d(x, y, z)
    transform: scaleX();
    transform: scaleY();
    transform: scaleZ();
    

2、动画 - animation

体验
  • 过渡:实现两个状态间的拜年话过程
  • 动画:实现多个状态间的变化过程,动画过程可控(重复播放、最终画面、是否暂停)

示例:

<!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>
       div {
           width: 100px;
           height: 100px;
           background-color: pink;
           animation: change 1s infinite alternate;
       }
       @keyframes change {
           0% {
               transform: translate(0);
           }
           100% {
               transform: translate(600px);
           }
       }
   </style>
</head>
<body>
   <div></div>
</body>
</html>
实现步骤
  1. 定义动画

    @keyframes 动画名称 {
        from {}
        to {}
    }
    @keyframes 动画名称 {
        0% {}
        10% {}
        ......
        100% {}
    }
    
  2. 使用动画
    animation: 动画名称 动画花费时长;

示例:

<!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>
       .box {
           width: 200px;
           height: 100px;
           background-color: pink;
           animation: change 1s;
       }
       /* 动画一:宽度从200变化到800 */
       /* @keyframes change {
           from {
               width: 200px;
           }
           to {
               width: 800px;
           }
       } */

       /* 动画二:从 200*100 变化到 300*300 变化到 800*500 */
           /* 百分比:表示的意思是动画时长的百分比 */
       @keyframes change {
           0% {
               width: 200px;
               height: 100px;
           }
           20% {
               width: 300px;
               height: 300px;
           }
           100% {
               width: 800px;
               height: 500px;
           }
       }
   </style>
</head>
<body>
   <div class="box"></div>
</body>
</html>
属性

animation: 动画名称 动画时长 速度曲线 延迟时间 重复次数 动画方向 执行完毕时状态;

提示:

  • 动画名称动画时长必须赋值
  • 取值分先后顺序
  • 如果有两个时间值,第一个时间表示动画时长第二个时间表示延迟时间

示例:

<!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>
       .box {
           width: 200px;
           height: 100px;
           background-color: pink;
               /* linear:匀速 */
           animation: change 1s linear;
               /* steps:分布动画,工作中,配合精灵图实现精灵动画 */
           animation: change 1s steps(3);
               /* 如果有两个时间值,第一个时间表示动画时长,第二个时间表示延迟时间 */
           animation: change 1s 2s;
               /* 重复次数:infinite:无限循环 */
           animation: change 1s 3;
           animation: change 1s infinite;
               /* alternate:反向 */
           animation: change 1s infinite alternate;
               /* 动画执行完毕时的状态:forwards:结束状态;backwards:开始状态 */
           animation: change 1s forwards;
           animation: change 1s backwards;
       }

       /* 宽度从 200 变化到 800 */
       @keyframes change {
           from {
               width: 200px;
           }
           to {
               width: 800px;
           }
       }
   </style>
</head>
<body>
   <div class="box"></div>
</body>
</html>
属性作用取值
animation-name动画名称
animation-duration动画时长
animation-delay延迟时间
animation-fill-mode动画执行完毕时的状态forwards:最后一帧
backwards:第一帧状态
animation-timing-function速度曲线steps(数字):逐帧动画
animation-iteration-count重复次数infinite为无限循环
animation-direction动画执行方向alternate为反向
animation-play-state暂停动画paused为暂停,通常配合==:hover==使用

示例:

<!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>
       .box {
           width: 200px;
           height: 100px;
           background-color: pink;
               /* 动画名称 */
           animation-name: change;
               /* 动画时长 */
           animation-duration: 1s;
               /* 播放次数 */
           animation-iteration-count: infinite;
       }
       .box:hover {
               /* 暂停动画 */
           animation-play-state: paused;
       }
       /* 宽度从 200 变化到 800 */
       @keyframes change {
           0% {
               width: 200px;
           }
           100% {
               width: 800px;
           }
       }
   </style>
</head>
<body>
   <div class="box"></div>
</body>
</html>
逐帧动画
属性作用取值
animation-timing-function速度曲线steps(数字):逐帧动画

核心原理:

  1. steps() 逐帧动画
  2. CSS 精灵图

精灵动画制作步骤:

  1. 准备显示区域
    盒子尺寸与一张精灵小图尺寸相同
  2. 定义动画
    移动背景图(移动距离 = 精灵图宽度)
  3. 使用动画
    steps(N),N 与精灵小图个数相同

示例:

<!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>
       div {
           width: 140px;
           height: 140px;
           border: 1px solid #000;
           background-image: url(./images/bg.png);
           animation: run 1s steps(12) infinite;
       }
       @keyframes run {
           from {
               background-position: 0 0;
           }
           to {
               background-position: -1680px 0;
           }
       }
   </style>
</head>
<body>
   <div></div>
</body>
</html>
多组动画
animation:
	动画1,
	动画2,
	动画N
;
animation:
	run 1s steps(12) infinite,
	move 3s linear forwards
;

示例:

<!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>
       div {
           width: 140px;
           height: 140px;
           background-image: url(./images/bg.png);
           animation: 
               run 1s steps(12) infinite,
               move 3s linear forwards
           ;
       }
       /* 当动画的开始状态样式跟盒子默认样式相同时,可以省略动画开始状态的代码 */
       @keyframes run {
           /* from {
               background-position: 0 0;
           } */
           to {
               background-position: -1680px 0;
           }
       }
       @keyframes move {
           /* 0% {
               transform: translate(0);
           } */
           100% {
               transform: translate(800px);
           }
       }
   </style>
</head>
<body>
   <div></div>
</body>
</html>
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值