1/* 2* imgCentering.js jQuery Plugin 3* A jQuery plugin to centering your pictures and images 4* 5* Name: imgCentering.js 6* Author: Kenny Ooi - http://www.inwebson.com 7* Date: September 28, 2012 8* Version: 1.1 9* Example: http://www.inwebson.com/demo/imgcentering/ 10* 11*/ 12(function($){ 13 var blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="; 14 15 var imgCentering = function(obj, options) { 16 var defaults = { 17 'forceWidth': false, 18 'forceHeight': false, 19 'forceSmart': false, 20 'bgColor': "inherit" 21 }; 22 23 function forceWidth(obj) { 24 obj.css('width', '100%'); 25 } 26 27 function forceHeight(obj) { 28 obj.css('height', '100%'); 29 } 30 31 var img = $(obj); 32 var settings = $.extend(defaults, options); 33 img.load( function() { 34 35 //get container width and height 36 var _conwidth = img.parent().width(); 37 var _conheight = img.parent().height(); 38 39 //get container position and bgcolr css 40 var _parentpos = img.parent().css('position'); 41 if( settings.bgColor == 'inherit' ) 42 var _parentbg = img.parent().css('backgroundColor'); 43 else 44 var _parentbg = settings.bgColor; 45 46 //reset img to full dimension 47 img.css('width', 'auto'); 48 img.css('height', 'auto'); 49 50 if( settings.forceSmart ) { 51 var _fullratio = img.width() / img.height(); 52 var _conratio = _conwidth / _conheight; 53 if( _fullratio < _conratio ) 54 forceWidth(img); 55 else 56 forceHeight(img); 57 } else { 58 if( settings.forceWidth ) 59 forceWidth(img); 60 if( settings.forceHeight ) 61 forceHeight(img); 62 } 63 64 //get final image dimension 65 var _finalwidth = img.width(); 66 var _finalheight = img.height(); 67 68 img.css({ 69 'position': 'relative', 70 'left': -(_finalwidth-_conwidth)/2+'px', 71 'top': -(_finalheight-_conheight)/2+'px' 72 }) 73 .parent().css({ 74 'position': _parentpos, 75 'overflow': 'hidden', 76 'backgroundColor': _parentbg 77 }); 78 }); 79 80 if( obj.complete || obj.complete === undefined ) { 81 var src = obj.src; 82 // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f 83 // data uri bypasses webkit log warning (thx doug jones) 84 obj.src = blank; 85 obj.src = src; 86 } 87 } 88 89 $.fn.imgCentering = function(options) { 90 91 return this.each( function(e) { 92 var img = $(this); 93 if(img.data('imgCentering')) return; 94 95 var imgcenter = new imgCentering(this, options); 96 img.data('imgCentering', imgcenter); 97 98 }); 99 } 100 101})(jQuery);
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.