CSS之动画(奔跑的熊、两面反转盒子、3D导航栏、旋转木马)

一、 2D转换
1.1 transform: translate( )
转换(transform) 是CSS3中具有颠覆性的特征之一,可以实现元素的位移、旋转、缩放等效果
移动:translate
旋转:rotate
缩放:scale
下图为2D转换的坐标系

回忆:移动盒子位置的三种方法:1. margin 2.position 3.2D转换
注意:盒子经过变化后,本来拥有的位置也会进行保留。也不是说不会影响其他的元素
1.2. transform: translatex. (50%)
移动的数值改为百分数则代表了盒子本身宽度乘50%。但是对于行内标签无效。

复制代码
//最简单的实现盒子居中显示效果的方法
 position:absolute;
        top: 50%;
            left: 50%;
        width: 100px;
        height: 100px;
        background-color: red;
        transform: translate(-50%,-50%);

1.3 旋转

transform: translate(x deg) x为正,则顺时针旋转。默认旋转点是元素的中心位置

transform-origin:x y; x y 可以是像素或者也可以是方位名词top bottom left right

旋转中心动画案例:

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .box{
        margin: 200px auto;
        width: 200px;
        height: 200px;
        background-color: pink;
        border: 1px solid black;
        overflow: hidden;
    }
.box::before{
    display: block;
    content: '温教授讲三农';
    width: 100%;
    height: 100%;
    background-color: hotpink;
    transform: rotate(180deg);
    transform-origin:left bottom;
    /* 谁做过渡给谁加 */
transition: all .7s;
}

/* 鼠标经过div盒子盒子复原 */
.box:hover::before{
    transform: rotate(0deg);
}
</style>
<body>
    <div class="box"></div>
</body>
</html>

1.4 Scale

transform: scale(x,y) x轴变为x倍,y变y倍

transform:scale(2) 宽、高都变成两倍

scale的优势:1. 可以设置缩放的中心点 2.不会影响其他的盒子

二、CSS动画

动画简写

前两个属性不可省略

奔跑的熊,旧百度浏览器

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    body{
        background-color: #ccc;
    }

    div{
        position: absolute;
        width: 200px;
        height: 100px;
        background: url(media/bear.png)no-repeat;
        animation: bear 1s steps(8) infinite,move 2s linear forwards;
    }

    @keyframes bear{
        0% {
            background-position: 0 0;
        }

        100% {
            background-position: -1600px 0;
        }
    }

    @keyframes move {
        0% {
            left: 0;
        }

        100% {
            left: 50%;
            margin-left: -100px;
        }
        
    }
</style>
<body>
    <div></div>
</body>
</html>

定义动画

复制代码
@keyframes 动画名称 {
0%{
width:100px;
} 
100%{
width:200px;
}
}

/* 调用动画 */
animation-name: 动画名称;
/* 持续时间 */
animation-duration: 持续时间;

动画的常用属性

动画的简写形式:类似background
animation: myfirst 5s linear 2s infinite alternate;
前两个一般不可以省略

三、3D动画
动画视角的三维坐标系


3D位移: translate3d(x,y,z)

3D旋转: rotate3d(x,y,z)

透视: perspective

3D呈现 transfrom-style
带有xyz的都不可以省略掉
可以使用左手定责来确定该向什么方向进行旋转
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>
</head>
<style>
    *{
        padding: 0;
         margin: 0;
    }

    ul {
        margin: 100px;
    }

    ul li{
        width: 120px;
        height: 35px;
        list-style:none;
        perspective: 300px;
    }

    .box{
        position: relative;
        width: 100%;
        height: 100%;
        transform-style: preserve-3d;
        transition: all .6s;
    }

    .box:hover{
        transform: rotateX(90deg);
    }

    .front,
    .back{
        position: absolute;
        left: 0;
        top:0;
        width: 100%;
        height: 100%;
    }

    .front{
        background-color:pink;
        z-index: 1;
        transform: translateZ(17.5px);
    }

    .back{
        /* transform: rotateX('90deg'); */
        background-color: aquamarine;
        transform: translateY(17.5px) rotateX(-90deg);
    }
</style>
<body>
    <ul>
        <li>
            <div class="box">
                <div class="front">韩愈文集1</div>
                <div class="back">感二鸟赋</div>
            </div>
        </li>
    </ul>
</body>

</html>
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D旋转案例之旋转木马</title>
</head>
<style>
    body{
        perspective: 1200px;
    }
    section{
        position: relative;
        width: 500px;
        height: 300px;
        margin: 50px auto;
        transform-style: preserve-3d;
        animation: rotate 4s linear infinite;
        background: url(media/dezoomify-result\(50\).jpg) no-repeat;
    }

    @keyframes rotate{
        0%{
            transform: rotateY(0);
        }
        100%{
            transform: rotateY(360deg);
        }
    }

    section div{
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: url(media/dog.jpg) no-repeat;
    }

    section div:nth-child(1){
        transform: translateZ(300px);
    }

    section div:nth-child(2){
        transform: rotateY(60deg) translateZ(300px);

    }

    section div:nth-child(3){
        transform: rotateY(120deg) translateZ(300px);

    }

    section div:nth-child(4){
        transform: rotateY(180deg) translateZ(300px);

    }
    section div:nth-child(5){
        transform: rotateY(240deg) translateZ(300px);

    }
    section div:nth-child(6){
        transform: rotateY(300deg) translateZ(300px);

    }
    section div:nth-child(7){
        transform: rotateY(360deg) translateZ(300px);

    }
</style>
<body>
    <section>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </section>
</body>
</html>
相关推荐
江城开朗的豌豆2 分钟前
JavaScript篇:解密JS执行上下文:代码到底是怎么被执行的?
前端·javascript·面试
EndingCoder1 小时前
React从基础入门到高级实战:React 高级主题 - React 微前端实践:构建可扩展的大型应用
前端·javascript·react.js·前端框架·状态模式
BigTopOne2 小时前
【ijkplayer】 android 初始化硬解码
前端
1024小神2 小时前
rust或tauri项目执行命令的时候,cmd窗口也会弹出显示解决方法
前端·javascript
橙某人2 小时前
🤝和Ollama贴贴!解锁本地大模型的「私人订制」狂欢🎉
前端·deepseek
贩卖纯净水.2 小时前
Webpack搭建本地服务器
前端·webpack·node.js
brzhang2 小时前
iOS 26 的备忘录,终于他娘的要支持 Markdown 了!
前端·后端·架构
云边散步3 小时前
《校园生活平台从 0 到 1 的搭建》第一篇:创建项目与构建目录结构
前端·javascript·vue.js
前端老爷更车3 小时前
Threejs 渲染阴影流程
前端
鸭蛋超人不会飞3 小时前
H5引入微信SDK
前端