css3——2D转换

移动-translate

2D移动是2D转换里面的一种功能,可以改变元素在页面中的位置,类似定位。

transform: translate(x, y);
transform: translateX(n);
transform: translateY(n);
     /* 移动盒子位置:定位、盒子的外边距、 2d转换移动*/
        div {
            width: 200px;
            height: 200px;
            background-color: aquamarine;

            /* 过渡效果 */
            transition: all 1s;
        }

        div:hover {
            /* 移动x轴 */
            /* transform: translateX(200px); */

            /* 移动y轴 */
            /* transform: translateY(100px) */
            /* x就是x轴上移动 y就是y轴上移动位置 中间用逗号分隔 */
            transform: translate(100px, 100px);
        }
    </style>
</head>

<body>
    <div></div>
</body>

在这里插入图片描述

重点

  • translate最大优点:不会影响其他元素的位置,而且还不会脱标(使用定位absolute会脱标)。margin添加margin-top后,下面元素会随之移动】
  • translate中百分比单位是相对于自身元素的translate(50%,50%);
  • 对行内标签没有效果

不会影响其他元素案例
使用margin-top移动元素

    <style>
    	.top {
            width: 200px;
            height: 200px;
            background-color: palegreen;
            margin-top: 100px;
        }

        .bottom {
            width: 200px;
            height: 200px;
            background-color: orange;
        }
    </style>
</head>

<body>
    <div class="top"></div>
    <div class="bottom"></div>
</body>

在这里插入图片描述

使用translate移动元素

<style>
    .top {
        width: 200px;
        height: 200px;
        background-color: palegreen;
        transform: translateY(100px);
    }

    .bottom {
        width: 200px;
        height: 200px;
        background-color: orange;
    }
</style>

在这里插入图片描述
应用: 鼠标hover时,图片向上浮动效果。使用translate最佳,因为向上浮动,不会影响其他盒子效果。

div盒子居中

盒子居中方法:

  1. 设定具体数据的margin-left以及margin-top

  2. 使用定位方法:

    水平居中:margin-left:50%,left:-(自身宽度的一半)

    垂直居中:margin-top:50%,top:-(自身高度的一半)

        <style>
            .father {
                position: relative;
                width: 400px;
                height: 400px;
                background-color: paleturquoise;
            }
    
            .son {
                position: absolute;
                /* 水平居中 */
                left: 50%;
                margin-left: -100px;
                /* 垂直居中 */
                top: 50%;
                margin-top: -50px;
    
                width: 200px;
                height: 100px;
                background-color: pink;
            }
        </style>
    </head>
    
    <body>
        <div class="father">
            <div class="son"></div>
        </div>
    </body>
    

    在这里插入图片描述
    3.使用定位+translate

    <style>
        .father {
            position: relative;
            width: 400px;
            height: 400px;
            background-color: paleturquoise;
        }

        .son {
            position: absolute;
            /* 水平居中 */
            left: 50%;
            top: 50%;
            /* 使盒子x轴以及y轴向左和上移动自身的50% */
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>

在这里插入图片描述

旋转-rotate

语法

trasform:rotate(度数)
  • rotate单位是度数,即deg,如 rotate(90deg)
    <style>
        img {
            width: 500px;
            margin: 200px;
            /* 顺时针旋转 45度*/
            transform: rotate(45deg);
            /* 逆时针旋转 */
            /* transform: rotate(-45deg); */

        }

        .img:hover {}
    </style>
</head>

<body>
    <img src="xiaoxin.jpg" alt="">
</body>

在这里插入图片描述

图片旋转效果
  <style>
        img {
            width: 500px;
            margin: 200px;
          	 /*过渡效果*/
            transition: all 3s;

        }

        img:hover {
            transform: rotate(360deg);
        }
    </style>
</head>

<body>
    <img src="xiaoxin.jpg" alt="">
</body>

在这里插入图片描述

三角形案例

<style>
        div {
            position: relative;
            width: 300px;
            height: 40px;
            border: 1px solid #000;
        }

        div::after {
            content: "";
            position: absolute;
            left: 270px;
            top: 6px;
            transform: rotate(45deg);
            width: 20px;
            height: 20px;
            border-bottom: 1px solid #000;
            border-right: 1px solid #000;
            transition: all .6s;
        }

        div:hover::after {
            top: 16px;
            transform: rotate(225deg);
        }
    </style>
</head>

<body>

    <div></div>

</body>

在这里插入图片描述

设置转换中心点-transform-orgin

语法

transform-orgin:x y;

重点

  • 注意后面的参数x和y用空格隔开
  • x y默认转换的中心点是元素的中心点(50% 50%)
  • 还可以给x y设置像素或者方位名词(top bottom left right center)
  <style>
        div {
            width: 200px;
            height: 200px;
            background-color: palegreen;
            margin: 100px auto;
            transition: all 1s;
            /* 设置旋转中心点: 方位名词 */
            transform-origin: left bottom; 

            /* 使用单位px */
            /* transform-origin: 50px 50px; */
        }

        div:hover {
            transform: rotate(360deg);
        }
    </style>
</head>

<body>

    <div></div>

</body>

在这里插入图片描述

旋转中心点案例

 <style>
        div {
            width: 200px;
            height: 200px;
        }

        .father {
            background-color: blue;
            overflow: hidden;
        }

        .son {
            text-align: center;
            font-size: 40px;
            line-height: 200px;
            color: #fff;
            background-color: skyblue;
            transition: all 2s;
            transform-origin: left bottom;


        }


        .son:hover.son {
            transform: rotate(180deg);
        }
    </style>
</head>

<body>

    <div class="father">
        <div class="son">
            前端
        </div>
    </div>

</body>

在这里插入图片描述

方法二

 <style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
            overflow: hidden;
            margin: 10px;
            float: left;
        }


        div::before {
            content: "前端";
            display: block;
            width: 100%;
            height: 100%;
            text-align: center;
            font-size: 40px;
            line-height: 200px;
            color: #fff;
            background-color: hotpink;
            /* 旋转效果 */
            transition: all 0.4s;
            transform-origin: left bottom;


        }


        div:hover::before {
            transform: rotate(180deg);
        }
    </style>
</head>

<body>
    <div></div>
    <div></div>
    <div></div>

</body>

在这里插入图片描述

缩放-scale

语法

transform:scale(x,y);

注意

  • transform:scale(1,1):宽度和高度都放大一倍,等于没有放大
  • transform:scale(2,2):高和宽都放大2倍
  • transform:scale(2):只写一个参数,第二个参数和第一个参数一样,相当于scale(2,2)
  • transform:scale(0.5,0.5):缩小
  • scale缩放最大的优势:可以设置中心缩放点,默认以中心点缩放,而且不影响其他盒子
    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: aqua;
            /* 放大 */
            transform: scale(2, 2);
			/*缩小*/
            /*transform:scale(0.5);*/
        }
    </style>
</head>

<body>
    <div></div>
</body>

未缩放效果
在这里插入图片描述
缩放后
在这里插入图片描述
变换缩放点

    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: aqua;

            /* 变换缩放点*/
            transform: scale(0.5);
            transform-origin: right bottom;
        }
    </style>
</head>

<body>
    <div></div>
</body>

在这里插入图片描述
scale优势

缩放也可以通过同时修改宽高来缩放,有何不同?

答: scale不会影响其他盒子,而且可以设置缩放中心点。通过修改宽高进行缩放,影响其他盒子,修改时往两边延伸。

 <style>
        div {
            width: 200px;
            height: 200px;
            background-color: aqua;
            margin: 100px auto;
        }

        /* 会影响下面元素,缩放时两边延伸 */
        /* div:hover {
            width: 400px;
            height: 400px;
        } */

        /* 不会影响下面元素 */
        div:hover {
            transform: scale(2);
        }
    </style>
</head>

<body>
    <div></div>
    BALCKPINK

修改宽高
在这里插入图片描述
scale缩放
在这里插入图片描述

案例-【经过图片放大效果】
 <style>
        div {
            width: 300px;
            float: left;
            margin: 10px;
            /* 进行放大后,超出部分hidden */
            overflow: hidden;
        }

        img {
            width: 100%;
            /* 过渡效果 */
            transition: all .5s;
        }

        img:hover {
            transform: scale(1.3);
        }
    </style>
</head>

<body>
    <div><a href="#"><img src="xiaoxin.jpg" alt=""></a></div>
    <div><a href="#"><img src="xiaoxin.jpg" alt=""></a></div>
    <div><a href="#"><img src="xiaoxin.jpg" alt=""></a></div>
</body>

在这里插入图片描述

案例-分页效果
<style>
        div {
            width: 400px;
            height: 100px;
            border: 1px solid #000;
        }

        ul {

            background-color: blueviolet;
        }

        li {
            width: 10px;
            height: 10px;
            line-height: 10px;
            border-radius: 30px;
            list-style: none;
            float: left;
            padding: 15px;
            margin: 4px;
            border: 1px solid aquamarine;

            /* 过渡效果 */
            transition: all .5s;
        }

        li:hover {
            transform: scale(1.2);
        }
    </style>
</head>

<body>
    <div>
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
        </ul>
    </div>

</body>

在这里插入图片描述

2D转换综合写法

  • 同时使用多个转换,格式

     transform: translate() rotate() scale();
    

    无顺序,中间空格隔开

  • 其顺序会影响转换效果(先旋转会改变坐标方向)

  • 同时有位移和其他属性,将位移放最前面(例如先旋转再位移,旋转后会改变坐标方向,出现不理想结果)

 <style>
        div {
            width: 200px;
            height: 200px;
            /* margin: 100px auto; */
            background-color: orange;
            transition: all .5s;
        }

        div:hover {
            transform: translate(150px, 150px) rotate(360deg) scale(1.5);
        }
    </style>
</head>

<body>
    <div>

    </div>

</body>

在这里插入图片描述

总结

  • translate(x,y),移动,不影响其他盒子,单位:px,%,相对于自身高度和宽度来计算,可以分开translateX(x),translateY(y),对行内元素不起作用。
  • rotate(度数),旋转。不影响其他盒子,单位:deg,
  • scale(x,y),缩放。参数为数字。可以是小数点。不影响其他盒子
  • 设置转换中心点 transform-orgin:x,y。 参数可以是百分比,像素或者方位名词
  • 进行综合写法是,同时有位移和其他属性,位移放前面

此笔记来源于黑马pink老师,希望大家多多点赞啊!!!https://space.bilibili.com/415434293
完结撒花
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值