HTML DOM中的Style backgroundAttachment属性用于设置或返回背景图像是固定的还是随内容滚动。
用法:
- 它返回backgroundAttachment属性。
object.style.backgroundAttachment
- 它用于设置backgroundAttachment属性。
object.style.backgroundAttachment = "scroll|fixed|local|initial| inherit"
属性值:每个属性值的示例说明。
-
滚动:此值使背景图像与元素一起滚动。它是默认值。
-
Example:
<!DOCTYPE html> <html> <head> <title> DOM Style backgroundAttachment Property </title> <style> body { background:url( 'https://media.geeksforgeeks.org/wp-content/uploads/20190311222622/sample-image.png') no-repeat right top / 200px; background-attachment:fixed; } #scrolling-area { height:1000px; } </style> </head> <body> <h1 style="color:green; margin-top:100px;"> GeeksforGeeks </h1> <b>DOM Style backgroundAttachment Property</b> <p> Click on the button to change the attachment of the background image to 'scroll'. </p> <button onclick="changeAttachment()"> Change attachment </button> <div id="scrolling-area"><br> This is a large area for scrolling. </div> <!-- Script to change backgroundAttachment --> <script> function changeAttachment() { document.body.style.backgroundAttachment = 'scroll'; } </script> </body> </html>
- Output:
在单击按钮之前:
单击按钮后:
- Example:
<!DOCTYPE html> <html> <head> <title> DOM Style backgroundAttachment Property </title> <style> body { background:url( 'https://media.geeksforgeeks.org/wp-content/uploads/20190311222622/sample-image.png') no-repeat right top / 200px; } #scrolling-area { height:1000px; } </style> </head> <body> <h1 style="color:green; margin-top:100px;"> GeeksforGeeks </h1> <b>DOM Style backgroundAttachment Property</b> <p> Click on the button to change the attachment of the background image to 'scroll'. </p> <button onclick="changeAttachment()"> Change attachment </button> <div id="scrolling-area"><br> This is a large area for scrolling. </div> <!-- Script to change backgroundAttachment --> <script> function changeAttachment() { document.body.style.backgroundAttachment = 'fixed'; } </script> </body> </html>
- Output:
在单击按钮之前:
单击按钮后:
- Example:
<!DOCTYPE html> <html> <head> <title> DOM Style backgroundAttachment Property </title> <style> body { background:url( 'https://media.geeksforgeeks.org/wp-content/uploads/20190311222622/sample-image.png') no-repeat right top / 200px; background-attachment:fixed; } #scrolling-area { height:1000px; } </style> </head> <body> <h1 style="color:green; margin-top:100px;"> GeeksforGeeks </h1> <b>DOM Style backgroundAttachment Property</b> <p> Click on the button to change the attachment of the background image to 'scroll'. </p> <button onclick="changeAttachment()"> Change attachment </button> <div id="scrolling-area"><br> This is a large area for scrolling. </div> <!-- Script to change backgroundAttachment --> <script> function changeAttachment() { document.body.style.backgroundAttachment = 'local'; } </script> </body> </html>
- Output:
在单击按钮之前:
单击按钮后:
- Example:
<!DOCTYPE html> <html> <head> <title> DOM Style backgroundAttachment Property </title> <style> body { background:url( 'https://media.geeksforgeeks.org/wp-content/uploads/20190311222622/sample-image.png') no-repeat right top / 200px; background-attachment:fixed; } #scrolling-area { height:1000px; } </style> </head> <body> <h1 style="color:green; margin-top:100px;"> GeeksforGeeks </h1> <b>DOM Style backgroundAttachment Property</b> <p> Click on the button to change the attachment of the background image to 'scroll'. </p> <button onclick="changeAttachment()"> Change attachment </button> <div id="scrolling-area"><br> This is a large area for scrolling. </div> <!-- Script to change backgroundAttachment --> <script> function changeAttachment() { document.body.style.backgroundAttachment = 'initial'; } </script> </body> </html>
- Output:
在单击按钮之前:
单击按钮后:
- Example:
<!DOCTYPE html> <html> <head> <title> DOM Style backgroundAttachment Property </title> <style> .bg-img { height:150px; background:url( 'https://media.geeksforgeeks.org/wp-content/uploads/20190311222622/sample-image.png') no-repeat right top / 200px; } #parent { background-attachment:fixed; height:1000px; } </style> </head> <body> <div id="parent"> <div class="bg-img"></div> <h1 style="color:green;"> GeeksforGeeks </h1> <b> DOM Style backgroundAttachment Property </b> <p> Click on the button to change the attachment of the background image to 'inherit'. </p> <button onclick="changeAttachment()"> Change attachment </button> </div> <!-- Script to change backgroundAttachment property --> <script> function changeAttachment() { elem = document.querySelector('.bg-img'); elem.style.backgroundAttachment = 'inherit'; } </script> </body> </html>
- Output:
在单击按钮之前:
单击按钮后:
固定:此值可使背景图像相对于视口固定。
本地:此值可使背景图像随元素的内容一起滚动。
初始值:用于将此属性设置为其默认值。
继承:它从其父元素继承属性。
支持的浏览器:以下列出了DOM Style backgroundAttachment属性支持的浏览器:
- 谷歌浏览器1.0
- Internet Explorer 4.0
- Firefox 1.0
- Opera 3.5
- Safari 1.0
相关用法
- HTML Style top用法及代码示例
- HTML Style right用法及代码示例
- HTML Style textAlign用法及代码示例
- HTML Style wordSpacing用法及代码示例
- HTML Style borderRight用法及代码示例
- HTML Style borderLeft用法及代码示例
- HTML Style height用法及代码示例
- HTML Style whiteSpace用法及代码示例
- HTML Style textDecorationLine用法及代码示例
- HTML Style opacity用法及代码示例
- HTML Style columnRuleStyle用法及代码示例
- HTML Style display用法及代码示例
- HTML Style transformStyle用法及代码示例
- HTML Style visibility用法及代码示例
- HTML Style animationDirection用法及代码示例
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 HTML | DOM Style backgroundAttachment Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。