Posted By

dobata on 09/27/09


Tagged

js png textmate


Versions (?)

jquerry pngfix


 / Published in: Other
 

  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.  
  32. $.fn.pngfix = function(options) {
  33.  
  34. // Review the Microsoft IE developer library for AlphaImageLoader reference
  35. // http://msdn2.microsoft.com/en-us/library/ms532969(VS.85).aspx
  36.  
  37. // ECMA scope fix
  38. var elements = this;
  39. var settings = $.extend({
  40. imageFixSrc: false,
  41. sizingMethod: false
  42. }, options);
  43.  
  44. if(!$.browser.msie || ($.browser.msie && $.browser.version >= 7)) {
  45. return(elements);
  46. }
  47.  
  48. function setFilter(el, path, mode) {
  49. var fs = el.attr("filters");
  50. var alpha = "DXImageTransform.Microsoft.AlphaImageLoader";
  51. if (fs[alpha]) {
  52. fs[alpha].enabled = true;
  53. fs[alpha].src = path;
  54. fs[alpha].sizingMethod = mode;
  55. } else {
  56. el.css("filter", 'progid:' + alpha + '(enabled="true", sizingMethod="' + mode + '", src="' + path + '")');
  57. }
  58. }
  59.  
  60. function setDOMElementWidth(el) {
  61. if(el.css("width") == "auto" & el.css("height") == "auto") {
  62. el.css("width", el.attr("offsetWidth") + "px");
  63. }
  64. }
  65.  
  66. return(
  67. elements.each(function() {
  68.  
  69. // Scope
  70. var el = $(this);
  71.  
  72. if(el.attr("tagName").toUpperCase() == "IMG" && (/.png/i).test(el.attr("src"))) {
  73. if(!settings.imageFixSrc) {
  74.  
  75. // Wrap the <img> in a <span> then apply style/filters,
  76. // removing the <img> tag from the final render
  77. el.wrap("<span></span>");
  78. var par = el.parent();
  79. par.css({
  80. height: el.height(),
  81. width: el.width(),
  82. display: "inline-block"
  83. });
  84. setFilter(par, el.attr("src"), "scale");
  85. el.remove();
  86. } else if((/.gif/i).test(settings.imageFixSrc)) {
  87.  
  88. // Replace the current image with a transparent GIF
  89. // and apply the filter to the background of the
  90. // <img> tag (not the preferred route)
  91. setDOMElementWidth(el);
  92. setFilter(el, el.attr("src"), "image");
  93. el.attr("src", settings.imageFixSrc);
  94. }
  95.  
  96. } else {
  97. var bg = new String(el.css("backgroundImage"));
  98. var matches = bg.match(/^url("(.*)")$/);
  99. if(matches && matches.length) {
  100.  
  101. // Elements with a PNG as a backgroundImage have the
  102. // filter applied with a sizing method relevant to the
  103. // background repeat type
  104. setDOMElementWidth(el);
  105. el.css("backgroundImage", "none");
  106.  
  107. // Restrict scaling methods to valid MSDN defintions (or one custom)
  108. var sc = "crop";
  109. if(settings.sizingMethod) {
  110. sc = settings.sizingMethod;
  111. }
  112. setFilter(el, matches[1], sc);
  113.  
  114. // Fix IE peek-a-boo bug for internal links
  115. // within that DOM element
  116. el.find("a").each(function() {
  117. $(this).css("position", "relative");
  118. });
  119. }
  120. }
  121.  
  122. })
  123. );
  124. }
  125.  
  126. })(jQuery)

Report this snippet  

You need to login to post a comment.