程序员的知识教程库

网站首页 > 教程分享 正文

Web开发学习笔记(34)——CSS3(8)新增特效7

henian88 2024-08-13 06:23:08 教程分享 28 ℃ 0 评论

(1)CSS3颜色的使用

介绍颜色定义方式,包含 RGB 模式、RGBA 模式、HSL 模式和 HSLA 模式。

知识点

  • RGB
  • RGBA
  • HSL
  • HSLA

RGB 指的是三原色,也就是说,R 是 Red 的简写,G 是 Green 的简写,B 是 Blue 的简写,其三者的颜色取值均为 0~255,当我们给这三种颜色的设定不同的值时,三者颜色混合在一起就会调配成其他颜色了。

RGBA 色彩模式是 RGB 色彩模式的扩展,在 Red(红)、Green(绿)、Blue(蓝)三原色通道的基础上,增加了 alpha(明度)参数,这样设置让颜色的设置变得更加合理和便捷。

其语法格式为:

rgba(r,g,b,<opacity>)

其中,rgb 的颜色取值范围为 0~255opacity 的取值范围为 0~1。若是输入值超过取值范围,浏览器会将数值调整到最近的可取值。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li{
            width:100px;
            list-style:none;
        }
        ul{
            float:left;
            margin-left: 40px;
        }
        li.opacity1{
            opacity: 1;
            background:red;
        }
        li.opacity2{
            opacity: 0.8;
            background:red;
        }
        li.opacity3{
            opacity: 0.6;
            background:red;
        }
        li.opacity4{
            opacity: 0.4;
            background:red;
        }
        li.opacity5{
            opacity: 0.2;
            background:red;
        }
        li.opacity6{
            opacity: 0;
            background:red;
        }
        li.rgba1{
            background: rgba(255, 0, 0, 1);
        }
        li.rgba2{
            background: rgba(255, 0, 0, 0.8);
        }
        li.rgba3{
            background: rgba(255, 0, 0, 0.6);
        }
        li.rgba4{
            background: rgba(255, 0, 0, 0.4);
        }
        li.rgba5{
            background: rgba(255, 0, 0, 0.2);
        }
        li.rgba6{
            background: rgba(255, 0, 0, 0);
        }
    </style>
</head>
<body>
    <ul>
        <li>opacity 效果</li>
        <li class="opacity1">100%</li>
        <li class="opacity2">80%</li>
        <li class="opacity3">60%</li>
        <li class="opacity4">40%</li>
        <li class="opacity5">20%</li>
        <li class="opacity6">0</li>
    </ul>
    <ul>
        <li>rgba 效果</li>
        <li class="rgba1">1</li>
        <li class="rgba2">0.8</li>
        <li class="rgba3">0.6</li>
        <li class="rgba4">0.4</li>
        <li class="rgba5">0.2</li>
        <li class="rgba6">0</li>
    </ul>
</body>
</html>

(2)HSL 标准几乎包含了人类所能感知的所有颜色,显示器上能呈现的颜色也都在这个范围内。

其语法格式为:

hsl(hue,staturation,lightness)

H(hue) 色相

CSS3 的色相使用一个圆环来表示,取值范围 0~360。其中 0360 表示红色,120 表示绿色,240 表示蓝色,其他的颜色依次类推。CSS3 色相环如下图所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width:100px;
            height:100px;
            float:left;
            font-size:12px;
        }
        #div1{
            background-color: hsl(60,0%,50%);
        }
        #div2{
            background-color: hsl(60,25%,50%);
        }
        #div3{
            background-color: hsl(60,50%,50%);
        }
        #div4{
            background-color: hsl(60,75%,50%);
        }
        #div5{
            background-color: hsl(60,100%,50%);
        }
    </style>
</head>
<body>
    <div id="div1">hsl(60,0%,50%)</div>
    <div id="div2">hsl(60,25%,50%)</div>
    <div id="div3">hsl(60,50%,50%)</div>
    <div id="div4">hsl(60,75%,50%)</div>
    <div id="div5">hsl(60,100%,50%)</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        float: left;
        font-size: 12px;
        color: darkgray;
      }
      #div1 {
        background-color: hsl(60, 100%, 0%);
      }
      #div2 {
        background-color: hsl(60, 100%, 25%);
      }
      #div3 {
        background-color: hsl(60, 100%, 50%);
      }
      #div4 {
        background-color: hsl(60, 100%, 75%);
      }
      #div5 {
        background-color: hsl(60, 100%, 100%);
      }
    </style>
  </head>
  <body>
    <div id="div1">hsl(60,100%,0%)</div>
    <div id="div2">hsl(60,100%,25%)</div>
    <div id="div3">hsl(60,100%,50%)</div>
    <div id="div4">hsl(60,100%,75%)</div>
    <div id="div5">hsl(60,100%,100%)</div>
  </body>
</html>

S(staturation) 饱和度

饱和度表示颜色的鲜艳程度,取值范围为 0%~100%,数值越高饱和度越高,颜色就越鲜艳。完全不饱和(0%)的颜色是没有色相的。

L(lightness)亮度

亮度用来控制色彩的明暗变化,取值范围为 0%~100%。数值越小色彩越暗越接近于黑色,数值越大色彩越亮越接近于白色。

(3)HSLA 是由色相(hue)、饱和度(saturation)、亮度(lightness)、明度(alpha)组成。

其语法格式为:

hsla(hue, saturation, lightness, alpha)

其中,参数 alpha 的取值在 0.0~1.0 这个区间。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 300px;
        height: 50px;
        text-align: center;
      }
      #item1 {
        background-color: hsla(2, 40%, 60%, 1);
      }
      #item2 {
        background-color: hsla(2, 40%, 60%, 0.7);
      }
      #item3 {
        background-color: hsla(2, 40%, 60%, 0.4);
      }
      #item4 {
        background-color: hsla(2, 40%, 60%, 0.2);
      }
    </style>
  </head>
  <body>
    <div id="item1">hsla(2, 40%, 60%, 1)</div>
    <div id="item2">hsla(2, 40%, 60%, 0.7)</div>
    <div id="item3">hsla(2, 40%, 60%, 0.4)</div>
    <div id="item4">hsla(2, 40%, 60%, 0.2)</div>
  </body>
</html>

(4)线性渐变函数和重复性线性渐变函数。

知识点

  • linear-gradient 函数
  • repeating-linear-gradient 函数

线性渐变是向下、向上、向左、向右、对角方向的颜色渐变。

其语法格式为:

background-image: linear-gradient(side-or-corner|angle, linear-color-stop);

参数说明如下:

  • side-or-corner 是描述渐变线的起始位置,它包含两个关键词:第一个指出水平位置 left or right,第二个指出垂直位置 top or bottom。
  • angle 是用角度值来指定渐变的方向。
  • linear-color-stop 是设置渐变的颜色值。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #linear{
            display: flex;
        }
        .gradient1{
            width:100px;
            height:100px;
            background-image:linear-gradient(#ff577f, #c6c9ff);
        }
        .gradient2{
            margin-left:10px;
            width:100px;
            height:100px;
            background-image: linear-gradient(to right, #ff9d72, #c6c9ff); 
        }
        .gradient3{
            margin-left: 10px;
            width:100px;
            height:100px;
            background-image:linear-gradient(to bottom right, #8ad7c1, #c6c9ff);
        }
        .gradient4{
            margin-left: 10px;
            width:100px;
            height:100px;
            background-image: linear-gradient(50deg,  #bc6ff1, #ffd5cd);
        }
    </style>
</head>
<body>
    <div id="linear">
        <div class="gradient1"></div>
        <div class="gradient2"></div>
        <div class="gradient3"></div>
        <div class="gradient4"></div>
    </div>
</body>
</html>

(5)重复性线性渐变是用重复的线性渐变组成的 <image>,它与线性渐变的区别在于,它会在所有方向上重复渐变来覆盖整个元素。

其语法格式为:

background-image: repeating-linear-gradient(side-or-corner|angle, color-stop);

参数说明如下:

  • side-or-corner 是描述渐变线的起始位置,它包含 to 和两个关键词:第一个指出水平位置 left or right,第二个指出垂直位置 top or bottom。
  • angle 是用角度值来指定渐变的方向。
  • color-stop 是由一个 <color> 组成,并且跟随一个可选的终点位置。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width:200px;
            height:200px;
            display:inline-block;
            margin-left:20px;
            margin-top:20px;
        }
        .item1{
            background-image:repeating-linear-gradient(
                15deg, /*上偏右的角度,北偏东*/
                #8843f8 0%,
                #ef2f88 5%,
                #f47340 10%,
                #f9d371 15%
            );
        }
        .item2{
            background-image:repeating-linear-gradient(
                to left top,
                #8843f8 0%,
                #ef2f88 5%,
                #f47340 10%,
                #f9d371 15%
            );
        }
    </style>
</head>
<body>
    <div class="item1"></div>
    <div class="item2"></div>
</body>
</html>
  • linear-gradient 函数没有内在尺寸,所有的颜色值会根据元素的大小与之匹配来渐变渲染整个元素。
  • repeating-linear-gradient 函数有内在尺寸,会从第一个颜色值开始重复性的渐变渲染元素,直到元素被填满。

(6)设置径向渐变的背景颜色。

知识点

  • radial-gradient 函数
  • repeating-radial-gradient 函数

径向渐变是由元素中间定义的渐变。

其语法格式为:

background-image: radial-gradient(shape, color-stop);

参数说明如下:

  • shape 设置渐变的形状,其取值有 circle(圆形) 和 ellipse(椭圆)。
  • color-stop 设置某个确定位置的颜色值。
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #radial {
        display: flex;
      }
      /*均匀分布径向渐变*/
      .gradient1 {
        width: 100px;
        height: 100px;
        background-image: radial-gradient(#ff577f, #c6c9ff, #8ad7c1);
      }
      /*不均匀渐变*/
      .gradient2 {
        margin-left: 10px;
        width: 100px;
        height: 100px;
        background-image: radial-gradient(#8bcdcd 5%, #ff9d72, #c6c9ff);
      }
      /*椭圆形渐变*/
      .gradient3 {
        margin-left: 10px;
        width: 100px;
        height: 100px;
        background-image: radial-gradient(ellipse, #8ad7c1, #c6c9ff, #fce2ce);
      }
      /*圆形渐变*/
      .gradient4 {
        margin-left: 10px;
        width: 100px;
        height: 100px;
        background-image: radial-gradient(circle, #bc6ff1, #ffd5cd, #b6eb7a);
      }
    </style>
  </head>
  <body>
    <div id="radial">
      <div class="gradient1"></div>
      <div class="gradient2"></div>
      <div class="gradient3"></div>
      <div class="gradient4"></div>
    </div>
  </body>
</html>

(7)重复性径向渐变是用重复性的径向渐变组成的图像。它与径向渐变的区别在于,它会从原点开始重复径向渐变来覆盖整个元素。

其语法格式为:

background: repeating-radial-gradient(extent-keyword, color-stop);

参数说明如下:

  • extent-keyword 是描述边缘轮廓的具体位置,关键字常量如下所示:
  • color-stop 是某个确定位置的固定颜色值。
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 200px;
        height: 200px;
        display: inline-block;
        margin-left: 20px;
        margin-top: 20px;
      }
      .gradient1 {
        background: repeating-radial-gradient(
          closest-corner,
          #8843f8 0%,
          #ef2f88 5%,
          #f47340 10%,
          #f9d371 15%
        );
      }
      .gradient2 {
        background: repeating-radial-gradient(
          farthest-side,
          #8843f8,
          #ef2f88,
          #f47340,
          #f9d371
        );
      }
    </style>
  </head>
  <body>
    <div class="gradient1"></div>
    <div class="gradient2"></div>
  </body>
</html>

两种径向渐变函数,两者之间的区别总结如下:

  • radial-gradient 函数没有固定的尺寸,其大小根据适应于元素的大小。
  • repeating-radial-gradient 函数有固定的尺寸,通过重复渐变来填充元素。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表