Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

คุณสมบัติ background-attachment: ควบคุมการเลื่อนของภาพพื้นหลัง


คุณสมบัติ background-attachment

เพื่อควบคุมการเลื่อนของภาพในพื้นหลัง ให้ใช้คุณสมบัติ background-attachment

ตัวอย่าง

ลองรันโค้ดต่อไปนี้เพื่อเรียนรู้การทำงานของคุณสมบัติ background-attachment:

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        background-image: url('/css/images/css.jpg');
        background-repeat: no-repeat;
        background-attachment: fixed;
      }
    </style>
  </head>
  <body>
    <p>The background-image is fixed. Try to scroll down the page.</p>
    <p>The background-image is fixed. Try to scroll down the page.</p>
    <p>The background-image is fixed. Try to scroll down the page.</p>
    <p>The background-image is fixed. Try to scroll down the page.</p>
    <p>The background-image is fixed. Try to scroll down the page.</p>
    <p>The background-image is fixed. Try to scroll down the page.</p>
    <p>The background-image is fixed. Try to scroll down the page.</p>
    <p>The background-image is fixed. Try to scroll down the page.</p>
  </body>
</html>

ค่าที่ใช้ได้:

  • fixed - ภาพพื้นหลังจะคงที่ ไม่เลื่อนตามเนื้อหา
  • scroll - ภาพพื้นหลังจะเลื่อนไปพร้อมกับเนื้อหา (ค่าเริ่มต้น)
  • local - ภาพพื้นหลังจะเลื่อนพร้อมกับเนื้อหาขององค์ประกอบนั้นๆ

หมายเหตุ: ในโค้ดต้นฉบับมีการประกาศ background-attachment ซ้ำกัน 2 ครั้ง ควรใช้เพียงครั้งเดียวเท่านั้น