-
Notifications
You must be signed in to change notification settings - Fork 3
/
GoScroll.js
185 lines (185 loc) · 3.52 KB
/
GoScroll.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
*
* @Name : GoScroll.js
* @Version : 1.0
* @Programmer : Max
* @Date : 2018-06-21
* @Released under : https://github.com/BaseMax/GoScrollJs/blob/master/LICENSE
* @Repository : https://github.com/BaseMax/GoScrollJs
*
**/
;(function(window,document)
{
"use strict";
/**
* @variable timeing
*
* @goal : Change Position of scroll
*
* @return int
**/
var timeing = 500;
/**
* @variable offset_default
*
* @goal : Default Offset for auto go-scroll to a element (element.height - offset)
*
* @type int
**/
var offset_default = 50;
/**
* @function change
*
* @goal : Change Position of scroll
*
* @return void
**/
var change=function(o,e,t)
{
var n = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0,
c = e - n,
l = function(o)
{
var e = animate_easeInOut(o += 20,n,c,t);
document.documentElement.scrollTop = e;
document.body.scrollTop = e;
if(o < t)
{
setTimeout(function()
{
l(o);
},20);
}
};
l(0);
};
/**
* @function animate_easeInOut
*
* @goal : Sample Animation
*
* @return number
**/
var animate_easeInOut=function(o,e,t,n)
{
return (o /= n / 2) < 1 ? t / 2 * o * o + e : (o -= 1,-t / 2 * (o * (o - 2) - 1) + e);
};
/**
* @function go_rand
*
* @goal : Begin from here and change scroll to $rand place offset
*
* @return void
**/
var go_rand=function(from)
{
//soon
return;
};
/**
* @function go_to
*
* @goal : Begin from here and change scroll to $e place offset
*
* @return void
**/
var go_to=function(from)
{
var time;
var target_place = from.getAttribute("data-scroll-to");
if(from.hasAttribute("data-scroll-offset"))
{
target_place=target_place-from.getAttribute("data-scroll-offset");
}
else
{
target_place=target_place-offset_default;
}
if(from.hasAttribute("data-scroll-time"))
{
time=from.getAttribute("data-scroll-time");
}
else
{
time=timeing;
}
change(document.body,target_place,time);
};
/**
* @function go
*
* @goal : Begin from here and change scroll to a element
*
* @return void
**/
var go=function(from)
{
var time;
var target_place = document.querySelector(from.getAttribute("data-scroll-go")).offsetTop;
if(from.hasAttribute("data-scroll-offset"))
{
target_place=target_place-from.getAttribute("data-scroll-offset");
}
else
{
target_place=target_place-offset_default;
}
if(from.hasAttribute("data-scroll-time"))
{
time=from.getAttribute("data-scroll-time");
}
else
{
time=timeing;
}
change(document.body,target_place,time);
};
/**
* @struct goscroll
*
* @goal : access to public functions
*
* @return struct
**/
window.goscroll=
{
offset:offset_default,
go:go,
goto:go_to,
};
/**
* @struct onload
*
* @goal : set onclick and events after page load...
*
* @return void
**/
window.addEventListener("load",function()
{
var data_goscroll;
data_goscroll = document.querySelectorAll("[data-scroll-go]");
data_goscroll.forEach(function(item)
{
if(item.onclick === null)//onclick not exists
{
item.onclick=function()
{
window.goscroll.go(this);
};
}
});
//////////////////////////////////////////////////////////////////
data_goscroll = document.querySelectorAll("[data-scroll-to]");
data_goscroll.forEach(function(item)
{
if(item.onclick === null)//onclick not exists
{
item.onclick=function()
{
window.goscroll.goto(this);
};
}
});
//////////////////////////////////////////////////////////////////
},false);
}(window,document));