forked from AntonTrollback/cover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.cover.js
64 lines (51 loc) · 1.4 KB
/
jquery.cover.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
/*! Cover v1.2.0 | MIT License | github.com/antontrollback/cover */
/*
minor chanegs by kater louis
*/
;(function($) {
var pluginName = "cover";
function Cover (element, options) {
this.element = $(element);
var t = this;
$.extend(this, {
// Defaults
container: this.element.parent(),
ratio: this.element.height() / this.element.width(),
resize: false
}, options);
// reset on resize
if (this.resize) {
$(window).resize(function() {
t.set();
})
}
this.set();
}
Cover.prototype = {
set: function() {
var height = this.container.height();
var width = this.container.width();
var fillWidth = this.ratio >= (height / width);
if (fillWidth && !this.fillingWidth) {
this.fillingWidth = true;
this.element.css('width', '100%').css('height', 'auto');
}
if (!fillWidth) {
this.fillingWidth = false;
this.element.css('width', 'auto').css('height', '100%');
}
}
};
// Wrapper preventing multiple instantiations
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Cover(this, options));
}
// Allow access to 'set' method
if (options === 'set') {
cover = $.data(this, 'plugin_' + pluginName).set();
}
});
};
})(jQuery);