你正在瀏覽的是 Tailwind CSS v2. 的技術文件
Tailwind CSS 的 GitHub

Background Attachment (背景滾動)

滾動時,控制背景圖片行為的功能

Default class reference

Class
屬性
bg-fixedbackground-attachment: fixed;
bg-localbackground-attachment: local;
bg-scrollbackground-attachment: scroll;

Fixed

在頁面捲動時,使用 bg-fixed 固定背景圖片

<div class="bg-fixed ..." style="background-image: url(...)"></div>

Local

使用 bg-local,背景圖片會隨著 container 及 viewport 一起滾動。

<div class="bg-local ..." style="background-image: url(...)"></div>

Scroll

使用 bg-scroll 時,背景圖片會隨著 viewport 一起滾動,,而不是隨著 container 滾動。

<div class="bg-scroll ..." style="background-image: url(...)"></div>

響應式

如要讓元素的背景滾動有響應式效果,可在現有背景滾動功能前加上前綴詞 {screen}: 。例如:中型或以上的螢幕尺寸使用 md:bg-fixed 以套用 bg-fixed 功能。

<div class="bg-local md:bg-fixed ...">
  <!-- ... -->
</div>

更多有關 Tailwind 響應式設計功能的資訊,可參考 響應式設計 文件。

客製化

變化模式

默認情況下, 只有 響應式 的 background attachment 變化模式 (variants) 會產生。

tailwind.config.jsvariants 區塊中變更 backgroundAttachment 屬性來決定有哪些變化模式會生成。

舉個例子來說,這個設定將會生成 響應式 和 hover 的變化模式。

  // tailwind.config.js
  module.exports = {
    variants: {
      extend: {
        // ...
+       backgroundAttachment: ['responsive', 'hover'],
      }
    }
  }

停用

如果你不打算在專案中使用 background attachment 功能,可以在你的設定檔裡的 corePlugins 屬性中將 backgroundAttachment 設定為 false 來停用功能:

  // tailwind.config.js
  module.exports = {
    corePlugins: {
      // ...
+     backgroundAttachment: false,
    }
  }