-
Notifications
You must be signed in to change notification settings - Fork 41
/
script.js
65 lines (59 loc) · 2.32 KB
/
script.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
var Resume = function(window, document) {
var start = NaN;
var dest = NaN;
var hash;
var step;
var scroll = function() {
var temp;
if(window.innerWidth < 650 || start == document.documentElement.scrollTop + document.body.scrollTop) {
window.location.hash = hash;
start = dest = NaN;
return;
}
start = document.documentElement.scrollTop + document.body.scrollTop;
temp = start > dest ? start + Math.floor((dest - start)/10) : start + Math.ceil((dest - start)/10);
if(Math.abs(dest - temp) > 1) {
window.scrollTo(0, temp);
step = window.setTimeout(scroll, 16);
} else {
window.scrollTo(0, dest);
window.location.hash = hash;
start = dest = NaN;
}
};
var init = function() {
var sect = document.getElementsByTagName('section');
var nav = document.createElement('nav');
var ul = document.createElement('ul');
Array.prototype.forEach.call(sect, function(s, i) {
ul.innerHTML += '<li><a href="#' + s.id + '">' + s.querySelector('h2').innerHTML + '</a></li>';
s.addEventListener('click', function(e) {
setTimeout(function() {
window.location.hash = '#' + s.id;
}, 10);
}, false);
});
nav.appendChild(ul);
nav.addEventListener('click', function(e) {
e.preventDefault();
if(e.target.tagName.toLowerCase() == 'a') {
dest = document.getElementById(e.target.getAttribute('href').substr(1)).offsetTop;
hash = e.target.getAttribute('href');
clearTimeout(step);
scroll();
}
}, false);
document.body.insertBefore(nav, document.querySelector('div'));
window.addEventListener('resize', function() {
document.body.className = window.innerWidth < 650 ? 'mobile' : 'desktop';
}, false);
window.addEventListener('load', function() {
if(window.location.hash == '') {
window.location.hash = '#' + sect[0].id;
}
document.body.className = window.innerWidth < 650 ? 'mobile' : 'desktop';
}, false);
};
init();
};
new Resume(window, document);