forked from BritneyMuller/search-engine-optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
112 lines (89 loc) · 3.26 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(function(window, document) {
'use strict';
// Bling
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function(name, fn) {
this.addEventListener(name, fn);
};
NodeList.prototype.forEach = Array.prototype.forEach;
NodeList.prototype.on = NodeList.prototype.addEventListener = function(name, fn) {
this.forEach(function(element, i) {
element.on(name, fn);
});
};
// easing
var easing = {
linear: function (t) { return t; },
easeInQuad: function (t) { return t * t; },
easeOutQuad: function (t) { return t * (2 - t); },
easeInOutQuad: function (t) { return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t; },
easeInCubic: function (t) { return t * t * t; },
easeOutCubic: function (t) { return (--t) * t * t + 1; },
easeInOutCubic: function (t) { return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1; },
easeInQuart: function (t) { return t * t * t * t; },
easeOutQuart: function (t) { return 1 - (--t) * t * t * t; },
easeInOutQuart: function (t) { return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t; },
easeInQuint: function (t) { return t * t * t * t * t; },
easeOutQuint: function (t) { return 1 + (--t) * t * t * t * t; },
easeInOutQuint: function (t) { return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t; }
};
// requestAnimationFrame
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
var i;
var length = vendors.length;
for (i = 0; i < length && !requestAnimationFrame; ++i) {
requestAnimationFrame = [vendors[i] + 'RequestAnimationFrame'];
cancelAnimationFrame = [vendors[i] + 'CancelAnimationFrame'] || [vendors[i] + 'CancelRequestAnimationFrame'];
}
if (!requestAnimationFrame)
requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!cancelAnimationFrame) {
cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
// scrollTo
function scrollTo(Y, duration, easingFunction, callback) {
var start = Date.now();
var elem = document.documentElement.scrollTop ? document.documentElement : document.body;
var from = elem.scrollTop;
if (from === Y) {
callback();
return;
}
function scroll(timestamp) {
var currentTime = Date.now();
var time = Math.min(1, ((currentTime - start) / duration));
var easedT = easingFunction(time);
elem.scrollTop = (easedT * (Y - from)) + from;
if (time < 1) {
requestAnimationFrame(scroll);
} else {
if (callback) {
callback();
}
}
}
requestAnimationFrame(scroll);
}
$('a[href^="#"]').on('click', function() {
scrollTo(document.getElementById(this.href.split('#')[1]).offsetTop, 800, easing.easeInOutQuad);
});
// Links
var links = document.links;
length = links.length;
for (i = 0; i < length; i++) {
if (links[i].hostname != window.location.hostname) {
links[i].target = '_blank';
}
}
}(window, document));