Skip to content

Instantly share code, notes, and snippets.

@pguerrant
Created April 12, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pguerrant/105e8d91e3ffcb1b6e2eed7ecc0571d3 to your computer and use it in GitHub Desktop.
Save pguerrant/105e8d91e3ffcb1b6e2eed7ecc0571d3 to your computer and use it in GitHub Desktop.
iOS/Safari bug - when using -webkit-overflow-scrolling:touch, dynamically setting overflow to hidden on either axis resets visual scroll position
<!DOCTYPE html>
<html>
<head>
<style>
#container {
background: yellowgreen;
height: 400px;
width: 400px;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
#content {
background: green;
height: 200px;
width: 200px;
margin: 350px;
}
.hidden-y {
overflow-y: hidden !important;
}
</style>
</head>
<body>
<div id="container">
<div id="content"></div>
</div>
<script>
(function() {
var container = document.getElementById('container');
container.scrollTop = 150;
container.scrollLeft = 150;
setTimeout(function() {
// incorrectly visually resets scrollTop to 0 on screen.
container.className = 'hidden-y';
// even though visual scroll position was reset scrollTop is still reported as being 150
console.log(container.scrollTop);
setTimeout(function() {
// try to reset the scroll position back to where it was previously.
container.scrollTop = 150;
// Nope! visual position on screen is still wrong.
// only way to fix the scroll position is to force it to some value other
// than the original value and then back again.
container.scrollTop = 0;
container.scrollTop = 150;
}, 1000);
}, 1000);
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment