Javascript DOM CSS Style backgroundAttachment Property set scroll vs local

Introduction

Choose between scroll and local on a DIV element:

document.getElementById("myDIV").style.backgroundAttachment = "local";

Click the radio buttons and scroll the DIV element to see the effect of the background-attachment property:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//from w  w w  . j a  v  a  2s. c  o  m
  width: 300px;
  height: 300px;
  background: lightblue url('background2.png') no-repeat;
  color: white;
  overflow: auto;
}
</style>
</head>
<body>

<form name="myForm">
  <input type="radio" name="myAtt" value="scroll" onclick="myFunction()" checked>scroll
  <input type="radio" name="myAtt" value="local" onclick="myFunction()">local
</form>

<div id="myDIV">
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
  <h1>Hello</h1>
</div>

<script>
function myFunction() {
  if (document.forms["myForm"]["myAtt"][0].checked) {
    document.getElementById("myDIV").style.backgroundAttachment = "scroll";
  } else {
    document.getElementById("myDIV").style.backgroundAttachment = "local";
  }
}
</script>

</body>
</html>



PreviousNext

Related