-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.showImg.min.js
17 lines (17 loc) · 1.29 KB
/
jquery.showImg.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 图片展示(大小不定情况)
* @param type 展示类型[fill(填满-溢出隐藏) | full(完整显示-多余部分留白)]
* @auther liutaowei [email protected] @date 2015-06-06
* @version 1.0.4
*
* --------------- 使用方法 ---------------
* -----DOM-----
* <div><img src=img.jpg /></div>
*
* -----JS-----
* $("div").showImg({
* "type":"fill(填满-溢出隐藏) | full(完整显示-多余部分留白)",
* });
*
**/
(function($){$.fn.showImg=function(k){var l={type:"fill",};var m=$.extend(l,k);var n=m.type.toLowerCase();$(this).find("img").parent().css("overflow","hidden");$(this).find("img").load(function(){$(this).each(function(){var a=$(this);var b=a.parent();var c=b.width();var d=b.height();var e=a.width();var f=a.height();$(this).css("position","relative");if(n=="fill"){if(e/f>=c/d){var g=d;var h=g*e/f;var i=(c-h)*0.5;$(this).css({"height":g,"left":i,"top":"0px","width":"auto",})}else{var h=c;var g=h*f/e;var j=(d-g)*0.5;$(this).css({"width":h,"top":j,"left":"0px","height":"auto",})}}else{if(e/f>=c/d){var h=c;var g=h*f/e;var j=(d-g)*0.5;$(this).css({"width":h,"top":j,"left":"0px","height":"auto",})}else{var g=d;var h=g*e/f;var i=(c-h)*0.5;$(this).css({"height":g,"left":i,"top":"0px","width":"auto",})}}})}).each(function(){if(this.complete)$(this).load()})}})(jQuery);