forked from ani23-11/getaway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mkinfinite.js
75 lines (74 loc) · 2.26 KB
/
jquery.mkinfinite.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
(function($){
jQuery.fn.mkinfinite = function(options){
var options = $.extend({
maxZoom: 1.25,
imagesRatio: 1.5,
animationTime: 5000,
animationInterval: 10,
isFixedBG: false,
zoomIn: true,
imagesList: new Array(),
}, options);
var currentImage = 1;
var currentZoom = 1;
var animationStep = 0.1;
var $object;
var make = function(){
currentZoom = options.zoomIn ? options.maxZoom : 1;
animationStep = (options.maxZoom - 1) / ( options.animationTime / options.animationInterval );
if (!options.zoomIn){
animationStep = -1 * animationStep;
}
$object = $(this);
$object.css({
'background-position': '50% 50%',
'background-repeat': 'no-repeat'
});
$object.addClass('mkinfinite');
if (options.imagesList.length > 1){
calculateZoom(currentImage);
}
animateBG();
};
var calculateZoom = function(setImageNumber){
var nBGw = ( options.isFixedBG ? $(window).width() : $object.width() ) * currentZoom;
var nBGh = $object.height() * currentZoom;
if ( (nBGw / nBGh) > options.imagesRatio ){
nBGw = Math.round(nBGw);
nBGh = Math.round(nBGw / options.imagesRatio);
} else {
nBGw = Math.round(nBGh * options.imagesRatio);
nBGh = Math.round(nBGh);
}
if (setImageNumber && (setImageNumber > 0)){
$object.css({
'background-size': nBGw + 'px ' + nBGh + 'px',
'background-image': 'url(' + options.imagesList[setImageNumber - 1] + ')'
});
var imgLoader = new Image();
imgLoader.src = options.imagesList[setImageNumber % options.imagesList.length];
} else {
$object.css('background-size', nBGw + 'px ' + nBGh + 'px');
}
}
var animateBG = function(){
if ( options.zoomIn && (currentZoom >= 1) || !options.zoomIn && (currentZoom <= options.maxZoom) ){
setTimeout(function(){
calculateZoom();
currentZoom = currentZoom - animationStep;
animateBG();
}, options.animationInterval);
} else {
currentZoom = options.zoomIn ? options.maxZoom : 1;
if (options.imagesList.length > 1){
currentImage = currentImage % options.imagesList.length + 1;
calculateZoom(currentImage);
} else {
calculateZoom();
}
animateBG();
}
}
return this.each(make);
};
})(jQuery);