移动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>
相关推荐
发现一只大呆瓜2 小时前
Vite 开发预构建机制详解,搞懂 esbuild 与 Rollup 分工差异
前端·面试·vite
小贺儿开发2 小时前
一句话生成网页 + 自动化办公(OpenCode + DeepSeek-V4)
css·自动化·html·工具·代码·网页·deepseek
九九落3 小时前
前端获取经纬度完全指南:从Geolocation API到地图集成
前端·获取经纬度
来恩10034 小时前
jQuery选择器
前端·javascript·jquery
前端繁华如梦4 小时前
树上挂苹果还是挂玻璃球?Three.js 程序化果实的完整实现指南
前端·javascript
墨痕诉清风4 小时前
Web浏览器客户端检测网站网络健康(代码)
前端·网络·测试工具
IMPYLH4 小时前
Linux 的 wc 命令
linux·运维·服务器·前端·bash
happybasic4 小时前
Python库升级标准流程~
linux·前端·python
川冰ICE4 小时前
前端工程化深度实战:从Webpack5到Vite5的构建工具演进与选型决策
前端
CDwenhuohuo4 小时前
优惠券组件直接用 uview plus
前端·javascript·vue.js