/*! jqthumb v1.9.3 copyright (c) 2013-2014 dual licensed under the mit and gpl licenses. author : pak cheong version : 1.9.3 repo : https://github.com/pakcheong/jqthumb demo : http://pakcheong.github.io/jqthumb/ last updated : thursday, july 10th, 2014, 4:32:44 pm requirements : jquery >=v1.3.0 or zepto (with zepto-data plugin) >=v1.0.0 */ ;(function ( $, window, document, undefined ) { var pluginname = "jqthumb", grandglobal = { outputelems: [], inputelems: [] }, defaults = { classname : 'jqthumb', width : 100, height : 100, position : { top: '50%', left: '50%' }, source : 'src', showoncomplete : true, before : function(){}, after : function(){}, done : function(){} }; function plugin ( element, options ) {// the actual plugin constructor this.element = element; this.settings = $.extend( {}, defaults, options ); this.settings.width = this.settings.width.tostring().replace(/px/g, ''); this.settings.height = this.settings.height.tostring().replace(/px/g, ''); this.settings.position.top = this.settings.position.top.tostring().replace(/px/g, ''); this.settings.position.left = this.settings.position.left.tostring().replace(/px/g, ''); this._defaults = defaults; this._name = pluginname; if(typeof options == 'string'){ if(options.tolowercase() == 'kill'){ this.kill(this.element); } }else{ this.init(); } } plugin.prototype = { init: function () { if(this.support_css3_attr('backgroundsize') === false){ // old browsers need to do calculation to perform same output like "background-size: cover" this.noncss3supported_method(this.element, this.settings); }else{ // modern browsers that support css3 would be easier this.css3supported_method(this.element, this.settings); } }, kill: function(_this){ if( $(_this).data(pluginname)){ if($(_this).prev().data(pluginname) !== pluginname){ console.error('we could not find the element created by jqthumb. it is probably due to one or more element has been added right before the image element after the plugin initialization, or it was removed.'); return false; } /* start: remove output elements */ var temparr = []; $.each(grandglobal.outputelems, function(index, obj){ if($(obj)[0] == $(_this).prev()[0]){ }else{ temparr.push(grandglobal.outputelems[index]); } }); grandglobal.outputelems = temparr; /* end: remove output elements */ /* start: remove input elements */ temparr = []; $.each(grandglobal.inputelems, function(index, obj){ if($(obj)[0] == $(_this)[0]){ }else{ temparr.push(grandglobal.inputelems[index]); } }); grandglobal.inputelems = temparr; /* end: remove input elements */ $(_this).prev().remove(); $(_this).removeattr('style'); // first, remove all the styles first if(typeof $(_this).data(pluginname + '-original-styles') !== 'undefined'){ $(_this).attr('style', $(_this).data(pluginname + '-original-styles')); // then re-store the original styles } if(typeof $(_this).data(pluginname + '-original-styles') !== 'undefined'){ $(_this).removedata(pluginname + '-original-styles'); // remove data that stores the original stylings before the image being rendered } if(typeof $(_this).data(pluginname) !== 'undefined'){ $(_this).removedata(pluginname); // remove data that stored during plugin initialization } } }, noncss3supported_method: function(_this, options){ options.before.call(_this, _this); var that = this, $this = $(_this); $this.data(pluginname + '-original-styles', $this.attr('style')); // keep original styles into data $this.hide(); var $tempimg = $(""); $tempimg.bind('load', function(){ var newimg = { obj: $tempimg, size: { width: this.width, height: this.height } }, pw = that.percentorpixel(options.width), ph = that.percentorpixel(options.height), imgcontainer = $('
'), ratio = 0; $(imgcontainer) .insertbefore($this) .append($(newimg.obj)) .css({ 'position' : 'relative', 'overflow' : 'hidden', 'width' : (pw == '%') ? options.width : options.width + 'px', 'height' : (ph == '%') ? options.height : options.height + 'px' }) .data(pluginname, pluginname); // it would be easy to kill later if(newimg.size.width > newimg.size.height){ // horizontal $(newimg.obj).css({ 'width' : 'auto', 'max-height' : 99999999, 'min-height' : 0, 'max-width' : 99999999, 'min-width' : 0, 'height' : $(newimg.obj).parent().height() + 'px' }); ratio = $(newimg.obj).height() / $(newimg.obj).width(); // get ratio if( $(newimg.obj).width() < $(newimg.obj).parent().width() ){ $(newimg.obj).css({ 'width': $(newimg.obj).parent().width(), 'height': parsefloat($(newimg.obj).parent().width() * ratio) }); } }else{ // vertical $(newimg.obj).css({ 'width' : $(newimg.obj).parent().width() + 'px', 'max-height' : 99999999, 'min-height' : 0, 'max-width' : 99999999, 'min-width' : 0, 'height' : 'auto' }); ratio = $(newimg.obj).width() / $(newimg.obj).height(); // get ratio if( $(newimg.obj).height() < $(newimg.obj).parent().height() ){ $(newimg.obj).css({ 'width': parsefloat($(newimg.obj).parent().height() * ratio), 'height': $(newimg.obj).parent().height() }); } } postop = (that.percentorpixel(options.position.top) == '%') ? options.position.top : options.position.top + 'px'; posleft = (that.percentorpixel(options.position.left) == '%') ? options.position.left : options.position.left + 'px'; $(newimg.obj).css({ 'position' : 'absolute', 'top' : postop, 'margin-top' : (function(){ if(that.percentorpixel(options.position.top) == '%'){ return '-' + parsefloat(($(newimg.obj).height() / 100) * options.position.top.slice(0,-1)) + 'px'; } })(), 'left' : posleft, 'margin-left' : (function(){ if(that.percentorpixel(options.position.left) == '%'){ return '-' + parsefloat(($(newimg.obj).width() / 100) * options.position.left.slice(0,-1)) + 'px'; } })() }); $(imgcontainer) .hide() .addclass(options.classname); if(options.showoncomplete === true){ $(imgcontainer).show(); } options.after.call(_this, $(imgcontainer)); that.updateglobal(_this, $(imgcontainer), options); }).attr("src", $this.attr(options.source)); // for older browsers, must bind events first then set attr later (ie7, ie8) }, css3supported_method: function (_this, options) { options.before.call(_this, _this); var that = this, $oriimage = $(_this), $tempimg = $('').attr('src', $oriimage.attr(options.source)); $oriimage.data(pluginname + '-original-styles', $oriimage.attr('style')); // keep original styles into data $oriimage.hide(); $.each($tempimg, function(index, obj){ var $tempimg = $(obj); $tempimg.one('load', function() { var pw = that.percentorpixel(options.width), ph = that.percentorpixel(options.height), featuredbgimgcontainer = null, featuredbgimg = null; featuredbgimgcontainer = $('
') .css({ 'width' : (pw == '%') ? options.width : options.width + 'px', 'height' : (ph == '%') ? options.height : options.height + 'px', 'display' : 'none' }) .addclass(options.classname) .data(pluginname, pluginname); // it would be easy to kill later featuredbgimg = $('
').css({ 'width' : '100%', 'height' : '100%', 'background-image' : 'url("' + $oriimage.attr(options.source) + '")', // '-ms-filter' : '"progid:dximagetransform.microsoft.alphaimageloader(src="' + $oriimage.attr(options.source) + '",sizingmethod="scale")', // this does not work in zepto 'background-repeat' : 'no-repeat', 'background-position': (function(){ var postop = (that.percentorpixel(options.position.top) == '%') ? options.position.top : options.position.top + 'px', posleft = (that.percentorpixel(options.position.left) == '%') ? options.position.left : options.position.left + 'px'; return postop + ' ' + posleft; })(), 'background-size' : 'cover' }) .appendto($(featuredbgimgcontainer)); $(featuredbgimgcontainer).insertbefore($(_this)); if(options.showoncomplete === true){ $(featuredbgimgcontainer).show(); } that.checksrcattrname(_this, options); options.after.call(_this, $(featuredbgimgcontainer)); that.updateglobal(_this, $(featuredbgimgcontainer), options); }); }); }, updateglobal: function(_this, obj, options){ _this.global.outputelems.push( $(obj)[0] ); _this.global.elemcounter++; grandglobal.outputelems.push( $(obj)[0] ); if(_this.global.elemcounter == _this.global.inputelems.length){ options.done.call(_this, _this.global.outputelems); } }, checksrcattrname: function(_this, options){ if( options.source != 'src' && ( typeof $(_this).attr('src') === 'undefined' || $(_this).attr('src') === '' ) ) { $(_this).attr('src', $(_this).attr(options.source)); } }, percentorpixel: function(str){ str = str.tostring(); if(str.match("px$") || str.match("px$") || str.match("px$") || str.match("px$")) { return 'px'; }else if(str.match("%$")) { return '%'; } }, support_css3_attr: (function() { /* code available at http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-detect-css-support-in-browsers-with-javascript/ */ var div = document.createelement('div'), vendors = 'khtml ms o moz webkit'.split(' '), len = vendors.length; return function(prop) { if ( prop in div.style ) return true; prop = prop.replace(/^[a-z]/, function(val) { return val.touppercase(); }); for(i=0; i