您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

113 行
3.3 KiB

  1. /**
  2. * bl-jquery-image-center jQuery Plugin
  3. *
  4. * @copyright Boxlight Media Ltd. 2012
  5. * @license MIT License
  6. * @description Centers an image by moving, cropping and filling spaces inside it's parent container. Call
  7. * this on a set of images to have them fill their parent whilst maintaining aspect ratio
  8. * @author Robert Cambridge
  9. *
  10. * Usage: See documentation at http://boxlight.github.com/bl-jquery-image-center
  11. */
  12. $.fn.centerImage = function(method, callback) {
  13. callback = callback || function() {};
  14. var els = this;
  15. var remaining = $(this).length;
  16. method = method == 'inside';
  17. // execute this on an individual image element once it's loaded
  18. var fn = function(img) {
  19. var $img = $(img);
  20. var $div = $img.parent();
  21. // parent CSS should be in stylesheet, but to reinforce:
  22. $div.css({
  23. overflow: 'hidden',
  24. });
  25. // temporarily set the image size naturally so we can get the aspect ratio
  26. $img.css({
  27. 'position': 'static',
  28. 'width': 'auto',
  29. 'height': 'auto',
  30. 'max-width': '100%',
  31. 'max-height': '100%'
  32. });
  33. // now resize
  34. var div = { w: $div.width(), h: $div.height(), r: $div.width() / $div.height() };
  35. var img = { w: $img.width(), h: $img.height(), r: $img.width() / $img.height() };
  36. var width = Math.round((div.r > img.r) ^ method ? '100%' : div.h / img.h * img.w);
  37. var height = Math.round((div.r < img.r) ^ method ? '100%' : div.w / img.w * img.h);
  38. if(width) width++;
  39. if(height) height++;
  40. $img.css({
  41. 'max-width': 'none',
  42. 'max-height': 'none',
  43. 'width': width,
  44. 'height': height,
  45. });
  46. // now center - but portrait images need to be centered slightly above halfway (33%)
  47. var div = { w: $div.width(), h: $div.height() };
  48. var img = { w: $img.width(), h: $img.height() };
  49. var left = Math.round((div.w - img.w) / 2);
  50. var top = Math.round((div.h - img.h) / 3);
  51. $img.css({
  52. 'margin-left': left,
  53. 'margin-top': top
  54. });
  55. callbackWrapped(img)
  56. };
  57. var callbackWrapped = function(img) {
  58. remaining--;
  59. callback.apply(els, [ img, remaining ]);
  60. };
  61. // iterate through elements
  62. return els.each(function(i) {
  63. if (this.complete || this.readyState === 'complete') {
  64. // loaded already? run fn
  65. // when binding, we can tell whether image loaded or not.
  66. // not if it's already failed though :(
  67. (function(el) {
  68. // use setTimeout to prevent browser locking up
  69. setTimeout(function() { fn(el) }, 10);
  70. })(this);
  71. } else {
  72. // not loaded? bind to load
  73. (function(el) {
  74. $(el)
  75. .one('load', function() {
  76. // use setTimeout to prevent browser locking up
  77. setTimeout(function() {
  78. fn(el);
  79. }, 10);
  80. })
  81. .one('error', function() {
  82. // the image did not load
  83. callbackWrapped(el)
  84. })
  85. .end();
  86. // IE9 won't always trigger the load event. fix it.
  87. if (navigator.userAgent.indexOf("Trident/5") >= 0) {
  88. el.src = el.src;
  89. }
  90. })(this);
  91. }
  92. });
  93. };
  94. // Alias functions which often better describe the use case
  95. $.fn.imageCenterResize = function(callback) {
  96. return $(this).centerImage('inside', callback);
  97. };
  98. $.fn.imageCropFill = function(callback) {
  99. return $(this).centerImage('outside', callback);
  100. };