1/* 2 * CSS Background-Size jQuery Plugin v.1.0 3 * 4 * By Aram Kocharyan, 2012. 5 * Released under the MIT licence. 6 * http://aramk.com/ 7 */ 8 9(function($) { 10 11 $.bgdSize = function(el, options) { 12 var base = this; 13 14 base.defaults = { 15 mode : '', // blank will do nothing, use 'cover' or 'contain' 16 repeatCount : 0, 17 repeatMax : 10, 18 repeatFreq : 100 19 }; 20 21 base.$el = $(el); 22 base.el = el; 23 24 base.init = function() { 25 if (typeof options == 'string') { 26 base.options = $.extend(base.defaults, { 27 mode : options 28 }); 29 } else { 30 base.options = $.extend(base.defaults, options); 31 } 32 base.repeat(); 33 }; 34 35 base.repeat = function() { 36 base.options.repeatCount++; 37 if (base.options.repeatCount > base.options.repeatMax) { 38 return; 39 } 40 base.scale(); 41 if (base.done == false) { 42 base.done = true; 43 setTimeout(function() { 44 base.repeat(); 45 }, base.options.repeatFreq); 46 } 47 }; 48 49 base.scale = function() { 50 var mode; 51 if (base.options.mode == 'cover') { 52 mode = Math.max; 53 } else if (base.options.mode == 'contain') { 54 mode = Math.min; 55 } else { 56 return; 57 } 58 59 var parent = base.$el.parent(); 60 if (!parent) { 61 return; 62 } 63 parent.css('overflow', 'hidden'); 64 var width = base.$el.width(); 65 var height = base.$el.height(); 66 if (width == 0 || height == 0) { 67 base.done = false; 68 return; 69 } 70 var parentWidth = parent.width(); 71 var parentHeight = parent.height(); 72 73 var widthRatio = parentWidth / width; 74 var heightRatio = parentHeight / height; 75 76 var ratio = mode(widthRatio, heightRatio); 77 78 var newWidth = ratio * width; 79 var newHeight = ratio * height; 80 base.$el.width(newWidth); 81 base.$el.height(newHeight); 82 83 base.$el.css('margin-left', (parentWidth - newWidth) / 2); 84 base.$el.css('margin-top', (parentHeight - newHeight) / 2); 85 }; 86 87 base.init(); 88 }; 89 90 $.fn.bgdSize = function(options) { 91 return this.each(function() { 92 (new $.bgdSize(this, options)); 93 }); 94 }; 95 96})(jQuery); 97
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.