-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickQuery.js
278 lines (229 loc) · 8.12 KB
/
quickQuery.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
REQUIRES jQUERY
INCLUDE jQUERY BEFORE THIS FILE IN <HEAD>
"element" parameter: an HTML element (e.g. "#myId" or "h1")
"speed" parameter: "slow", "normal", "fast", 1000, 2000, etc. (in milliseconds)
*/
// clicking an element will initiate scroll to top of page
// style: if false, will not reformat CSS cursor
function scrollToTop(element, speed, style) {
if (style != false)
$(element).css("cursor", "pointer");
$(element).on("click", function() {
$("html, body").animate({scrollTop: 0}, speed);
});
};
// clicking on start element will initiate scroll to destination element
// offset: +/- to move destination position down/up
// style: if false, will not reformat CSS cursor
function scrollTo(start, destination, speed, offset, style) {
var end = $(destination);
var position = 0;
if (offset)
position = offset;
if (style != false)
$(start).css("cursor", "pointer");
$(start).on("click", function() {
$("html, body").animate({scrollTop: end.offset().top + position}, speed);
});
};
// fade in an element (e.g on page load)
// opacity: 0 to 1
function appear(element, speed, opacity) {
var finalOpacity = 1;
if (opacity)
finalOpacity = opacity;
$(element).css('opacity', '0');
$(element).fadeTo(speed, finalOpacity);
}
// fade in an element (e.g on page load) after a certain period of time
// opacity: 0 to 1
// time: in milliseconds
function timedAppear(element, speed, opacity, time) {
$(element).css('opacity', '0');
setTimeout(function() {
appear(element, speed, opacity);
}, time);
}
// fade in an element (e.g on page load) ONLY when you scroll to it
// works well with imgs
// opacity: 0 to 1
function lazyAppear(element, speed, opacity) {
var appearFlag = 0;
$(element).css('opacity', '0');
$(window).scroll(function() {
if (appearFlag == 0) {
var elemOffset = $(element).offset().top + $(element).outerHeight();
var windowOffset = $(this).scrollTop() + $(this).height();
if (windowOffset > elemOffset){
appear(element, speed, opacity);
appearFlag = 1;
}
}
return false;
});
}
// fade out header as you scroll down
// fade in header as you scroll up
function ghostHeader(element) {
$(window).scroll(function(){
var scrollDistance = $(this).scrollTop();
var bottomOfElement = $(element).offset().top + $(element).outerHeight();
var opacity = 1 - (scrollDistance/(bottomOfElement))
$(element).css("opacity", opacity);
});
}
// fade out footer as you scroll up
// fade in footer as you scroll down
function ghostFooter(element) {
$(window).scroll(function(){
var scrollDistance = $(this).height() + $(this).scrollTop();
var startOfElement = $(element).offset().top;
var bottomOfElement = $(element).offset().top + $(element).outerHeight();
var opacity = (scrollDistance - startOfElement)/(bottomOfElement - startOfElement);
$(element).css("opacity", opacity);
});
}
// change the CSS property of elementTwo by hovering over elementOne
// elementOne and elementTwo can be the same
// cssProperty: a string with a CSS property
// cssValueOne: a string with a CSS value (will show on mouseenter)
// cssValueTwo: a string with a CSS value (will show on mouseleave)
// transition: "slow", "normal", "fast", 1000, 2000, etc.
// EXAMPLE: cssHover("#firstElem", "#secondElem", "color", "blue", "black", 1000);
function cssHover(elementOne, elementTwo, cssProperty, cssValueOne, cssValueTwo, transition) {
if (transition) {
if (transition == "fast") {
transition = 200;
}
else if (transition == "normal") {
transition = 400;
}
else if (transition == "slow") {
transition = 600;
}
$(elementTwo).css({
transition : cssProperty + ' ' + transition + "ms " + "ease-in-out",
WebkitTransition : cssProperty + ' ' + transition + "ms " + "ease-in-out",
MozTransition : cssProperty + ' ' + transition + "ms " + "ease-in-out",
MsTransition : cssProperty + ' ' + transition + "ms " + "ease-in-out",
OTransition : cssProperty + ' ' + transition + "ms " + "ease-in-out",
});
}
$(elementOne).hover(
function() {
$(elementTwo).css(cssProperty, cssValueOne);
}, function() {
$(elementTwo).css(cssProperty, cssValueTwo);
}
);
}
// returns true if mobile device
function isMobile() {
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent))
return true;
else
return false;
}
// load an image as a full-screen background image (w/o choppiness)
// path: path of image
function backgroundImg(path) {
var img = new Image();
img.src = path;
img.onload = function() {
$('body').css({'background': 'url(' + path + ')' + 'no-repeat 50% 50% fixed', 'background-size': 'cover'});
};
}
// give element shadow on hover
// transition: time to display/fade shadow (optional)
// depth: strength of shadow (optional)
// color: shade of shadow (optional)
function hoverShadow(elem, transition, depth, color) {
shadowOne = shadowTwo = 10;
shadowThree = 5;
shadowColor = '#888';
shadowTransition = 500;
if (depth) {
shadowOne *= depth;
shadowTwo *= depth;
shadowThree *= depth;
}
if (color)
shadowColor = color;
if (transition || transition == 0)
shadowTransition = transition;
shadowString = shadowOne + 'px ' + shadowTwo + 'px ' + shadowThree + 'px ';
cssHover(elem, elem, 'box-shadow', shadowString + ' ' + shadowColor, '', shadowTransition);
}
// change element opacity on hover
// valueOne: opacity on mouseIn: 0 - 1 (optional)
// valueTwo: opacity on mouseOut: 0 - 1 (optional)
// transition: time to change (optional)
function hoverOpacity(elem, valueOne, valueTwo, transition){
mouseIn = 1;
mouseOut = .7;
opacTransition = 0;
if (valueOne)
mouseIn = valueOne;
if (valueTwo)
mouseOut = valueTwo;
if (transition || transition == 0)
opacTransition = transition;
cssHover(elem, elem, 'opacity', mouseOut, mouseIn, opacTransition);
}
// zoom element on hover
// scale: magnitude of zoom (optional)
// transition: time to change (optional)
function hoverScale(elem, scale, transition) {
scaleSize = 1.2;
scaleTransition = 500;
if (scale)
scaleSize = scale;
if(transition || transition == 0)
scaleTransition = transition;
cssArgOne = 'scale('+scaleSize+')';
cssArgTwo = 'scale(1)';
cssHover(elem, elem, 'transform', cssArgOne, cssArgTwo, scaleTransition);
}
// cycle text of element
// textArray: list of strings to cycle
// time: cycle time (optional)
function cycleText(elem, textArray, time) {
var cycleTime = 1000;
if (time || time == 0)
cycleTime = time;
var index = -1;
var interval = setInterval(function() {
index++;
if (index >= textArray.length)
index = 0;
$(elem).html(textArray[index]);
}, cycleTime);
}
// cycle background image of element
// picArray: list of image urls to cycle
// time: cycle time (optional)
// fade: fade time (optional)
function cyclePic(elem, picArray, time, fade) {
var cycleTime = 5000;
var fadeTime = 1000;
if (time || time == 0)
cycleTime = time;
if (fade || fade == 0)
fadeTime = fade;
var picProperties = [];
picArray.forEach(function(path) {
property = 'url(' + path + ')' + 'no-repeat 50% 50%';
picProperties.push(property);
});
var index = -1;
var interval = setInterval(function() {
index++;
if (index >= picArray.length)
index = 0;
$(elem).fadeOut(fadeTime, function() {
$(this).css('background', picProperties[index]);
});
$(elem).fadeIn(fadeTime);
}, cycleTime);
}