[javascript] How can I make a div stick to the top of the screen once it's been scrolled to?

And here's how without jquery (UPDATE: see other answers where you can now do this with CSS only)

_x000D_
_x000D_
var startProductBarPos=-1;_x000D_
window.onscroll=function(){_x000D_
  var bar = document.getElementById('nav');_x000D_
  if(startProductBarPos<0)startProductBarPos=findPosY(bar);_x000D_
_x000D_
  if(pageYOffset>startProductBarPos){_x000D_
    bar.style.position='fixed';_x000D_
    bar.style.top=0;_x000D_
  }else{_x000D_
    bar.style.position='relative';_x000D_
  }_x000D_
_x000D_
};_x000D_
_x000D_
function findPosY(obj) {_x000D_
  var curtop = 0;_x000D_
  if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent) {_x000D_
    while (obj.offsetParent) {_x000D_
      curtop += obj.offsetTop;_x000D_
      obj = obj.offsetParent;_x000D_
    }_x000D_
    curtop += obj.offsetTop;_x000D_
  }_x000D_
  else if (obj.y)_x000D_
    curtop += obj.y;_x000D_
  return curtop;_x000D_
}
_x000D_
* {margin:0;padding:0;}_x000D_
.nav {_x000D_
  border: 1px red dashed;_x000D_
  background: #00ffff;_x000D_
  text-align:center;_x000D_
  padding: 21px 0;_x000D_
_x000D_
  margin: 0 auto;_x000D_
  z-index:10; _x000D_
  width:100%;_x000D_
  left:0;_x000D_
  right:0;_x000D_
}_x000D_
_x000D_
.header {_x000D_
  text-align:center;_x000D_
  padding: 65px 0;_x000D_
  border: 1px red dashed;_x000D_
}_x000D_
_x000D_
.content {_x000D_
  padding: 500px 0;_x000D_
  text-align:center;_x000D_
  border: 1px red dashed;_x000D_
}_x000D_
.footer {_x000D_
  padding: 100px 0;_x000D_
  text-align:center;_x000D_
  background: #777;_x000D_
  border: 1px red dashed;_x000D_
}
_x000D_
<header class="header">This is a Header</header>_x000D_
<div id="nav" class="nav">Main Navigation</div>_x000D_
<div class="content">Hello World!</div>_x000D_
<footer class="footer">This is a Footer</footer>
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to scroll

How to window.scrollTo() with a smooth effect Angular 2 Scroll to bottom (Chat style) Scroll to the top of the page after render in react.js Get div's offsetTop positions in React RecyclerView - How to smooth scroll to top of item on a certain position? Detecting scroll direction How to disable RecyclerView scrolling? How can I scroll a div to be visible in ReactJS? Make a nav bar stick Disable Scrolling on Body

Examples related to positioning

jQuery position DIV fixed at top on scroll CSS transition effect makes image blurry / moves image 1px, in Chrome? How can I fill a div with an image while keeping it proportional? In jQuery how can I set "top,left" properties of an element with position values relative to the parent and not the document? sizing div based on window width Make div stay at bottom of page's content all the time even when there are scrollbars How to position the div popup dialog to the center of browser screen? How do I center an SVG in a div? CSS: Position text in the middle of the page Stopping fixed position scrolling at a certain point?