-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjquery.cover.js
50 lines (39 loc) · 1.21 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
/*! Cover v1.2.0 | MIT License | github.com/antontrollback/cover */
;(function($) {
var pluginName = "cover";
function Cover (element, options) {
this.element = $(element);
$.extend(this, {
container: this.element.parent(),
ratio: this.element.height() / this.element.width()
}, options);
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);