• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

CSS3学习第七天

武飞扬头像
你还要我怎样?
帮助1

1、2d-平移translate()

translate将元素向指定的方向移动,类似于position中的relative。


水平移动:向右移动translate(tx,0)和向左移动translate(-tx,0);

垂直移动:向上移动translate(0,-ty)和向下移动translate(0,ty);

对角移动:右下角移动translate(tx,ty)

                  右上角移动translate(tx,-ty)

                  左上角移动translate(-tx,-ty)

                  左下角移动translate(-tx,-ty)。


translateX():水平方向移动一个对象。对象只向x轴进行移动,如果值为正值,对象向右移动

                                                                                                        如果值为负值,对象向左移动

translateY():纵轴方向移动一个对象,对象只向Y轴进行移动,如果值为正值,对象向下移动

                                                                                                         如果值为负值,对象向上移动

这两个函数和前面介绍的translate()函数不同的地方是每个方向只接受一个值。

所以:

transform:translate(-100px,0)之际上等于transform:translateX(-100px)

transform:translate(0,-100px)之际上等于transform:translateY(-100px)


设置left属性会频繁的造成浏览器回流重排,而transform和opacity属性不会,因为它是作为合成图发送到GPU上,由显卡执行的渲染,这样做的优化如下:

  • 可以通过亚像素精度得到一个运行在特殊优化过的单位图形任务上的平滑动画,并且运行非常快
  • 动画不再绑定到CPU重排,而是通过GPU合成图像,即使运行一个非常复杂到的JavaScript任务,动画仍然会很快运行。

代码练手:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title>Document</title>
  8.  
    <style>
  9.  
    div{
  10.  
    width: 200px;
  11.  
    height: 200px;
  12.  
    background: rebeccapurple;
  13.  
    margin: 0 auto ;
  14.  
    transition: all 2s;
  15.  
    /* 最开始让它定位在平移50px位置处 */
  16.  
    /* transform: translate(50px); */
  17.  
    /* px值会叠加,也就是如果你设置transform: translate(100px,100px)
  18.  
    那么div在50px处则在x轴上向右移动50px
  19.  
    y轴上向下移动100px
  20.  
    */
  21.  
    }
  22.  
    div:hover{
  23.  
    /* 100px 往右平移100px */
  24.  
    /* -100px 往左平移100px */
  25.  
    /* translateX(100px) 往右平移100px*/
  26.  
    /* translateX(-100px) 往左平移100px*/
  27.  
    /* translateY(100px) 往下平移100px */
  28.  
    /* translateY(-100px) 往上平移100px */
  29.  
     
  30.  
     
  31.  
    /* 对角写法 */
  32.  
    /* 第一种写法 */
  33.  
    /* transform: translateX(100px) translateY(-100px); */
  34.  
    /* 第二种写法 */
  35.  
    transform: translate(100px,100px);
  36.  
    }
  37.  
    </style>
  38.  
    </head>
  39.  
    <body>
  40.  
    <div></div>
  41.  
    </body>
  42.  
    </html>
学新通

2、2d-缩放scale()

让元素根据中心原点对对象进行缩放,默认的值1.因此0.01到0.99之间的任何值,使一个元素缩小;而任何大于或等于1.01的值,让元素显得更大。

缩放scale()函数和translate()函数的语法非常相似,他可以接受一个值,也可以同时接受两个值,如果只有一个值时,其第二个值默认与第一个值相等。

例如:

scale(1,1)元素不会有任何变化,而scale(2,2)让元素沿X轴和Y轴放大两倍。

scaleX():相当于scale(sx,1)。表示元素只在X轴(水平方向)缩放元素,其默认值是1。

scaleY():相当于scale(1,sy)。表示元素只在Y轴(垂直方向)缩放元素,其默认值是1


 scale()里边填负值-1.5,是倒向放大的效果

                                    -0.5  是倒放缩小的效果

支持x轴  ,y轴单独放大scaleX(),scaleY()


代码练手:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title>Document</title>
  8.  
    <style>
  9.  
    div{
  10.  
    width: 300px;
  11.  
    border: 5px solid rebeccapurple ;
  12.  
    margin: 0% auto;
  13.  
    /* 放大溢出隐藏 */
  14.  
    /* overflow: hidden; */
  15.  
    }
  16.  
    img{
  17.  
    width: 100%;
  18.  
    transition: all 2s;
  19.  
    /* 改变中心点的位置,从左上角开始放大 */
  20.  
    /* 值:center(默认值),left top,left bottom,left center ,right top,right center,right bottom*/
  21.  
    transform-origin: left top;
  22.  
    }
  23.  
    div:hover img{
  24.  
    transform: scale(1.5);
  25.  
     
  26.  
    }
  27.  
    </style>
  28.  
    </head>
  29.  
    <body>
  30.  
    <div>
  31.  
    <img src="1.jpg" alt="">
  32.  
    </div>
  33.  
    </body>
  34.  
    </html>
学新通

3、2d-旋转rotate()

学新通

 rotate(绕着中心转)===rotateZ

学新通

transform-origin:用来改变旋转中心的点

4、2d-旋转-折扇效果案例

效果:

                鼠标未放上去之前                                                      鼠标放上去之后

学新通学新通 

代码:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title>Document</title>
  8.  
    <style>
  9.  
    *{
  10.  
    margin: 0%;
  11.  
    padding: 0%;
  12.  
    }
  13.  
    ul{
  14.  
    list-style: none;
  15.  
    }
  16.  
     
  17.  
    ul{
  18.  
    margin: 10px auto;
  19.  
    width: 600px;
  20.  
    height: 400px;
  21.  
    border: 5px solid gray;
  22.  
    position: relative;
  23.  
     
  24.  
    }
  25.  
    li{
  26.  
    width: 60px;
  27.  
    height: 200px;
  28.  
    position: absolute;
  29.  
    left: 50%;
  30.  
    margin-left: -30px;
  31.  
    bottom: 20px;
  32.  
    text-align: center;
  33.  
    /* 调整中心点 */
  34.  
    transform-origin: bottom center;
  35.  
    /* 圆角 */
  36.  
    border-radius: 10px;
  37.  
     
  38.  
    transition: all 2s;
  39.  
    box-shadow: 5px 5px 5px black;
  40.  
    }
  41.  
    ul li:not(:nth-child(7)){
  42.  
    opacity: 0;
  43.  
    }
  44.  
    ul:hover li{
  45.  
    opacity: 1;
  46.  
    }
  47.  
    ul li:nth-child(1),ul li:nth-child(13){
  48.  
    background: purple;
  49.  
    }
  50.  
    ul li:nth-child(2),ul li:nth-child(12){
  51.  
    background: red;
  52.  
    }
  53.  
    ul li:nth-child(3),ul li:nth-child(11){
  54.  
    background: skyblue;
  55.  
    }
  56.  
    ul li:nth-child(4),ul li:nth-child(10){
  57.  
    background: green;
  58.  
    }
  59.  
    ul li:nth-child(5),ul li:nth-child(9){
  60.  
    background: orange;
  61.  
    }
  62.  
    ul li:nth-child(6),ul li:nth-child(8){
  63.  
    background: palevioletred;
  64.  
    }
  65.  
    ul li:nth-child(7){
  66.  
    background: yellow;
  67.  
    }
  68.  
     
  69.  
     
  70.  
    ul:hover li:nth-child(1){
  71.  
    transform: rotate(90deg);
  72.  
    }
  73.  
    ul:hover li:nth-child(13){
  74.  
    transform: rotate(-90deg);
  75.  
    }
  76.  
    ul:hover li:nth-child(2){
  77.  
    transform: rotate(75deg);
  78.  
    }
  79.  
    ul:hover li:nth-child(12){
  80.  
    transform: rotate(-75deg);
  81.  
    }
  82.  
    ul:hover li:nth-child(3){
  83.  
    transform: rotate(60deg);
  84.  
    }
  85.  
    ul:hover li:nth-child(11){
  86.  
    transform: rotate(-60deg);
  87.  
    }
  88.  
    ul:hover li:nth-child(4){
  89.  
    transform: rotate(45deg);
  90.  
    }
  91.  
    ul:hover li:nth-child(10){
  92.  
    transform: rotate(-45deg);
  93.  
    }
  94.  
    ul:hover li:nth-child(5){
  95.  
    transform: rotate(30deg);
  96.  
    }
  97.  
    ul:hover li:nth-child(9){
  98.  
    transform: rotate(-30deg);
  99.  
    }
  100.  
    ul:hover li:nth-child(6){
  101.  
    transform: rotate(15deg);
  102.  
    }
  103.  
    ul:hover li:nth-child(8){
  104.  
    transform: rotate(-15deg);
  105.  
    }
  106.  
    </style>
  107.  
    </head>
  108.  
    <body>
  109.  
    <ul>
  110.  
    <li>1</li>
  111.  
    <li>2</li>
  112.  
    <li>3</li>
  113.  
    <li>4</li>
  114.  
    <li>5</li>
  115.  
    <li>6</li>
  116.  
    <li>7</li>
  117.  
    <li>8</li>
  118.  
    <li>9</li>
  119.  
    <li>10</li>
  120.  
    <li>11</li>
  121.  
    <li>12</li>
  122.  
    <li>13</li>
  123.  
    </ul>
  124.  
    </body>
  125.  
    </html>
学新通

5、2d-多属性值

学新通

两个transform不要一起写,后面的会覆盖前面的

注意平移和缩放写的顺序

效果:

学新通

 代码:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title>Document</title>
  8.  
    <style>
  9.  
    .box{
  10.  
    width: 600px;
  11.  
    height: 600px;
  12.  
    border: 2px solid gray;
  13.  
    }
  14.  
    .box div{
  15.  
    width: 200px;
  16.  
    height: 200px;
  17.  
    background: red;
  18.  
    border: 1px solid black;
  19.  
    }
  20.  
    .box div:nth-child(1){
  21.  
    transform: translate(400px);
  22.  
    }
  23.  
     
  24.  
    /* 注意顺序 平移和缩放 */
  25.  
    .box div:nth-child(2){
  26.  
    transform: translate(400px) scale(0.5);
  27.  
    }
  28.  
    .box div:nth-child(3){
  29.  
    transform: scale(0.5) translate(400px);
  30.  
    }
  31.  
    </style>
  32.  
    </head>
  33.  
    <body>
  34.  
    <div class="box">
  35.  
    <div></div>
  36.  
    <div></div>
  37.  
    <div></div>
  38.  
    </div>
  39.  
    </body>
  40.  
    </html>
学新通

注意平移和旋转

效果:

学新通

代码:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title>Document</title>
  8.  
    <style>
  9.  
    .box{
  10.  
    width: 600px;
  11.  
    height: 600px;
  12.  
    border: 2px solid gray;
  13.  
    }
  14.  
    .box div{
  15.  
    width: 100px;
  16.  
    height: 100px;
  17.  
    background: red;
  18.  
    border: 1px solid black;
  19.  
    }
  20.  
    .box div:nth-child(1){
  21.  
    transform: translate(400px);
  22.  
    }
  23.  
     
  24.  
    /* 注意顺序 */
  25.  
    .box div:nth-child(2){
  26.  
    transform: translate(400px) rotate(45deg);
  27.  
    }
  28.  
    .box div:nth-child(3){
  29.  
    transform: rotate(45deg) translate(400px);
  30.  
    }
  31.  
    </style>
  32.  
    </head>
  33.  
    <body>
  34.  
    <div class="box">
  35.  
    <div></div>
  36.  
    <div></div>
  37.  
    <div></div>
  38.  
    </div>
  39.  
    </body>
  40.  
    </html>
学新通

6、2d-倾斜

学新通

 效果:

学新通

代码:

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title>Document</title>
  8.  
    <style>
  9.  
    .box1{
  10.  
    width: 200px;
  11.  
    height: 200px;
  12.  
    background: red;
  13.  
    line-height: 200px;
  14.  
    text-align: center;
  15.  
    border: 1px solid gray;
  16.  
    font-size: 50px;
  17.  
    margin: 0 auto;
  18.  
    transform: skewX(30deg);
  19.  
    /* skewX 正值,拽右下角,往右拉动,形成30deg
  20.  
    skewY 正值,拽着右下角,往下拉动,形成30deg
  21.  
    skew(x,y) 正值,拽住右下角,往右下角,形成30deg
  22.  
    */
  23.  
    }
  24.  
    .box2{
  25.  
    width: 200px;
  26.  
    height: 200px;
  27.  
    background: red;
  28.  
    line-height: 200px;
  29.  
    text-align: center;
  30.  
    border: 1px solid gray;
  31.  
    font-size: 50px;
  32.  
    margin: 0 auto;
  33.  
    transform: skewY(30deg);
  34.  
    }
  35.  
    .box3{
  36.  
    width: 200px;
  37.  
    height: 200px;
  38.  
    background: red;
  39.  
    line-height: 200px;
  40.  
    text-align: center;
  41.  
    border: 1px solid gray;
  42.  
    font-size: 50px;
  43.  
    margin: 0 auto;
  44.  
    transform: skew(30deg,30deg);
  45.  
    }
  46.  
    </style>
  47.  
    </head>
  48.  
    <body>
  49.  
    <div class="box1">hello</div>
  50.  
    <div class="box2">hello</div>
  51.  
    <div class="box3">hello</div>
  52.  
    </body>
  53.  
    </html>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgeiehe
系列文章
更多 icon
同类精品
更多 icon
继续加载