I'm looking for a way to position the #header element of my page as "fixed" only after having scrolled for about 100% to the right.
Here is the JSFiddle demo: http://jsfiddle.net/3zrw2xko/1/
Here is the JavaScript:
var header = $("#header");
$(document).scroll(function() {
if($(this).scrollLeft() >= window.innerWidth) {
header.css(
{
"display":"",
"position" : "fixed",
"top" : "0",
"left" : "0",
"width" : "50px",
"height" : "50px",
"background" : "red",
"z-index" : "9999"
}
);
} else {
header.css({"display" : "none"});
}
});
But there's one last issue: when I refresh the page of my real project containing this script, the red square disappears and I have to scroll again.
I think that this is about the use of
window.innerWidth
Because it takes the position calculated from the current position. The ideal solution would be to take the position calculated from the left of the document, and not from the left of the current window.