Is there a way to fix a position on the x-axis only? So when a user scrolls up, the div tag will scroll up with it, but not side to side?
Check out the website for a list of many examples; but yes, "overflow-y: hidden" will only hide vertical scroller.. if you were to use "overflow-x: scroll; overflow-y: hidden;" then this will only enable the x-axis and the y-axis is hidden.
An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element. A fixed element does not leave a gap in the page where it would normally have been located.
1. Element with position: fixed property is fixed to the viewport and doesn't move irrespective of scrolling. Element with position: sticky property can scroll to an offset value provided by the user.
position:static; , position:absolute; , and position:relative; are the alternatives to position:fixed; . There isn't a definitive opposite, because relative , absolute , static , and fixed have different properties to behave differently.
Its a simple technique using the script also. You can check a demo here too.
JQuery
$(window).scroll(function(){ $('#header').css({ 'left': $(this).scrollLeft() + 15 //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to remain at 15px left }); });
CSS
#header { top: 15px; left: 15px; position: absolute; }
Update Credit: @PierredeLESPINAY
As commented, to make the script support the changes in the css without having to recode them in the script. You can use the following.
var leftOffset = parseInt($("#header").css('left')); //Grab the left position left first $(window).scroll(function(){ $('#header').css({ 'left': $(this).scrollLeft() + leftOffset //Use it later }); });
Demo :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With