5

I wanted to change margin-top of a div which has margin-top:80px; by default. When people scroll the page down it should change its margin-top:40px . This has to be done in JS. I searched on web about this and all i could get is a tutorial on JQuery. Could any one help me with pure JS. I dont want JQuery or any other framework as it loads all code for this simple one.

Any help will be appreciated.

Thank You.

PHP Newbie
  • 79
  • 1
  • 1
  • 2

2 Answers2

12

document.getElementById('YouDiv').style.marginTop='40px';
#YouDiv {
  border:1px solid black;
}
<div>This is other Div</div>
<div id="YouDiv">This is YouDiv</div>
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
6
window.onscroll = function() { document.getElementById('id').style.margin = '40px 0 0 0' }
Artek Wisniewski
  • 797
  • 5
  • 13