如何使用 JavaScript DOM 将背景图像设置为固定?

htmljavascriptprogramming scripts

要在 JavaScript 中将背景图像设置为固定,请使用 backgroundAttachmentt 属性。它允许您设置不会滚动的图像。

示例

您可以尝试运行以下代码来了解如何使用 JavaScript 使用 backgroundAttachment 属性 −

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Click to Set background</button>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <p>Demo Text</p>
      <script>
         function display() {
            document.body.style.background = "url('https://www.tutorialspoint.com/html5/images/html5-mini-logo.jpg') no-repeat right top";
            document.body.style.backgroundAttachment = "fixed";
         }
      </script>
   </body>
</html>

相关文章