移动Web——平面转换-平移

1、平面转换-平移

取值

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

技巧

  • translate()只写一个值,表示沿着X轴移动
  • 单独设置X或Y轴移动距离:translateX()或translateY()
html 复制代码
<!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 {
            width: 500px;
            height: 300px;
            margin: 100px auto;
            border: 1px solid #000;
        }

        .son {
            width: 200px;
            height: 100px;
            background-color: pink;
            transition: all 0.5s;
        }

        /* 鼠标移入到父盒子,son改变位置 */
        .father:hover .son {
            transform: translate(200px, 100px);

            /* 取值是百分比:参照盒子自身尺寸计算结果 */
            transform: translate(50%, 100%);
            transform: translate(-50%, 100%);

            /* 只写一个数表示水平方向 */
            transform: translate(100%);

            transform: translateY(100%);
            transform: translateX(100%);
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

2、平移实现居中效果

方法一:

方法二:百分比参照盒子自身尺寸计算结果

3、案例 双开门效果

html 复制代码
<!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;
        }

        /* 1. 布局:父子结构,父级是大图,子级是左右小图*/
        .father {
            display: flex;
            margin: 0 auto;
            width: 1300px;
            height: 600px;
            background-image: url(../photo/1.jpg);

            overflow: hidden;
        }

        .father .left,
        .father .right {
            width: 50%;
            height: 600px;
            background-image: url(../photo/2.jpg);

            transition: all .5s;
        }

        .father .right {
            /* right 表示的取到精灵图右面的图片 */
            background-position: right 0;
        }
       /* 2. 鼠标悬停的效果:左右移动 */
       .father:hover .left {
            transform: translate(-100%);
       }

       .father:hover .right {
        transform: translate(100%);
       }
    </style>
</head>
<body>
    <div class="father">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>
</html>
相关推荐
科普瑞传感仪器1 分钟前
基于六维力传感器的机器人柔性装配,如何提升发动机零部件装配质量?
java·前端·人工智能·机器人·无人机
步步为营DotNet3 分钟前
深入理解IAsyncEnumerable:.NET中的异步迭代利器
服务器·前端·.net
JackieDYH5 分钟前
CSS滚动吸附详解:构建精准流畅的滚动体验-scroll-snap-type
前端·css
CodeCraft Studio26 分钟前
纯前端文档编辑组件——Spire.WordJS全新发布
前端·javascript·word·office·spire.wordjs·web文档编辑·在线文档编辑器
前端一课30 分钟前
第 32 题:Vue3 Template 编译原理(Template → AST → Transform → Codegen → Render 函数)
前端·面试
前端一课34 分钟前
第 33 题:Vue3 v-model 原理(语法糖 → props + emit → modelValue → update:modelValue)
前端·面试
前端一课38 分钟前
第 25 题:说一下 Vue3 的 keep-alive 原理?缓存是怎么做的?
前端·面试
前端一课41 分钟前
第 30 题:Vue3 自定义渲染器(Custom Renderer)原理- 为什么 Vue 能渲染到 DOM / Canvas / WebGL / 三方平台
前端·面试