forked from jtnix/jQuery-LavaLamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.lavalamp.js
257 lines (233 loc) · 7.54 KB
/
jquery.lavalamp.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* jquery.LavaLamp v1.4.1 - light up your menus with fluid, jQuery powered animations.
*
* Requires jQuery version 1.7 or later from https://jquery.com
* Tested on jQuery versions 1.7 through 2.2.4
*
* https://nixbox.com/projects/jquery-lavalamp/
*
* Copyright (c) 2008, 2009, 2010, 2012 Jolyon Terwilliger, [email protected]
* Source code Copyright (c) 2008, 2009, 2010, 2012
* Dual licensed under the MIT and GPL licenses:
* https://www.opensource.org/licenses/mit-license.php
* https://www.gnu.org/licenses/gpl.html
*
* LavaLamp Usage and Release History moved to
* github page at https://github.com/jtnix/jQuery-LavaLamp
**/
(function($) {
jQuery.fn.lavaLamp = function(o) {
o = $.extend(
{
target: "li",
container: "",
fx: "swing",
speed: 500,
click: function() {
return true;
},
startItem: "",
includeMargins: false,
autoReturn: true,
returnDelay: 0,
setOnClick: true,
homeTop: 0,
homeLeft: 0,
homeWidth: 0,
homeHeight: 0,
returnHome: false,
autoResize: false,
selectClass: "selectedLava",
homeClass: "homeLava",
skipClass: "noLava",
returnStart: function() {},
returnFinish: function() {},
hoverStart: function() {},
hoverFinish: function() {}
},
o || {}
);
// safe parseInt for easy mathing
function getInt(arg) {
var myint = parseInt(arg);
return isNaN(myint) ? 0 : myint;
}
if (o.container == "") o.container = o.target;
if (o.autoResize)
$(window).resize(function() {
$(o.target + "." + o.selectClass).trigger("mouseenter");
});
return this.each(function() {
// ensures parent UL or OL element has some positioning
if ($(this).css("position") == "static")
$(this).css("position", "relative");
// create homeLava element if origin dimensions set
if (o.homeTop || o.homeLeft) {
var $home = $(
"<" +
o.container +
' class="' +
o.homeClass +
'"></' +
o.container +
">"
).css({
left: o.homeLeft,
top: o.homeTop,
width: o.homeWidth,
height: o.homeHeight,
position: "absolute",
display: "block"
});
$(this).prepend($home);
}
var path = location.pathname + location.search + location.hash,
$selected,
$back,
$lt = $(o.target, this).not("." + o.skipClass),
delayTimer,
bx = 0,
by = 0,
mh = 0,
mw = 0,
ml = 0,
mt = 0;
// start $selected default with CSS `selectedClass`
$selected = $(o.target + "." + o.selectClass, this);
// override $selected if startItem is set
if (o.startItem != "") $selected = $lt.eq(o.startItem);
// default to $home element
if ((o.homeTop || o.homeLeft) && $selected.length < 1) $selected = $home;
// loop through all the target element a href tags and
// the longest href to match the location path is deemed the most
// accurate and selected as default
if ($selected.length < 1) {
var pathmatch_len = 0,
$pathel;
$lt.each(function() {
var thishref = $("a:first", this).attr("href");
if (path.indexOf(thishref) > -1 && thishref.length > pathmatch_len) {
$pathel = $(this);
pathmatch_len = thishref.length;
}
});
if (pathmatch_len > 0) {
$selected = $pathel;
}
}
// if still no matches, default to the first element
if ($selected.length < 1) $selected = $lt.eq(0);
// make sure we only have one element as $selected and apply selectedClass
$selected = $($selected.eq(0).addClass(o.selectClass));
// add mouseover event for every sub element
$lt
.on("mouseenter focusin", function() {
// help backLava behave if returnDelay is set
if (delayTimer) {
clearTimeout(delayTimer);
delayTimer = null;
}
move($(this));
})
.click(function(e) {
if (o.setOnClick) {
$selected.removeClass(o.selectClass);
$selected = $(this).addClass(o.selectClass);
}
return o.click.apply(this, [e, this]);
});
// creates and adds to the container a backLava element with absolute positioning
$back = $(
"<" +
o.container +
' class="backLava"><div class="leftLava"></div><div class="bottomLava"></div><div class="cornerLava"></div></' +
o.container +
">"
)
.css({ position: "absolute", display: "block", margin: 0, padding: 0 })
.prependTo(this);
// setting css height and width actually sets the innerHeight and innerWidth, so
// compute border and padding differences on styled backLava element to fit them in also.
if (o.includeMargins) {
mh =
getInt($selected.css("marginTop")) +
getInt($selected.css("marginBottom"));
mw =
getInt($selected.css("marginLeft")) +
getInt($selected.css("marginRight"));
}
bx =
getInt($back.css("borderLeftWidth")) +
getInt($back.css("borderRightWidth")) +
getInt($back.css("paddingLeft")) +
getInt($back.css("paddingRight")) -
mw;
by =
getInt($back.css("borderTopWidth")) +
getInt($back.css("borderBottomWidth")) +
getInt($back.css("paddingTop")) +
getInt($back.css("paddingBottom")) -
mh;
// set the starting position for the lavalamp hover element: .back
if (o.homeTop || o.homeLeft)
$back.css({
left: o.homeLeft,
top: o.homeTop,
width: o.homeWidth,
height: o.homeHeight
});
else {
if (!o.includeMargins) {
ml = getInt($selected.css("marginLeft"));
mt = getInt($selected.css("marginTop"));
}
$back.css({
left: $selected.position().left + ml,
top: $selected.position().top + mt,
width: $selected.outerWidth() - bx,
height: $selected.outerHeight() - by
});
}
// after we leave the container element, move back to default/last clicked element
$(this).on("mouseleave focusout", function() {
var $returnEl = null;
if (o.returnHome) $returnEl = $home;
else if (!o.autoReturn) return true;
if (o.returnDelay) {
if (delayTimer) clearTimeout(delayTimer);
delayTimer = setTimeout(function() {
move($returnEl, "return");
}, o.returnDelay);
} else {
move($returnEl, "return");
}
return true;
});
function move($el, cbType) {
if (cbType == "return") {
o.returnStart($el);
} else {
o.hoverStart($el);
}
if (!$el) $el = $selected;
if (!o.includeMargins) {
ml = getInt($el.css("marginLeft"));
mt = getInt($el.css("marginTop"));
}
var dims = {
left: $el.position().left + ml,
top: $el.position().top + mt,
width: $el.outerWidth() - bx,
height: $el.outerHeight() - by
};
$back.stop().animate(dims, o.speed, o.fx, function() {
if (cbType == "return") {
o.returnFinish($el);
} else {
o.hoverFinish($el);
}
});
}
});
};
})(jQuery);