如何使用 JavaScript DOM 将背景图像设置为固定?
htmljavascriptprogramming scripts更新于 2024/6/17 10:20:17
要在 JavaScript 中将背景图像设置为固定,请使用 backgroundAttachmentt 属性。它允许您设置不会滚动的图像。
示例
您可以尝试运行以下代码来了解如何使用 JavaScript 使用 backgroundAttachment 属性 −
<!DOCTYPE html>
<html>
<body>
<button onclick="display()">Click to Set background</button>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<p>Demo Text</p>
<script>
function display() {
document.body.style.background = "url('https://www.tutorialspoint.com/html5/images/html5-mini-logo.jpg') no-repeat right top";
document.body.style.backgroundAttachment = "fixed";
}
</script>
</body>
</html>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

