经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
WEB之CSS3系列笔记
来源:cnblogs  作者:Hac_Zhang  时间:2020/11/9 16:02:04  对本文有异议

WEB之CSS3系列笔记

CSS3属性选择器

属性选择器列表

选择符 简介
E[att] 选择具有att属性的E元素
E[att="val"] 选择具有att属性且属性值为val的E元素
E[att^="val"] 匹配具有att属性, 且值以val开头的E元素
E[att$="val"] 匹配具有att属性, 且值以val借位的E元素
E[att*="val"] 匹配具有att属性, 且值中含有val的E元素
  • 例如:

    1. button {
    2. cursor: pointer;
    3. }
    4. button[disabled] {
    5. cursor: default
    6. }
  • 例如

    1. input[type=search] {
    2. color: skyblue;
    3. }
    4. span[class^=black] {
    5. color: lightgreen;
    6. }
    7. span[class$=black] {
    8. color: lightsalmon;
    9. }
    10. span[class*=black] {
    11. color: lightseagreen;
    12. }

结构伪类选择器

属性列表

选择符 简介
E:first-child 匹配父元素中的第一个子元素E
E:last-child 匹配父元素中最后一个E元素
E:nth-child(n) 匹配父元素中的第n个子元素E
E:first-of-type 指定类型E的第一个
E:last-of-type 指定类型E的最后一个
E:nth-of-type(n) 指定类型E的第n个
  • 例如

    1. ul li:first-child {
    2. background-color: lightseagreen;
    3. }
    4. ul li:last-child {
    5. background-color: lightcoral;
    6. }
    7. ul li:nth-child(3) {
    8. background-color: aqua;
    9. }
    10. /************************************************/
    11. <style>
    12. /* 偶数 */
    13. ul li:nth-child(even) {
    14. background-color: aquamarine;
    15. }
    16. /* 奇数 */
    17. ul li:nth-child(odd) {
    18. background-color: blueviolet;
    19. }
    20. /*n 是公式,从 0 开始计算 */
    21. ul li:nth-child(n) {
    22. background-color: lightcoral;
    23. }
    24. /* 偶数 */
    25. ul li:nth-child(2n) {
    26. background-color: lightskyblue;
    27. }
    28. /* 奇数 */
    29. ul li:nth-child(2n + 1) {
    30. background-color: lightsalmon;
    31. }
    32. /* 选择第 0 5 10 15, 应该怎么选 */
    33. ul li:nth-child(5n) {
    34. background-color: orangered;
    35. }
    36. /* n + 5 就是从第5个开始往后选择 */
    37. ul li:nth-child(n + 5) {
    38. background-color: peru;
    39. }
    40. /* -n + 5 前五个 */
    41. ul li:nth-child(-n + 5) {
    42. background-color: tan;
    43. }
    44. </style>

伪元素选择器

选择符 简介
::before 在元素内部的前面插入内容
::after 在元素内部的后面插入内容
  • 伪类选择器注意事项
    • beforeafter 必须有 content 属性
    • before 在内容前面,after 在内容后面
    • beforeafter 创建的是一个元素,但是属于行内元素
    • 创建出来的元素在 Dom 中查找不到,所以称为伪元素
    • 伪元素和标签选择器一样,权重为 1

2D转换之translate

2D 转换

  • 2D 转换是改变标签在二维平面上的位置和形状
  • 移动: translate
  • 旋转: rotate
  • 缩放: scale

translate 语法

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

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

    1. div {
    2. background-color: lightseagreen;
    3. width: 200px;
    4. height: 100px;
    5. /* 平移 */
    6. /* 水平垂直移动 100px */
    7. /* transform: translate(100px, 100px); */
    8. /* 水平移动 100px */
    9. /* transform: translate(100px, 0) */
    10. /* 垂直移动 100px */
    11. /* transform: translate(0, 100px) */
    12. /* 水平移动 100px */
    13. /* transform: translateX(100px); */
    14. /* 垂直移动 100px */
    15. transform: translateY(100px)
    16. }

2D 转换 rotate

  1. rotate 旋转

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

    1. /* 单位是:deg */
    2. transform: rotate(度数)
    3. /**/
    4. div{
    5. transform: rotate(0deg);
    6. }
  3. 重点知识点

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

    1. img:hover {
    2. transform: rotate(360deg)
    3. }

设置元素旋转中心点(transform-origin)

  1. transform-origin 基础语法

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

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

2D 转换之 scale

  1. scale 的作用

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

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

    • 注意,x 与 y 之间使用逗号进行分隔
    • transform: scale(1, 1): 宽高都放大一倍,相当于没有放大
    • transform: scale(2, 2): 宽和高都放大了二倍
    • transform: scale(2): 如果只写了一个参数,第二个参数就和第一个参数一致
    • transform:scale(0.5, 0.5): 缩小
    • scale 最大的优势:可以设置转换中心点缩放,默认以中心点缩放,而且不影响其他盒子
  4. 代码演示

  1. div:hover {
  2. /* 注意,数字是倍数的含义,所以不需要加单位 */
  3. /* transform: scale(2, 2) */
  4. /* 实现等比缩放,同时修改宽与高 */
  5. /* transform: scale(2) */
  6. /* 小于 1 就等于缩放*/
  7. transform: scale(0.5, 0.5)
  8. }

动画(animation)

  1. 什么是动画

    • 动画是 CSS3 中最具颠覆性的特征之一,可通过设置多个节点来精确的控制一个或者一组动画,从而实现复杂的动画效果
  2. 动画的基本使用

    • 先定义动画
    • 在调用定义好的动画
  3. 语法格式(定义动画)

    1. @keyframes 动画名称 {
    2. 0% {
    3. width: 100px;
    4. }
    5. 100% {
    6. width: 200px
    7. }
    8. }
  4. 语法格式(使用动画)

    1. div {
    2. /* 调用动画 */
    3. animation-name: 动画名称;
    4. /* 持续时间 */
    5. animation-duration: 持续时间;
    6. }
  5. 动画序列

    • 0% 是动画的开始,100 % 是动画的完成,这样的规则就是动画序列
    • 在 @keyframs 中规定某项 CSS 样式,就由创建当前样式逐渐改为新样式的动画效果
    • 动画是使元素从一个样式逐渐变化为另一个样式的效果,可以改变任意多的样式任意多的次数
    • 用百分比来规定变化发生的时间,或用 fromto,等同于 0% 和 100%
  6. 代码演示

    1. <style>
    2. div {
    3. width: 100px;
    4. height: 100px;
    5. background-color: aquamarine;
    6. animation-name: move;
    7. animation-duration: 0.5s;
    8. }
    9. @keyframes move{
    10. 0% {
    11. transform: translate(0px)
    12. }
    13. 100% {
    14. transform: translate(500px, 0)
    15. }
    16. }
    17. </style>

动画常见属性

属性 描述
@keyframes 规定动画
animation 所有动画属性的简写属性, 除了animation-play-state属性。
animation-name 规定@keyframes动画的名称
animation-duration 规定动画完成一个周期所花费的秒或毫秒, 默认是0
animation-timing-function 规定动画的速度曲线
animation-delay 规定动画何时开始, 默认是0.
animation-iteration-count 规定动画被播放的次数, 默认是1, 还有infinite
animation-direction 规定动画是否在下一周期逆向播放, 默认是"normal", alternate逆播放
animation-play-state 规定动画是否正在运行或暂停。默认"running"还有paused
animation-fill-mode 规定动画结束后状态, 保持forwards回到起始backwards
  • 例如:

    1. div {
    2. width: 100px;
    3. height: 100px;
    4. background-color: aquamarine;
    5. /* 动画名称 */
    6. animation-name: move;
    7. /* 动画花费时长 */
    8. animation-duration: 2s;
    9. /* 动画速度曲线 */
    10. animation-timing-function: ease-in-out;
    11. /* 动画等待多长时间执行 */
    12. animation-delay: 2s;
    13. /* 规定动画播放次数 infinite: 无限循环 */
    14. animation-iteration-count: infinite;
    15. /* 是否逆行播放 */
    16. animation-direction: alternate;
    17. /* 动画结束之后的状态 */
    18. animation-fill-mode: forwards;
    19. }
    20. div:hover {
    21. /* 规定动画是否暂停或者播放 */
    22. animation-play-state: paused;
    23. }

    动画简介方式

    1. /* animation: 动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 起始与结束状态 */
    2. animation: name duration timing-function delay iteration-count direction fill-mode

    知识要点

    • 简写属性里面不包含 animation-paly-state
    • 暂停动画 animation-paly-state: paused; 经常和鼠标经过等其他配合使用
    • 要想动画走回来,而不是直接调回来:animation-direction: alternate
    • 盒子动画结束后,停在结束位置:animation-fill-mode: forwards

速度曲线细节

animation-timing-function: 规定动画的速度曲线,默认是ease

描述
linear 动画从头到尾的速度是相同的。匀速
ease 默认。动画从低俗开始,然后加快,在结束前变慢。
ease-in 动画以低速开始。
ease-out 动画以低速结束。
ease-in-out 动画以低速开始和结束。
strps() 指定了时间函数中的间隔数量(步长)
  • 例如

    1. div {
    2. width: 0px;
    3. height: 50px;
    4. line-height: 50px;
    5. white-space: nowrap;
    6. overflow: hidden;
    7. background-color: aquamarine;
    8. animation: move 4s steps(24) forwards;
    9. }
    10. @keyframes move {
    11. 0% {
    12. width: 0px;
    13. }
    14. 100% {
    15. width: 480px;
    16. }
    17. }

3D 转换

3D 转换知识要点

  • 3D 位移:translate3d(x, y, z)
  • 3D 旋转:rotate3d(x, y, z)
  • 透视:perspctive
  • 3D呈现 `transfrom-style``

``3D移动translate3d`

  • 3D 移动就是在 2D 移动的基础上多加了一个可以移动的方向,就是 z 轴方向
  • transform: translateX(100px):仅仅是在 x 轴上移动
  • transform: translateY(100px):仅仅是在 y 轴上移动
  • transform: translateZ(100px):仅仅是在 z 轴上移动
  • transform: translate3d(x, y, z):其中x、y、z 分别指要移动的轴的方向的距离
  • 注意:x, y, z 对应的值不能省略,不需要填写用 0 进行填充
  1. transform: translate3d(x, y, z)
  2. /*******************************/
  3. transform: translate3d(100px, 100px, 100px)
  4. /* 注意:x, y, z 对应的值不能省略,不需要填写用 0 进行填充 */
  5. transform: translate3d(100px, 100px, 0)

透视 perspective

知识点讲解

  • 如果想要网页产生 3D 效果需要透视(理解成 3D 物体投影的 2D 平面上)
  • 实际上模仿人类的视觉位置,可视为安排一直眼睛去看
  • 透视也称为视距,所谓的视距就是人的眼睛到屏幕的距离
  • 距离视觉点越近的在电脑平面成像越大,越远成像越小
  • 透视的单位是像素

知识要点

  • 透视需要写在被视察元素的父盒子上面

    1. body {
    2. perspective: 1000px;
    3. }

translateZ

translateZperspecitve 的区别

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

3D 旋转rotateX

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

  • 语法

    • transform: rotateX(45deg) -- 沿着 x 轴正方向旋转 45 度
    • transform: rotateY(45deg) -- 沿着 y 轴正方向旋转 45 度
    • transform: rotateZ(45deg) -- 沿着 z 轴正方向旋转 45 度
    • transform: rotate3d(x, y, z, 45deg) -- 沿着自定义轴旋转 45 deg 为角度
  • 例如

    1. div {
    2. perspective: 300px;
    3. }
    4. img {
    5. display: block;
    6. margin: 100px auto;
    7. transition: all 1s;
    8. }
    9. img:hover {
    10. transform: rotateX(-45deg)
    11. }
    12. /*
    13. - 左手的手拇指指向 x 轴的正方向
    14. - 其余手指的弯曲方向就是该元素沿着 x 轴旋转的方向
    15. */

3D 旋转 rotateY

  1. div {
  2. perspective: 500px;
  3. }
  4. img {
  5. display: block;
  6. margin: 100px auto;
  7. transition: all 1s;
  8. }
  9. img:hover {
  10. transform: rotateY(180deg)
  11. }
  12. /*
  13. - 左手的拇指指向 y 轴的正方向
  14. - 其余的手指弯曲方向就是该元素沿着 y 轴旋转的方向(正值)
  15. */

3D 旋转 rotateZ

  1. 代码演示

    1. div {
    2. perspective: 500px;
    3. }
    4. img {
    5. display: block;
    6. margin: 100px auto;
    7. transition: all 1s;
    8. }
    9. img:hover {
    10. transform: rotateZ(180deg)
    11. }
  2. rotate3d

    • transform: rotate3d(x, y, z, deg) -- 沿着自定义轴旋转 deg 为角度
    • x, y, z 表示旋转轴的矢量,是标识你是否希望沿着该轴进行旋转,最后一个标识旋转的角度
      • transform: rotate3d(1, 1, 0, 180deg) -- 沿着对角线旋转 45deg
      • transform: rotate3d(1, 0, 0, 180deg) -- 沿着 x 轴旋转 45deg

3D 呈现 transform-style

  1. transform-style
    • ☆☆☆☆☆

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

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

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

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

原文链接:http://www.cnblogs.com/Hac-Zhang/p/13756187.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号