I am looking to have a div stick to the top of the page when someone scrolls down
When the page is scrolled, the green div with class stickdiv
should automatically stick to the top
var left = document.getElementsByClassName("stickdiv");
for( var i = 0;i<left.length;i++){
var stop = (left[0].offsetTop);
window.onscroll = function (e) {
var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
if (scrollTop >= stop) {
left.className += " stick"; //adding a class name
} else {
className = '';
}
}
}
Why does adding the stick class to the div not work? - https://jsfiddle.net/kzk5qab2/1/
I want the div to stick at the top like the yellow div in this example - http://jsfiddle.net/qc4NR/