On a Desktop browser, I can use the following javascript to vertically fix an element, but still allow for horizontal scroll. As you see, it re-positions the element on every scroll event. To see what I mean, try horizontally and vertically scrolling in this JSFiddle.
var verticallyFixedBox = document.getElementById('verticallyFixedBox');
window.addEventListener('scroll', function() {
verticallyFixedBox.style.top = '' + document.body.scrollTop + 'px';
});
However, this logic fails for mobile browsers since mobile browsers apparently do not paint until an entire drag completes, leading to a choppy experience.
What's a good way to vertically fix an element, but still allow for horizontal scrolling for mobile browsers?
If you use a container box that is set to
height: 100%
and set the height of thehtml
andbody
elements to 100%, then you can useposition: absolute; top: 0;
for the fixed header and setoverflow: auto
(orscroll
) for the box that you want to scroll vertically.Remember to set your body and html margins to 0 to avoid browser default styles.
I've updated the fiddle you had: http://jsfiddle.net/jb489ddL/1/
This solution doesn't use javascript at all so it will work on mobile browsers, since you're not relying on the scroll event.