-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.slotload.js
78 lines (67 loc) · 2.47 KB
/
jquery.slotload.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
/*
* jQuery SlotLoad
*
* Copyright (c) 2011-2012 Navkaran Garcha <nav_garcha@hotmail.co.uk>
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Version: 0.1
* Dependencies: jQuery
*
*/
(function($) {
$.fn.slotload = function(options) {
var settings = $.extend({}, $.fn.slotload.settings, options),
showDelay = Math.floor(settings.baseDelay + (Math.random() * 500)),
elmWrapper = '<div class="slotload-img-wrapper '+ settings.loadingClass +'" style="display:inline-block; overflow:hidden;" />';
this.find(settings.selector +'[data-load]').each(function() {
var img = $(this).wrap(elmWrapper),
imgSrc = img.data('load');
$('<img />').load(function() {
var loadedImg = $(this).hide().insertAfter(img),
imgHeight = loadedImg.height();
// Only required to get accurate height
loadedImg.remove();
// Setup img elm for slot effect
img.attr('src', imgSrc).css({
position: 'relative',
top: -(imgHeight + 50)
});
setTimeout(function() {
img.animate({
top: 0
}, {
duration: settings.speed,
easing: 'elasticOut',
complete: settings.onComplete
}).parent().removeClass(settings.loadingClass);
}, showDelay);
}).error(function() {
img.parent().removeClass(settings.loadingClass);
}).attr('src', imgSrc);
});
// Ensure plugin is chainable
return this;
};
// Public settings
$.fn.slotload.settings = {
selector: 'img',
loadingClass: 'loading',
speed: 800,
baseDelay: 500,
onComplete: function() {}
};
// Setup the required easing animation
$.easing.elasticOut = function(x, t, b, c, d) {
var s = 1.70158,
p = 0,
a = c;
if(t === 0) return b;
if((t/=d) == 1) return b + c;
if(!p) p = d * 0.3;
if(a < Math.abs(c)) { a = c; s = p/4; }
else s = p/(2*Math.PI) * Math.asin (c/a);
return a * Math.pow(2,-10*t) * Math.sin((t*d-s) * (2*Math.PI)/p) + c + b;
};
})(jQuery);