Skip to content

Instantly share code, notes, and snippets.

@7cc
Created September 11, 2013 08:24
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 7cc/6520753 to your computer and use it in GitHub Desktop.
Save 7cc/6520753 to your computer and use it in GitHub Desktop.
window.pageYOffset always returns 0 in some CSS
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>body scroll in Standards Mode</title>
<style>
html{
height: 100%;
overflow: hidden;
}
body{
height: 100%;
overflow: auto;
}
/* irrelevant */
#mid{
margin: 500px 0
}
.title{
display: inline-block;
width: 3.2em
}
button{
width: 5.3em
}
</style>
</head>
<body>
<p>
<span class="title">window</span>
<button id="to-bottom">bottom</button>
<button id="to-top">top</button><br>
<span class="title">html</span>
<button id="html-bottom">bottom</button>
<button id="html-top">top</button><br>
<span class="title">body</span>
<button id="body-bottom">bottom</button>
<button id="body-top">top</button>
</p>
<ul>
<li>body scroll by html (directly)
<li>window can scroll by JS
<li>document.body.scrollTop
<ul>
<li>returns: Firefox, IE
<li>not returns: Webkit, GC
</ul>
</ul>
<pre style="border: 1px solid orange">
var all = document.getElementsByTagName("*")
for(var i=all.length; i--;){
all[i].scrollTop = 100
}
</pre>
<p><a href="#mid">#mid</a></p>
<p id="mid">#mid</p>
<p><a href="#top">top</a>
<script>
window["to-bottom"].addEventListener("click", function(){
scrollTo(0, 1000)
}, false)
window["to-top"].addEventListener("click", function(){
scrollTo(0, 0)
}, false)
window["html-bottom"].addEventListener("click", function(){
document.documentElement.scrollTop = 1000
}, false)
window["html-top"].addEventListener("click", function(){
document.documentElement.scrollTop = 0
}, false)
window["body-bottom"].addEventListener("click", function(){
document.body.scrollTop = 1000
}, false)
window["body-top"].addEventListener("click", function(){
document.body.scrollTop = 0
}, false)
;[window, document, document.documentElement, document.body].forEach(function(e){
e.addEventListener("scroll", logEvent, false)
})
function logEvent(e){
console.log(e.currentTarget, e.target)
}
</script>
</body>
</html>
@7cc
Copy link
Author

7cc commented Sep 11, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment