ในการตั้งค่าพื้นที่การวางตำแหน่งของภาพพื้นหลังใน 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>