; (function($, window, document, undefined) { $.fn.bsPictureAlign = function(options) { var num = this.length, loadnum = 0; for (i = 0; i < this.length; i++) { var _this = this[i]; var bsPictureAlign = new BsPictureAlign(_this, options); if (options.style == "full") bsPictureAlign.full(); if (options.style == "fill") bsPictureAlign.fill({ callback: function() { loadnum++; if (num == loadnum && options.loadover) { options.loadover(); } } }); } }; var BsPictureAlign = function(ele, opt) { this.$ele = ele; this.defaults = { "width": "100%", "height": "300px", "background": "#000" }; this.options = $.extend({}, this.defaults, opt); }; BsPictureAlign.prototype = { fill: function(opc) { var that = this, _options = that.options, _ele = $(this.$ele), _dom = _options.img_dom, _img = _dom ? _ele.find(_dom) : _ele.find("img"), _this, _width, _height, _ratio, _w, _h, _r, _wider; _ele.css({ "width": _options.width, "height": _options.height, "overflow": "hidden", "background": _options.background, "position": "relative" }); _width = _ele.width(); _height = _ele.height(); _ratio = _width / _height; _img.each(function() { _this = $(this); _this.css({ 'opacity': 0, }); _this.load(function() { _w = _this.width(); _h = _this.height(); _r = _w / _h; _wider = _r > _ratio ? true : false; if (_wider) { _this.css({ "width": "100%", "height": "auto" }); var _top = (_height - _this.height()) / 2; _this.css({ "position": "absolute", "top": _top + "px", "left": 0 }); } else { _this.css({ "width": "auto", "height": "100%" }); var _left = (_width - _this.width()) / 2; _this.css({ "position": "absolute", "top": 0, "left": _left + "px" }); } _this.css({ 'opacity': 1, }); if (opc.callback) opc.callback(); }); _this.error(function() { if (opc.callback) opc.callback(); _this.css({ 'opacity': 1, }); }); }); return this; }, full: function() { var that = this, _options = that.options, _ele = $(this.$ele), _img = _ele.find("img"), _this, _width, _height, _ratio, _w, _h, _r, _wider; _ele.css({ "width": _options.width, "height": _options.height, "overflow": "hidden", "background": "#000", "position": "relative" }); _width = _ele.width(); _height = _ele.height(); _ratio = _width / _height; _img.each(function() { _this = $(this); _this.load(function() { _w = _this.width(); _h = _this.height(); _r = _w / _h; _wider = _r > _ratio ? true : false; if (_wider) { _this.css({ "width": "auto", "height": "100%" }); var _left = (_width - _this.width()) / 2; _this.css({ "position": "absolute", "top": 0, "left": _left + "px" }); } else { _this.css({ "width": "100%", "height": "auto" }); var _top = (_height - _this.height()) / 2; _this.css({ "position": "absolute", "top": _top + "px", "left": 0 }); } }); }); return this; }, } })(jQuery, window, document);
|