1
0

jquery.pngFix.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * jQuery (PNG Fix) v1.2
  3. * Microsoft Internet Explorer 24bit PNG Fix
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2007 Paul Campbell (pauljamescampbell.co.uk)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. * @param Object
  28. * @return Array
  29. */
  30. (function($) {
  31. $.fn.pngfix = function(options) {
  32. // Review the Microsoft IE developer library for AlphaImageLoader reference
  33. // http://msdn2.microsoft.com/en-us/library/ms532969(VS.85).aspx
  34. // ECMA scope fix
  35. var elements = this;
  36. var settings = $.extend({
  37. imageFixSrc: false,
  38. sizingMethod: false
  39. }, options);
  40. if(!$.browser.msie || ($.browser.msie && $.browser.version >= 7)) {
  41. return(elements);
  42. }
  43. function setFilter(el, path, mode) {
  44. var fs = el.attr("filters");
  45. var alpha = "DXImageTransform.Microsoft.AlphaImageLoader";
  46. if (fs[alpha]) {
  47. fs[alpha].enabled = true;
  48. fs[alpha].src = path;
  49. fs[alpha].sizingMethod = mode;
  50. } else {
  51. el.css("filter", 'progid:' + alpha + '(enabled="true", sizingMethod="' + mode + '", src="' + path + '")');
  52. }
  53. }
  54. function setDOMElementWidth(el) {
  55. if(el.css("width") == "auto" & el.css("height") == "auto") {
  56. el.css("width", el.attr("offsetWidth") + "px");
  57. }
  58. }
  59. return(
  60. elements.each(function() {
  61. // Scope
  62. var el = $(this);
  63. if(el.attr("tagName").toUpperCase() == "IMG" && (/\.png/i).test(el.attr("src"))) {
  64. if(!settings.imageFixSrc) {
  65. // Wrap the <img> in a <span> then apply style/filters,
  66. // removing the <img> tag from the final render
  67. el.wrap("<span></span>");
  68. var par = el.parent();
  69. par.css({
  70. height: el.height(),
  71. width: el.width(),
  72. display: "inline-block"
  73. });
  74. setFilter(par, el.attr("src"), "scale");
  75. el.remove();
  76. } else if((/\.gif/i).test(settings.imageFixSrc)) {
  77. // Replace the current image with a transparent GIF
  78. // and apply the filter to the background of the
  79. // <img> tag (not the preferred route)
  80. setDOMElementWidth(el);
  81. setFilter(el, el.attr("src"), "image");
  82. el.attr("src", settings.imageFixSrc);
  83. }
  84. } else {
  85. var bg = new String(el.css("backgroundImage"));
  86. var matches = bg.match(/^url\("(.*)"\)$/);
  87. if(matches && matches.length) {
  88. // Elements with a PNG as a backgroundImage have the
  89. // filter applied with a sizing method relevant to the
  90. // background repeat type
  91. setDOMElementWidth(el);
  92. el.css("backgroundImage", "none");
  93. // Restrict scaling methods to valid MSDN defintions (or one custom)
  94. var sc = "crop";
  95. if(settings.sizingMethod) {
  96. sc = settings.sizingMethod;
  97. }
  98. setFilter(el, matches[1], sc);
  99. // Fix IE peek-a-boo bug for internal links
  100. // within that DOM element
  101. el.find("a").each(function() {
  102. $(this).css("position", "relative");
  103. });
  104. }
  105. }
  106. })
  107. );
  108. }
  109. })(jQuery)