CSS3笔记

2020-07-19  本文已影响0人  Scincyc

一、CSS3属性选择器

  1. 什么是 CSS3

    • CSS2 的基础上拓展、新增的样式
  2. CSS3 发展现状

    • 移动端支持优于 PC
    • CSS3 目前还草案,在不断改进中
    • CSS3 相对 H5,应用非常广泛
  3. 属性选择器列表

  1. 属性选择器代码演示

    button {
      cursor: pointer;
    }
    button[disabled] {
      cursor: default
    }
    
  2. 代码演示

/*中间没有空格*/
input[type=search] {
  color: skyblue;
}

span[class^=black] {
  color: lightgreen;
}

span[class$=black] {
  color: lightsalmon;
}

span[class*=black] {
  color: lightseagreen;
}

二、结构伪类选择器

  1. 属性列表
  1. 代码演示

    ul li:first-child {
      background-color: lightseagreen;
    }
    
    ul li:last-child {
      background-color: lightcoral;
    }
    
    ul li:nth-child(3) {
      background-color: aqua;
    }
    
(一)nth-child参数详解
  1. nth-child 详解

    • 注意:本质上就是选中第几个子元素

    • n 可以是数字、关键字、公式

    • n 如果是数字,就是选中第几个

    • 常见的关键字even 偶数、odd 奇数

    • 常见的公式如下(如果 n 是公式,则从 0 开始计算)

    • 但是第 0 个元素或者超出了元素的个数会被忽略

  1. 代码演示

    <style>
      /* 偶数 */
      ul li:nth-child(even) {
        background-color: aquamarine;
      }
    
      /* 奇数 */
      ul li:nth-child(odd) {
        background-color: blueviolet;
      }
    
      /*n 是公式,从 0 开始计算 */
      ul li:nth-child(n) {
        background-color: lightcoral;
      }
    
      /* 偶数 */
      ul li:nth-child(2n) {
        background-color: lightskyblue;
      }
    
      /* 奇数 */
      ul li:nth-child(2n + 1) {
        background-color: lightsalmon;
      }
    
      /* 选择第 0 5 10 15, 应该怎么选 */
      ul li:nth-child(5n) {
        background-color: orangered;
      }
    
      /* n + 5 就是从第5个开始往后选择 */
      ul li:nth-child(n + 5) {
        background-color: peru;
      }
    
      /* -n + 5 前五个 */
      ul li:nth-child(-n + 5) {
        background-color: tan;
      }
    </style>
    
(二)nth-child和 nt-of-type 的区别
  1. 代码演示

    <style>
      div :nth-child(1) {
        background-color: lightblue;
      }
    
    /*div的孩子中的第2个 且该孩子是span类型*/
      div span:nth-child(2) {
        background-color: lightpink;
      }
    
    /*div的span孩子中的第2个*/
      div span:nth-of-type(2) {
        background-color: lightseagreen;
      }
    
      div span:nth-of-type(3) {
        background-color: #fff;
      }
    </style>
    
  1. 区别

    • nth-child 选择父元素里面的第几个子元素,不管是第几个类型
    • nt-of-type 选择指定类型的元素

三、伪元素选择器

  1. 伪元素选择器

👌在盒子内容 的前/后 插入行内元素

  1. 注意事项

    • beforeafter 必须有 content 属性
    • before 在内容前面,after 在内容后面
    • beforeafter 创建的是一个元素,但是属于行内元素
    • 创建出来的元素在 Dom 中查找不到,所以称为伪元素
    • 伪元素和标签选择器一样,权重为 1
  2. 代码演示

    <style>
        div {
          width: 100px;
          height: 100px;
          border: 1px solid lightcoral;
        }
    
        div::after,
        div::before {
          display: inline-block;
          width: 20px;
          height: 50px;
          text-align: center;
        }
        div::after {
          content: '德';
          background-color: lightskyblue;
        }
    
        div::before {
          content: '道';
          background-color: mediumaquamarine;
        }
      </style>
    
  1. 添加字体图标

    p {
       width: 220px;
       height: 22px;
       border: 1px solid lightseagreen;
       margin: 60px;
       position: relative;
    }
    p::after {
      content: '\ea50';
      font-family: 'icomoon';
      position: absolute;
      top: -1px;
      right: 10px;
    }
    

四、2D 转换

(一)translate移动

使得盒子移动的方法:定位、外边距、2d转换移动

  1. 2D 转换

    • 2D 转换是改变标签在二维平面上的位置和形状

    • 移动: translate

    • 旋转: rotate

    • 缩放: scale

  2. translate 语法

    • x 就是 x 轴上水平移动
    • y 就是 y 轴上水平移动
    transform: translate(x, y)
    transform: translateX(n)
    transfrom: translateY(n)
    
  3. 重点知识点

    • 2D 的移动主要是指 水平、垂直方向上的移动
    • translate 最大的优点就是不影响其他元素的位置
    • translate 中的100%单位,是相对于本身的宽度和高度来进行计算的
    • 行内标签没有效果
  4. 代码演示

div {
  background-color: lightseagreen;
  width: 200px;
  height: 100px;
  /* 平移 */
  /* 水平垂直移动 100px */
  /* transform: translate(100px, 100px); */

  /* 水平移动 100px */
  /* transform: translate(100px, 0) */

  /* 垂直移动 100px */
  /* transform: translate(0, 100px) */

  /* 水平移动 100px */
  /* transform: translateX(100px); */

  /* 垂直移动 100px */
  transform: translateY(100px)
      
  /* 如果单位不是px,是% 移动的距离是相对盒子自身的宽度/高度对比的 */
  transform: translate(-50%, -50%)
      /*盒子往上走和往左走自己高度和宽度的一半*/
}
(二)rotate旋转
  1. rotate 旋转

    • 2D 旋转指的是让元素在二维平面内顺时针或者逆时针旋转
  2. rotate 语法

    /* 单位是:deg */
    transform: rotate(度数) 
    
  3. 重点知识点

    • rotate 里面跟度数,单位是 deg
    • 角度为时,顺时针,角度为负时,逆时针
    • 默认旋转的中心点是元素的中心点
  4. 代码演示

    img:hover {
      transform: rotate(360deg)
    }
    img {
        transition: all 0.3s;
        /* 过渡加到本身上,谁做动画给谁加 */
    }
    
1)三角案例
div {
    position: relative;
    width: 240px;
    height: 35px;
    border: 1px solid #000;
}
div::after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    width: 10px;
    height: 10px;
    border-right: 1px solid #000;
    border-bottom: 1px solid #000;
    transform: rotate(45deg);
    transition: all 0.2s;/*谁做动画hover给谁加*/
}
/*鼠标经过div 里面的三角旋转 */
div:hover::after {
    transform: rotate(225deg);
}
2)设置旋转中心点(transform-origin)
  1. transform-origin 基础语法

    transform-origin: x y;
    
  2. 重要知识点

    • 注意后面的参数 x 和 y 用空格隔开
    • x y 默认旋转的中心点是元素的中心 (50% 50%),等价于 center center
    • 还可以给 x y 设置像素或者方位名词(topbottomleftrightcenter)

    案例

div {
            width: 200px;
            height: 200px;
            background-color: pink;
            margin: 100px auto;
            transition: all 1s;
            /* 1.可以跟方位名词 */
            /* transform-origin: left bottom; */
            /* 2. 默认的是 50%  50%  等价于 center  center */
            /* 3. 可以是px 像素 */
            transform-origin: 50px 50px;
        }
        
        div:hover {
            transform: rotate(360deg);
        }
3)旋转中心案例
div {
            overflow: hidden;
            width: 200px;
            height: 200px;
            border: 1px solid pink;
            margin: 10px;
            float: left;
        }
        
        div::before {
            content: "黑马";
            display: block;
            width: 100%;
            height: 100%;
            background-color: hotpink;
            transform: rotate(180deg);
            transform-origin: left bottom;
            transition: all 0.4s;
        }
        /* 鼠标经过div 里面的before 复原 */
        
        div:hover::before {
            transform: rotate(0deg);
        }
(三)scale缩放
  1. scale 的作用

    • 用来控制元素的放大与缩小
  2. 语法

    transform: scale(x, y)
    
  3. 知识要点

    • 注意,x 与 y 之间使用逗号进行分隔
    • transform: scale(1, 1): 宽高都放大一倍,相当于没有放大
    • transform: scale(2, 2): 宽和高都放大了二倍
    • transform: scale(2): 只写了一个参数,等比例缩放(x=y=2)
    • transform:scale(0.5, 0.5): 缩小
    • scale 最大的优势:可以设置缩放中心点,默认以中心点缩放;而且不影响其他盒子(width height缩放以上边框缩放 影响后面盒子)
  4. 代码演示

       div:hover {
        /* 注意,数字是倍数的含义,所以不需要加单位 */
        /* transform: scale(2, 2) */
       
        /* 实现等比缩放,同时修改宽与高 */
        /* transform: scale(2) */
       
        /* 小于 1 就等于缩放*/
        transform: scale(0.5, 0.5)
       }
    div {
        /* 设置缩放中心点 */
        transform-origin: left bottom;
    }
    
(1)图片放大案例
div {
            overflow: hidden;
            float: left;
            margin: 10px;
        }
        
        div img {
            transition: all .4s;
        }
        
        div img:hover {
            transform: scale(1.1);
        }
(2)分页按钮案例
 li {
            float: left;
            width: 30px;
            height: 30px;
            border: 1px solid pink;
            margin: 10px;
            text-align: center;
            line-height: 30px;
            list-style: none;
            border-radius: 50%;
            cursor: pointer;
            transition: all .4s;
        }
        
        li:hover {
            transform: scale(1.2);
        }
(3)2d转换综合案例
div {
            width: 200px;
            height: 200px;
            background-color: pink;
            transition: all .5s;
        }
        
        div:hover {
            /* transform: rotate(180deg) translate(150px, 50px); */
            /* 我们同时有位移和其他属性,我们需要把位移放到最前面 */
            transform: translate(150px, 50px) rotate(180deg) scale(1.2);
        }

五、动画

(一)基本使用

1、动画的基本使用

2、语法格式(定义动画)

@keyframes 动画名称 {
    0% {
        width: 100px;
    }
    100% {
        width: 200px
    }
}

语法格式(使用动画)

div {
    /* 调用动画 */
    animation-name: 动画名称;
    /* 持续时间 */
    animation-duration: 持续时间;
}

3、动画序列

4、演示

<style>
    div {
      width: 100px;
      height: 100px;
      background-color: aquamarine;
      animation-name: move;
      animation-duration: 0.5s;
    }

    @keyframes move{
      0% {
        transform: translate(0px)
      }
      100% {
        transform: translate(500px, 0)
      }
    }
  </style>

案例2

 div {
            width: 100px;
            height: 100px;
            background-color: #666;
            animation-name: move;
            animation-duration: 5s;
        }
        /* multiple state : */
        @keyframes move{
            /* int and time */
            0% {
                transform: translate(0, 0);
            }
            25% {
                transform: translate(1000px, 0);
            }
            50% {
                transform: translate(1000px, 500px);
            }
            75% {
                transform: translate(0, 500px);
            }
            100% {
                transform: translate(0, 0);
            }
        }
(二)常见属性
1、animation-duration(持续时间)

2、animation-name(动画名)

3、animation-timing-function(时间曲线)
ease:(默认)值,动画先慢后快,在即将结束时再变慢
ease-out:动画以高速开始,以低速结束
ease-in:动画以低速开始,以高速结束
linear:动画从头到尾的速度是相同的
ease-in-out:动画由慢到快再到慢直至结束
steps():指定时间函数中的间隔数量(步长)

ease不同的是它均等得分为三份,而ease是只在结束时变慢;

贝赛尔函数:规定动画的播放时间曲线

4、animation-delay(动画延迟时间)
默认是0

5、animation-iteration-count(动画播放次数)
默认是1,即一次。值infinite是无限循环

6、animation-direction(动画反向播放)
normal:(默认)值,不进行反方向播放;
reverse:全部播放都是用反方向播放;
alternate:在奇数次数时正向播放,偶数反向播放;
alternate-reverse:在偶数次数正向播放,奇数反向播放;

7、animation-fill-mode(动画保持开始或者结束时的样式)
forwards:动画结束后保持结束时的样式;
backwards:动画结束后 回到开始时的状态

8、animation-play-state(暂停动画)
running:动画运行;(默认)
paused:动画暂停;

代码演示

div {
  width: 100px;
  height: 100px;
  background-color: aquamarine;
  /* 动画名称 */
  animation-name: move;
  /* 动画花费时长 */
  animation-duration: 2s;
  /* 动画速度曲线 */
  animation-timing-function: ease-in-out;
  /* 动画等待多长时间执行 */
  animation-delay: 2s;
  /* 规定动画播放次数 infinite: 无限循环 */
  animation-iteration-count: infinite;
  /* 是否逆行播放 */
  animation-direction: alternate;
  /* 动画结束之后的状态 */
  animation-fill-mode: forwards;
}

div:hover {
  /* 规定动画是否暂停或者播放 */
  animation-play-state: paused;
}
(三)动画简写方式
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
前2个属性一定要写

知识要点

演示

animation: move 2s linear 1s infinite alternate forwards;
(四)热点案例
<body>
    <div class="map">
        <!--北京-->
        <div class="city">
            <div class="dotted"></div>
            <div class="pulse1"></div>
            <div class="pulse2"></div>
            <div class="pulse3"></div>
        </div>
        <div class="city taibei">
            <div class="dotted"></div>
            <div class="pulse1"></div>
            <div class="pulse2"></div>
            <div class="pulse3"></div>
        </div>
    </div>
</body>
 body {
            background-color: black;
        }
        .map {
            position: relative;
            width: 747px;
            height: 616px;
            background: url(media/map.png) no-repeat;
            margin: 0 auto;
        }
        .city {
            position: absolute;
            top: 227px;
            right: 193px;

        }
        .dotted {
            width: 8px;
            height: 8px;
            background-color: #09f;
            border-radius: 50%;
        }
        .city div[class^="pulse"] {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 8px;
            height: 8px;
            box-shadow: 0 0 12px #009df9;
            border-radius: 50%;
            animation: pulse 1.2s linear infinite;
        }
        .city div.pulse2 {
            animation-delay: 0.4s;
        }
        .city div.pulse3 {
            animation-delay: 0.8s;
        }

        @keyframes pulse {
            0% {

            }
            70% {
                /* transform: scale(5); */
                width: 40px;
                height: 40px;
                opacity: 1;
            }
            100% {
                /* transform: scale(7); */
                width: 70px;
                height: 70px;
                opacity: 0;
            }
        }
        .taibei {
            top: 500px;
            right: 80px;
        }
(五)animation-timing-function:steps()
div {
            overflow: hidden;
            font-size: 20px;
            width: 0;
            height: 30px;
            background-color: pink;
            /* 让我们的文字强制一行内显示 */
            white-space: nowrap;
            /* steps 就是分几步来完成我们的动画 有了steps 就不要在写 ease 或者linear 了 */
            animation: w 4s steps(10) forwards;
        }
        
        @keyframes w {
            0% {
                width: 0;
            }
            100% {
                width: 200px;
            }
        }
    <div>世纪佳缘我在这里等你</div>

案例:奔跑的小熊(百度浏览器)

  1. 代码演示
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            background-color: #ccc;
        }      
        div {
            position: absolute;
            width: 200px;
            height: 100px;
            background: url(media/bear.png) no-repeat;
            /* 我们元素可以添加多个动画, 用逗号分隔 */
            animation: bear .4s steps(8) infinite, move 3s forwards;
        }
        
        @keyframes bear {
            0% {
                background-position: 0 0;
            }
            100% {
                background-position: -1600px 0;
            }
        }
        
        @keyframes move {
            0% {
                left: 0;
            }
            100% {
                left: 50%;
                /* margin-left: -100px; */
                transform: translateX(-50%);
            }
        }
    </style>
</head>

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

</html>

六、3D转换

三维坐标系

(一)3D移动
  1. 语法

     transform: translate3d(x, y, z)
    
  2. get start

    transform: translate3d(100px, 100px, 100px)
    /* 注意:x, y, z 对应的值不能省略,不需要填写用 0 进行填充 */
    transform: translate3d(100px, 100px, 0)
    
(二)透视 perspective

1、知识要点

2、get start

body {
  perspective: 1000px;
}

3、translateZperspecitve 的区别

perspecitve 给父级进行设置,translateZ 给 子元素进行设置不同的大小

(三)3D 旋转

让元素在三维平面内沿着 x 轴、y 轴、z 轴 或者自定义轴进行旋转

1、语法

2、rotateX

(1)get start

div {
  perspective: 300px;
}

img {
  display: block;
  margin: 100px auto;
  transition: all 1s;
}

img:hover {
  transform: rotateX(-45deg)
}

(2)左手准则

3、rotateY

(1)get start

div {
  perspective: 500px;
}

img {
  display: block;
  margin: 100px auto;
  transition: all 1s;
}

img:hover {
  transform: rotateY(180deg)
}

(2)左手准则

4、rotateZ

(1)get start

div {
  perspective: 500px;
}

img {
  display: block;
  margin: 100px auto;
  transition: all 1s;
}

img:hover {
  transform: rotateZ(180deg)
}

5、rotate3d

(1)语法

(2)get start

div {
  perspective: 500px;
}

img {
  display: block;
  margin: 100px auto;
  transition: all 1s;
}

img:hover {
  transform: rotate3d(1, 1, 0, 180deg)
}
(四)3D 呈现 transform-style
  1. transform-style

    • 控制子元素是否开启三维立体环境

      transform-style: flat 代表子元素不开启 3D 立体空间,默认的

      transform-style: preserve-3d 子元素开启立体空间

    • 代码写给父级,但是影响的是子盒子

  2. get start

body {
            perspective: 500px;
        }
        
        .box {
            position: relative;
            width: 200px;
            height: 200px;
            margin: 100px auto;
            transition: all 2s;
            /* 让子元素保持3d立体空间环境 */
            transform-style: preserve-3d;
        }
        
        .box:hover {
            transform: rotateY(60deg);
        }
        
        .box div {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: pink;
        }
        
        .box div:last-child {
            background-color: purple;
            transform: rotateX(60deg);
        }
<body>
    <div class="box">
        <div></div>
        <div></div>
    </div>
</body>

七、浏览器私有前缀

div
{    
    -ms-transform: rotate(30deg);        
    -webkit-transform: rotate(30deg);    
    -o-transform: rotate(30deg);        
    -moz-transform: rotate(30deg);    
    transform: rotate(30deg);
}
上一篇下一篇

猜你喜欢

热点阅读