-
Notifications
You must be signed in to change notification settings - Fork 3
/
fx.js
135 lines (116 loc) · 2.92 KB
/
fx.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
//
// Section: Effects
//
//
// Function: fade
// Fades a DOM element in or out.
//
// Parameters:
// el - the element to fade
// options - an object of options to use. This object must have a *fade*
// property, which should be either the string 'in' or 'out',
// corresponding to the direction of the fade. The second
// property used here, *onComplete*, is a function that is called
// once the fade is complete. This is optional.
//
// Returns:
// nothing
//
function fade (el, options)
{
var current;
var proxy = el.cloneNode(true);
var direction = (options.fade == 'in') ? 1 : -1;
el.parentNode.replaceChild(proxy, el);
if (options.fade == 'in')
{
current = 0;
proxy.style.visibility = 'visible';
}
else
current = 1;
setOpacity(proxy, current);
var interval = window.setInterval(tick, 25);
function tick()
{
current += 0.05 * direction;
setOpacity(proxy, Math.easeInOut(current));
if (((direction == 1) && (current >= 1))
|| ((direction == -1) && (current <= 0)))
{
console.log('swapping fader proxy out');
el.style.visibility = (options.fade == 'in') ? 'visible' : 'hidden';
proxy.parentNode.replaceChild(el, proxy);
delete proxy;
window.clearInterval(interval);
if (options.onComplete)
options.onComplete();
}
};
function setOpacity (el, opacity)
{
var percent = Math.floor(opacity * 100);
// IE
el.style.zoom = 1;
el.style.filter = 'alpha(opacity=' + percent + ')';
// CSS 3
el.style.opacity = opacity;
};
};
//
// Function: scrollWindowTo
// This scrolls the browser window to ensure that a DOM element is
// in view. Make sure that the element has been added to the page
// before calling this function.
//
// Parameters:
// el - the element to scroll to.
//
// Returns:
// nothing
//
function scrollWindowTo (el)
{
var start = window.scrollY ? window.scrollY : document.body.scrollTop;
var end = ensureVisible(el);
var distance = Math.abs(start - end);
var progress = 0;
var direction = (start > end) ? -1 : 1;
var interval = window.setInterval(tick, 25);
function tick()
{
progress += 0.1;
window.scrollTo(0, start + direction * (distance * Math.easeInOut(progress)));
if (progress >= 1)
window.clearInterval(interval);
};
function ensureVisible (el)
{
var posTop = findPosY(el);
var posBottom = posTop + el.offsetHeight;
var winTop = window.scrollY ? window.scrollY : document.body.scrollTop;
var winHeight = window.innerHeight ? window.innerHeight : document.body.clientHeight;
var winBottom = winTop + winHeight;
if (posTop < winTop)
return posTop;
else if (posBottom > winBottom)
{
if (el.offsetHeight < winHeight)
return (posTop - (winHeight - el.offsetHeight) + 20);
else
return posTop;
}
else
return posTop;
};
function findPosY (el)
{
var curtop = 0;
while (el.offsetParent)
{
curtop += el.offsetTop;
el = el.offsetParent;
}
return curtop;
};
}