css3 2D位移transfrom

基本语法:transform:translate()
将元素再水平或垂直方向上移动指定距离
translate(参数1,参数2):水平方向平移和垂直方向位移
translate(参数):只能实现水平方向的移动
translateX(参数):水平方向位移
translateY(参数):垂直方向位移

   <style>
        div{
            background-color: red;
            width: 300px;
            height: 300px;
            margin: 50px auto;
        
            transition:all 3s;



        }
        div:hover{
        
            transform: translate(50px,0)//正数往左,负数往右
        }
    </style>

当参数是百分比的单位时。位移的长度是参照本身大小的

## **位移可以配合绝对定位实现水平垂直居中**
   div{
            background-color: red;
            width: 300px;
            height: 300px;
            margin: 50px ;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);

        }

案例

    <style>
*{
    margin: 0;
    padding: 0;
}
img{
    transition:all 1s;
}
    .box{
        width: 1000px;
        height: 600px;
        background:url(../image/bg.jpg) ;
        margin:50px auto;
        position: relative;
        overflow: hidden;
    }
    img:nth-child(1){
        position: absolute;
        left: 100px;
        top: 50px;
       
    }
    img:nth-child(2){
        position: absolute;
        left: 1000px;
        bottom: 600px;
    }
    img:nth-child(3){
        position: absolute;
        left: 1000px;
        top: 600px;
    }
    .box:hover img:nth-child(1){
        transform: translate(-90px);

    }
    .box:hover img:nth-child(2){
        transform: translate(-400px,275px);

    }
    .box:hover img:nth-child(3){
        transform: translate(-400px,-275px);

    }
    </style>
</head>
<body>
    <div class="box">
        <img src="../image/01.jpg" alt="">
        <img src="../image/02.jpg" alt="">
        <img src="../image/03.jpg" alt="">

    </div>
    </body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值