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

วิธีตั้งค่าพื้นที่จัดตำแหน่งของภาพพื้นหลังด้วย JavaScript


หากต้องการตั้งค่าพื้นที่จัดตำแหน่งของภาพพื้นหลังใน JavaScript ให้ใช้คุณสมบัติ backgroundOrigin

ตัวอย่าง

คุณสามารถลองรันโค้ดต่อไปนี้เพื่อเรียนรู้วิธีตั้งค่าพื้นที่จัดตำแหน่งของภาพสำหรับพื้นหลัง −

<!DOCTYPE html> <html>    <head>       <style>          #box {             border: 2px dashed yellow;             padding: 35px;             width: 600px;             height: 400px;             background: url('https://www.tutorialspoint.com/videotutorials/images/coding_ground_home.jpg') no-repeat;          }       </style>    </head>    <body>       <button onclick="display()">Set area of background image</button>       <div id="box">          This is Demo Text! This is Demo Text! This is Demo Text!          This is Demo Text! This is Demo Text!       </div>       <script>          function display() {             document.getElementById("box").style.backgroundOrigin = "content-box";          }       </script>    </body> </html>