-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsyncscroll.js
140 lines (117 loc) · 4.5 KB
/
syncscroll.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
/**
* @fileoverview syncscroll - scroll several areas simultaniously
* @version 0.0.3
*
* @license MIT, see http://github.com/asvd/intence
* @copyright 2015 asvd <heliosframework@gmail.com>
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports);
} else {
factory((root.syncscroll = {}));
}
}(this,function (exports) {
var Width = 'Width';
var Height = 'Height';
var Top = 'Top';
var Left = 'Left';
var scroll = 'scroll';
var client = 'client';
var EventListener = 'EventListener';
var addEventListener = 'add' + EventListener;
var length = 'length';
var Math_round = Math.round;
var names = {};
var reset = function() {
var elems = document.getElementsByClassName('sync'+scroll);
// clearing existing listeners
var i, j, el, found, name;
for (name in names) {
if (names.hasOwnProperty(name)) {
for (i = 0; i < names[name][length]; i++) {
names[name][i]['remove'+EventListener](
scroll, names[name][i].syn, 0
);
}
}
}
// setting-up the new listeners
for (i = 0; i < elems[length];) {
found = j = 0;
el = elems[i++];
if (!(name = el.getAttribute('name'))) {
// name attribute is not set
continue;
}
el = el[scroll+'er']||el; // needed for intence
// searching for existing entry in array of names;
// searching for the element in that entry
for (;j < (names[name] = names[name]||[])[length];) {
found |= names[name][j++] == el;
}
if (!found) {
names[name].push(el);
}
el.eX = el.eY = 0;
(function(el, name) {
el[addEventListener](
scroll,
el.syn = function() {
var elems = names[name];
var scrollX = el[scroll+Left];
var scrollY = el[scroll+Top];
var xRate =
scrollX /
(el[scroll+Width] - el[client+Width]);
var yRate =
scrollY /
(el[scroll+Height] - el[client+Height]);
var updateX = scrollX != el.eX;
var updateY = scrollY != el.eY;
var otherEl, i = 0;
el.eX = scrollX;
el.eY = scrollY;
for (;i < elems[length];) {
otherEl = elems[i++];
if (otherEl != el) {
if (updateX &&
Math_round(
otherEl[scroll+Left] -
(scrollX = otherEl.eX =
Math_round(xRate *
(otherEl[scroll+Width] -
otherEl[client+Width]))
)
)
) {
otherEl[scroll+Left] = scrollX;
}
if (updateY &&
Math_round(
otherEl[scroll+Top] -
(scrollY = otherEl.eY =
Math_round(yRate *
(otherEl[scroll+Height] -
otherEl[client+Height]))
)
)
) {
otherEl[scroll+Top] = scrollY;
}
}
}
}, 0
);
})(el, name);
}
}
if (document.readyState == "complete") {
reset();
} else {
window[addEventListener]("load", reset, 0);
}
exports.reset = reset;
}));