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

วิธีตั้งค่าตำแหน่งเริ่มต้นของภาพพื้นหลังใน JavaScript DOM


หากต้องการตั้งค่าตำแหน่งเริ่มต้นของภาพพื้นหลังใน JavaScript ให้ใช้คุณสมบัติ backgroundPosition คุณสมบัตินี้ช่วยให้คุณตั้งค่าตำแหน่งของภาพได้

ตัวอย่าง

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

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