如何使用 CSS 创建滚动时的渐变背景色?

cssweb developmentfront end technology更新于 2025/12/3 5:32:17

要创建滚动时的渐变背景色,代码如下: −

示例

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      height: 250vh;
      color: white;
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      background: linear-gradient(
         141deg,
         #a47dff 0%,
         #4e28a7 40%,
         #22053d 65%,
         #72a4ff 75%
      );
   }
</style>
</head>
<body>
<h1>Change Background Gradient on Scroll Example</h1>
<p style="font-size: 30px;">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus, laborum minus? Vero accusantium laborum quas cum, sed obcaecati quibusdam dignissimos.
</p>
<h2 style="position:fixed;">Scroll to see the effect.</h2>
</body>
</html>

输出

上述代码将产生以下输出 −

滚动时渐变将从深蓝色变为浅蓝色 −


相关文章


有用资源