欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端實(shí)現(xiàn)文字漸變的三種方式

 更新時(shí)間:2025年07月07日 08:28:26   作者:飛天貓貓頭  
這篇文章主要介紹了前端實(shí)現(xiàn)文字漸變的三種方式:CSS通過(guò)背景漸變與裁切實(shí)現(xiàn),Canvas利用createLinearGradient繪制,SVG通過(guò)定義漸變并應(yīng)用到文字,CSS最常用,其他兩種適用于特定場(chǎng)景,需要的朋友可以參考下

前言

最近開(kāi)發(fā)的時(shí)候發(fā)現(xiàn)很多ui圖上面的標(biāo)題都是帶有漸變效果的,這里就記錄一下前端實(shí)現(xiàn)文字漸變的幾種方式。

完整效果如下

CSS 方式

通過(guò)給文字容器的背景設(shè)置漸變顏色,并使用background-clip屬性,將其以文字內(nèi)容進(jìn)行裁切。最后使用text-fill-color屬性,給文字設(shè)置透明填充來(lái)實(shí)現(xiàn)

屬性名稱效果
backgroundlinear-gradient(to top, #b1495a, #c71a44)給文字容器設(shè)置漸變背景色 
background-cliptext背景被裁切成文字的前景色
text-fill-colortransparent文字的填充顏色

效果如下

  • 具體樣式代碼
.up-gradient {
  background: linear-gradient(to top, #b1495a, #c71a44);
  /* 背景被裁剪成文字的前景色。 */
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
.down-gradient {
  background: linear-gradient(to bottom, #b1495a, #c71a44);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
.left-gradient {
  background: linear-gradient(to left, #b1495a, #c71a44);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
.right-gradient {
  background: linear-gradient(to right, #b1495a, #c71a44);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
/* 多顏色漸變 */
.multi-gradient {
  background: linear-gradient(90deg, #b1495a 10%, #c71a44 50%, #ffb86c 80%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}

html 結(jié)構(gòu)

<body>
  <div class="container">
    <h1>CSS實(shí)現(xiàn)文字漸變</h1>
    <!-- css版本 -->
    <article class="panel">
      <div class="panel-box-title">CSS版:</div>
      <div class="box">
        <div class="content-text up-gradient">向上漸變</div>
        <div class="content-text down-gradient">向下漸變</div>
        <div class="content-text left-gradient">向左漸變</div>
        <div class="content-text right-gradient">向右漸變</div>
        <!-- 設(shè)置多個(gè)顏色 -->
        <div class="content-text multi-gradient">多顏色漸變</div>
      </div>
    </article>
  </div>
</body>

Canvas 方式

canvas中的文字漸變的實(shí)現(xiàn)方式就很簡(jiǎn)單了,因?yàn)?code>canvas可以直接給文字設(shè)置漸變樣式。

主要用到createLinearGradient方法,用來(lái)創(chuàng)建一個(gè)線性漸變,addColorStop設(shè)置漸變的色標(biāo),就像是這個(gè)效果

最后再用fillStyle指定使用我們創(chuàng)建的漸變對(duì)象即可

效果如下

核心代碼

<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>文字漸變</title>
    <link rel="stylesheet" href="index.css" rel="external nofollow"  rel="external nofollow"  />
  </head>
  <body>
    <div class="container">
      <!-- canvas版本 -->
      <article class="panel">
        <div class="panel-box-title">Canvas版:</div>
        <div class="box">
          <canvas id="canvas" height="180" width="900"></canvas>
        </div>
      </article>
    </div>
  </body>
  <script>
    const canvas = document.getElementById('canvas')
    const ctx = canvas.getContext('2d')
    ctx.font = '32px Arial'
    // 從左到右的漸變文字
    const leftToRightGradient = ctx.createLinearGradient(0, 0, canvas.width, 0)
    leftToRightGradient.addColorStop(0, '#fff')
    leftToRightGradient.addColorStop(1, '#000')

    ctx.fillStyle = leftToRightGradient
    ctx.fillText('Canvas 從左到右漸變', 20, 40)

    // 從上到下的漸變文字
    const topToBottomGradient = ctx.createLinearGradient(0, 0, 0, canvas.height)
    topToBottomGradient.addColorStop(0, '#fff')
    topToBottomGradient.addColorStop(1, '#000')

    ctx.fillStyle = topToBottomGradient
    ctx.fillText('Canvas 從上到下漸變', 20, 80)

    // 從右到左的漸變文字
    const rightToLeftGradient = ctx.createLinearGradient(canvas.width, 0, 0, 0)
    rightToLeftGradient.addColorStop(0, '#fff')
    rightToLeftGradient.addColorStop(1, '#000')

    ctx.fillStyle = rightToLeftGradient
    ctx.fillText('Canvas 從右到左漸變', 20, 120)

    // 從下到上的漸變文字
    const bottomToTopGradient = ctx.createLinearGradient(0, canvas.height, 0, 0)
    bottomToTopGradient.addColorStop(0, '#fff')
    bottomToTopGradient.addColorStop(1, '#000')

    ctx.fillStyle = bottomToTopGradient
    ctx.fillText('Canvas 從下到上漸變', 20, 160)
  </script>
</html>

SVG 方式

SVG 文字漸變的核心原理是使用 SVG 的<linearGradient>定義漸變,然后通過(guò)fill="url(#gradientId)"將漸變應(yīng)用到文字上。

漸變效果如下

核心代碼如下

    <svg width="900" height="180" xmlns="http://www.w3.org/2000/svg">
            <defs>
              <!-- 從左到右漸變 -->
              <linearGradient
                id="leftToRight"
                x1="0%"
                y1="0%"
                x2="100%"
                y2="0%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>

              <!-- 從上到下漸變 -->
              <linearGradient
                id="topToBottom"
                x1="0%"
                y1="0%"
                x2="0%"
                y2="100%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>

              <!-- 從右到左漸變 -->
              <linearGradient
                id="rightToLeft"
                x1="100%"
                y1="0%"
                x2="0%"
                y2="0%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>

              <!-- 從下到上漸變 -->
              <linearGradient
                id="bottomToTop"
                x1="0%"
                y1="100%"
                x2="0%"
                y2="0%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>
            </defs>

            <!-- 從左到右漸變文字 -->
            <text
              x="20"
              y="40"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#leftToRight)"
            >
              SVG 從左到右漸變
            </text>

            <!-- 從上到下漸變文字 -->
            <text
              x="20"
              y="80"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#topToBottom)"
            >
              SVG 從上到下漸變
            </text>

            <!-- 從右到左漸變文字 -->
            <text
              x="20"
              y="120"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#rightToLeft)"
            >
              SVG 從右到左漸變
            </text>

            <!-- 從下到上漸變文字 -->
            <text
              x="20"
              y="160"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#bottomToTop)"
            >
              SVG 從下到上漸變
            </text>
          </svg>

完整示例代碼

index.css

樣式代碼

html,
body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  background-color: #000;
  color: #fff;
  font-family: 'Segoe UI', 'Arial', sans-serif;
  font-size: 16px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
}

/* 外層容器 */
.container {
  width: 80%;
  max-width: 900px;
  margin: 40px auto;
  padding: 24px;
  background: #181c24;
  border-radius: 18px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.45);
  border: 1.5px solid #232936;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.panel {
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.panel-box-title {
  font-weight: bold;
  color: #ffb86c;
  text-shadow: 0 2px 8px #181c24cc;
}
/* 通用文字樣式 */
.content-text {
  font-size: 32px;
  font-weight: bold;
}

.box {
  background: #191b22;
  border-radius: 14px;
  padding: 24px;
  box-shadow: 0 4px 32px rgba(0, 0, 0, 0.32);
  display: flex;
  flex-direction: column;
  border: 1.5px solid #232936;
  transition: box-shadow 0.2s, border 0.2s;
  position: relative;
  overflow: hidden;
  z-index: 1;
}
.box:hover {
  box-shadow: 0 8px 48px 0 rgba(0, 0, 0, 0.76);
  border: 1.5px solid #3a3f4b;
}

.up-gradient {
  background: linear-gradient(to top, #b1495a, #c71a44);
  /* 背景被裁剪成文字的前景色。 */
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
.down-gradient {
  background: linear-gradient(to bottom, #b1495a, #c71a44);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
.left-gradient {
  background: linear-gradient(to left, #b1495a, #c71a44);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
.right-gradient {
  background: linear-gradient(to right, #b1495a, #c71a44);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}
/* 多顏色漸變 */
.multi-gradient {
  background: linear-gradient(90deg, #b1495a 10%, #c71a44 50%, #ffb86c 80%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  /* 文字透明填充 */
  text-fill-color: transparent;
}

index.html

<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>文字漸變</title>
    <link rel="stylesheet" href="index.css" rel="external nofollow"  rel="external nofollow"  />
  </head>
  <body>
    <div class="container">
      <h1>前端實(shí)現(xiàn)文字漸變的幾種方式</h1>
      <!-- css版本 -->
      <article class="panel">
        <div class="panel-box-title">CSS版:</div>
        <div class="box">
          <div class="content-text up-gradient">向上漸變</div>
          <div class="content-text down-gradient">向下漸變</div>
          <div class="content-text left-gradient">向左漸變</div>
          <div class="content-text right-gradient">向右漸變</div>
          <!-- 設(shè)置多個(gè)顏色 -->
          <div class="content-text multi-gradient">多顏色漸變</div>
        </div>
      </article>
      <!-- canvas版本 -->
      <article class="panel">
        <div class="panel-box-title">Canvas版:</div>
        <div class="box">
          <canvas id="canvas" height="180" width="900"></canvas>
        </div>
      </article>
      <!-- svg版本 -->
      <article class="panel">
        <div class="panel-box-title">SVG版:</div>
        <div class="box">
          <svg width="900" height="180" xmlns="http://www.w3.org/2000/svg">
            <defs>
              <!-- 從左到右漸變 -->
              <linearGradient
                id="leftToRight"
                x1="0%"
                y1="0%"
                x2="100%"
                y2="0%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>

              <!-- 從上到下漸變 -->
              <linearGradient
                id="topToBottom"
                x1="0%"
                y1="0%"
                x2="0%"
                y2="100%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>

              <!-- 從右到左漸變 -->
              <linearGradient
                id="rightToLeft"
                x1="100%"
                y1="0%"
                x2="0%"
                y2="0%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>

              <!-- 從下到上漸變 -->
              <linearGradient
                id="bottomToTop"
                x1="0%"
                y1="100%"
                x2="0%"
                y2="0%"
              >
                <stop
                  offset="0%"
                  style="stop-color: #b1495a; stop-opacity: 1"
                />
                <stop
                  offset="100%"
                  style="stop-color: #c71a44; stop-opacity: 1"
                />
              </linearGradient>
            </defs>

            <!-- 從左到右漸變文字 -->
            <text
              x="20"
              y="40"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#leftToRight)"
            >
              SVG 從左到右漸變
            </text>

            <!-- 從上到下漸變文字 -->
            <text
              x="20"
              y="80"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#topToBottom)"
            >
              SVG 從上到下漸變
            </text>

            <!-- 從右到左漸變文字 -->
            <text
              x="20"
              y="120"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#rightToLeft)"
            >
              SVG 從右到左漸變
            </text>

            <!-- 從下到上漸變文字 -->
            <text
              x="20"
              y="160"
              font-family="Arial"
              font-size="32"
              font-weight="bold"
              fill="url(#bottomToTop)"
            >
              SVG 從下到上漸變
            </text>
          </svg>
        </div>
      </article>
    </div>
  </body>
  <script>
    const canvas = document.getElementById('canvas')
    const ctx = canvas.getContext('2d')
    ctx.font = '32px Arial'
    // 從左到右的漸變文字
    const leftToRightGradient = ctx.createLinearGradient(0, 0, canvas.width, 0)
    leftToRightGradient.addColorStop(0, '#fff')
    leftToRightGradient.addColorStop(1, '#000')

    ctx.fillStyle = leftToRightGradient
    ctx.fillText('Canvas 從左到右漸變', 20, 40)

    // 從上到下的漸變文字
    const topToBottomGradient = ctx.createLinearGradient(0, 0, 0, canvas.height)
    topToBottomGradient.addColorStop(0, '#fff')
    topToBottomGradient.addColorStop(1, '#000')

    ctx.fillStyle = topToBottomGradient
    ctx.fillText('Canvas 從上到下漸變', 20, 80)

    // 從右到左的漸變文字
    const rightToLeftGradient = ctx.createLinearGradient(canvas.width, 0, 0, 0)
    rightToLeftGradient.addColorStop(0, '#fff')
    rightToLeftGradient.addColorStop(1, '#000')

    ctx.fillStyle = rightToLeftGradient
    ctx.fillText('Canvas 從右到左漸變', 20, 120)

    // 從下到上的漸變文字
    const bottomToTopGradient = ctx.createLinearGradient(0, canvas.height, 0, 0)
    bottomToTopGradient.addColorStop(0, '#fff')
    bottomToTopGradient.addColorStop(1, '#000')

    ctx.fillStyle = bottomToTopGradient
    ctx.fillText('Canvas 從下到上漸變', 20, 160)
  </script>
</html>

結(jié)尾

日常開(kāi)發(fā)中還是css版本的比較常用。另外兩種,只有在特定環(huán)境下才有用。

相關(guān)文章

  • JavaScript?中比較字符串的?4?種方法示例詳解

    JavaScript?中比較字符串的?4?種方法示例詳解

    這篇文章主要介紹了在?JavaScript?中比較字符串的?4?種方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-10-10
  • JS跳過(guò)debugger的幾種方法小結(jié)

    JS跳過(guò)debugger的幾種方法小結(jié)

    本文主要介紹了JS跳過(guò)debugger的幾種方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • JavaScript重定向URL參數(shù)的兩種方法小結(jié)

    JavaScript重定向URL參數(shù)的兩種方法小結(jié)

    關(guān)于JavaScript重定向URL參數(shù)的實(shí)現(xiàn)方法網(wǎng)站有很多,這篇文章的主要內(nèi)容是從網(wǎng)上查找,并進(jìn)行了修改,簡(jiǎn)單粗暴的實(shí)現(xiàn)使用JavaScript重置url參數(shù),文中給出了詳細(xì)的示例代碼和調(diào)用代碼,對(duì)大家的理解和學(xué)習(xí)很有幫助,感興趣的朋友們下面來(lái)一起看看吧。
    2016-10-10
  • 基于javascript實(shí)現(xiàn)根據(jù)身份證號(hào)碼識(shí)別性別和年齡

    基于javascript實(shí)現(xiàn)根據(jù)身份證號(hào)碼識(shí)別性別和年齡

    這篇文章主要介紹了基于javascript實(shí)現(xiàn)根據(jù)身份證號(hào)碼識(shí)別性別和年齡的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 動(dòng)態(tài)加載iframe時(shí)get請(qǐng)求傳遞中文參數(shù)亂碼解決方法

    動(dòng)態(tài)加載iframe時(shí)get請(qǐng)求傳遞中文參數(shù)亂碼解決方法

    這篇文章主要介紹了動(dòng)態(tài)加載iframe時(shí)get請(qǐng)求傳遞中文參數(shù)亂碼解決方法,需要的朋友可以參考下
    2014-05-05
  • 自己編寫的類似JS的trim方法

    自己編寫的類似JS的trim方法

    在這里我們可以使用自己編寫的trim方法來(lái)處理一些剪掉字符串兩端的空字符串的需要,下面有個(gè)不錯(cuò)的方法,感興趣的朋友可以參考下
    2013-10-10
  • 原生JavaScript實(shí)現(xiàn)換膚

    原生JavaScript實(shí)現(xiàn)換膚

    這篇文章主要為大家詳細(xì)介紹了原生JavaScript實(shí)現(xiàn)換膚,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • 新手學(xué)習(xí)前端之js模仿淘寶主頁(yè)網(wǎng)站

    新手學(xué)習(xí)前端之js模仿淘寶主頁(yè)網(wǎng)站

    淘寶網(wǎng)大家在熟悉不過(guò)了,那么淘寶網(wǎng)首頁(yè)模板是怎么做的呢?今天小編抽時(shí)間給大家分享新手學(xué)習(xí)前端之js模仿淘寶主頁(yè)網(wǎng)站的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • setTimeout和setInterval的區(qū)別你真的了解嗎?

    setTimeout和setInterval的區(qū)別你真的了解嗎?

    setTimeout和setInterval這兩個(gè)函數(shù), 大家肯定都不陌生, 但可能并不是每個(gè)用過(guò)這兩個(gè)方法的同學(xué), 都了解其內(nèi)部的實(shí)質(zhì)
    2011-03-03
  • flash調(diào)用js中的方法,讓js傳遞變量給flash的辦法及思路

    flash調(diào)用js中的方法,讓js傳遞變量給flash的辦法及思路

    前幾天發(fā)表了 將FlashVars寫在JS函數(shù)中,實(shí)現(xiàn)與后臺(tái)的實(shí)時(shí)變量更新,但是僅支持 IE,隨后與 Luckyer 進(jìn)行了交流,發(fā)現(xiàn)用 SetVariable 可以很方便的實(shí)現(xiàn)多瀏覽器兼容。舉例如下。
    2013-08-08

最新評(píng)論