Skip to content

Instantly share code, notes, and snippets.

@fujikky
Created November 15, 2012 00:45
Show Gist options
  • Save fujikky/4075894 to your computer and use it in GitHub Desktop.
Save fujikky/4075894 to your computer and use it in GitHub Desktop.
scroll timer
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>$title</title>
$styles
</head>
<body>
<h1>$title</h1>
<p>hello</p>
<div id="log">log</div>
$scripts
</body>
</html>
var timer;
function getY() {
return scrollY + innerHeight - 21;
}
function handleScroll() {
var log = document.getElementById('log');
var y = getY();
log.innerHTML = y;
if (y >= 2000) {
timer = setTimeout(scrollEnd, 500);
} else {
clearTimeout(timer);
}
}
function scrollEnd() {
var y = getY();
if (y >= 2000) {
document.body.style.background = 'red';
setTimeout(function () {
scrollTo(0, 0);
document.body.style.background = '';
}, 500);
}
}
window.addEventListener('scroll',handleScroll, false);
handleScroll();
body {
background: #eee;
height: 2000px;
margin: 0;
padding: 0;
}
#log {
position: fixed;
bottom: 20px;
left: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment