如何在 JavaScript DOM 中设置背景图像的起始位置?
htmljavascriptprogramming scripts更新于 2024/6/20 10:52:17
要在 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>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

