Javascript DOM CSS Style backgroundAttachment Property set

Introduction

Set the background-image to be fixed:

document.body.style.backgroundAttachment = "fixed";

Try to to resize the window if the scrollbar is not available.

Scroll the page before and after you have clicked the button.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
body {/*from www.j a  v a  2  s. c  o m*/
  background: #f3f3f3 url('image1.png') no-repeat right top;
}
</style>
</head>
<body>

<h1>Hello World!</h1>

<button onclick="myFunction()">Set background image to be fixed</button>
<br>

<script>
function myFunction() {
  document.body.style.backgroundAttachment = "fixed";
}
</script>

<p>Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
Some text to enable scrolling..
<br><br><br><br>
</p>

</body>
</html>



PreviousNext

Related