ในการกำหนดตำแหน่งเริ่มต้นของภาพพื้นหลังใน JavaScript ให้ใช้ ตำแหน่งพื้นหลัง คุณสมบัติ. ช่วยให้คุณกำหนดตำแหน่งของภาพได้
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีกำหนดตำแหน่งทั้งหมดของภาพพื้นหลังด้วย 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>