-
Notifications
You must be signed in to change notification settings - Fork 0
/
canyouscroll.js
72 lines (65 loc) · 2.02 KB
/
canyouscroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(function(){
var form = document.getElementsByTagName('form')[0],
out = document.getElementById('distract'),
result = document.getElementsByTagName('output')[0],
scrollison = false,
start = 0,
end = 0,
goal = 0;
result.tabIndex = '-1';
form.addEventListener('submit', function(ev){
var val = document.getElementById('amount');
window.scrollTo( 0, 0 );
if( +val.value > 0 ) {
form.className = 'hidden';
out.className = 'visible';
scrollison = true;
goal = +val.value;
}
ev.preventDefault();
},false);
window.addEventListener('scroll',function(ev){
if (scrollison) {
// this is so cheating :)
//console.log(document.body.scrollTop);
}
}, false);
document.body.addEventListener('click',function(ev){
if (scrollison) { endthisnonsense(); }
}, false);
out.addEventListener('click',function(ev){
if (scrollison) { endthisnonsense(); }
}, false);
function endthisnonsense() {
end = document.body.scrollTop || document.documentElement.scrollTop;
start = 0;
window.scrollTo( 0, 0 );
scrollison = false;
out.className = '';
form.className = '';
var msg = 'You said you will scroll ' + goal + ' pixels. ';
var off = goal - end;
var tweet = '';
if (off === 0) {
msg += 'You totally nailed it!';
tweet = 'I tried to scroll ' + goal + ' pixels at ' +
'http://codepo8.github.com/canyouscroll/ and I got it'+
' right! Your turn! #canyouscroll';
}
if (off > 0) {
msg += 'But you were ' + off + ' pixels short.';
tweet = 'I tried to scroll ' + goal + ' pixels at '+
'http://codepo8.github.com/canyouscroll/ and was '+
off + ' pixels short. Your turn! #canyouscroll';
}
if (off < 0) {
msg += 'But you were ' + -off + ' pixels over.';
tweet = 'I tried to scroll ' + goal + ' pixels at '+
'http://codepo8.github.com/canyouscroll/ and was '+
-off + ' pixels over. Your turn! #canyouscroll';
}
result.innerHTML = msg + ' <a href="http://twitter.com/home?status='+
encodeURIComponent(tweet)+'">Tweet this</a>';
result.focus();
}
})();