iepngfix_tilebg.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
  2. // (c) 2008-2009 Angus Turnbull http://www.twinhelix.com
  3. // This is licensed under the GNU LGPL, version 2.1 or later.
  4. // For details, see: http://creativecommons.org/licenses/LGPL/2.1/
  5. var IEPNGFix = window.IEPNGFix || {};
  6. IEPNGFix.tileBG = function(elm, pngSrc, ready) {
  7. // Params: A reference to a DOM element, the PNG src file pathname, and a
  8. // hidden "ready-to-run" passed when called back after image preloading.
  9. var data = this.data[elm.uniqueID],
  10. elmW = Math.max(elm.clientWidth, elm.scrollWidth),
  11. elmH = Math.max(elm.clientHeight, elm.scrollHeight),
  12. bgX = elm.currentStyle.backgroundPositionX,
  13. bgY = elm.currentStyle.backgroundPositionY,
  14. bgR = elm.currentStyle.backgroundRepeat;
  15. // Cache of DIVs created per element, and image preloader/data.
  16. if (!data.tiles) {
  17. data.tiles = {
  18. elm: elm,
  19. src: '',
  20. cache: [],
  21. img: new Image(),
  22. old: {}
  23. };
  24. }
  25. var tiles = data.tiles,
  26. pngW = tiles.img.width,
  27. pngH = tiles.img.height;
  28. if (pngSrc) {
  29. if (!ready && pngSrc != tiles.src) {
  30. // New image? Preload it with a callback to detect dimensions.
  31. tiles.img.onload = function() {
  32. this.onload = null;
  33. IEPNGFix.tileBG(elm, pngSrc, 1);
  34. };
  35. return tiles.img.src = pngSrc;
  36. }
  37. } else {
  38. // No image?
  39. if (tiles.src) ready = 1;
  40. pngW = pngH = 0;
  41. }
  42. tiles.src = pngSrc;
  43. if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
  44. bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
  45. return;
  46. }
  47. // Convert English and percentage positions to pixels.
  48. var pos = {
  49. top: '0%',
  50. left: '0%',
  51. center: '50%',
  52. bottom: '100%',
  53. right: '100%'
  54. },
  55. x,
  56. y,
  57. pc;
  58. x = pos[bgX] || bgX;
  59. y = pos[bgY] || bgY;
  60. if (pc = x.match(/(\d+)%/)) {
  61. x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
  62. }
  63. if (pc = y.match(/(\d+)%/)) {
  64. y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
  65. }
  66. x = parseInt(x);
  67. y = parseInt(y);
  68. // Handle backgroundRepeat.
  69. var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
  70. repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
  71. if (repeatX) {
  72. x %= pngW;
  73. if (x > 0) x -= pngW;
  74. }
  75. if (repeatY) {
  76. y %= pngH;
  77. if (y > 0) y -= pngH;
  78. }
  79. // Go!
  80. this.hook.enabled = 0;
  81. if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
  82. elm.style.position = 'relative';
  83. }
  84. var count = 0,
  85. xPos,
  86. maxX = repeatX ? elmW : x + 0.1,
  87. yPos,
  88. maxY = repeatY ? elmH : y + 0.1,
  89. d,
  90. s,
  91. isNew;
  92. if (pngW && pngH) {
  93. for (xPos = x; xPos < maxX; xPos += pngW) {
  94. for (yPos = y; yPos < maxY; yPos += pngH) {
  95. isNew = 0;
  96. if (!tiles.cache[count]) {
  97. tiles.cache[count] = document.createElement('div');
  98. isNew = 1;
  99. }
  100. var clipR = Math.max(0, xPos + pngW > elmW ? elmW - xPos : pngW),
  101. clipB = Math.max(0, yPos + pngH > elmH ? elmH - yPos : pngH);
  102. d = tiles.cache[count];
  103. s = d.style;
  104. s.behavior = 'none';
  105. s.left = (xPos - parseInt(elm.currentStyle.paddingLeft)) + 'px';
  106. s.top = yPos + 'px';
  107. s.width = clipR + 'px';
  108. s.height = clipB + 'px';
  109. s.clip = 'rect(' +
  110. (yPos < 0 ? 0 - yPos : 0) + 'px,' +
  111. clipR + 'px,' +
  112. clipB + 'px,' +
  113. (xPos < 0 ? 0 - xPos : 0) + 'px)';
  114. s.display = 'block';
  115. if (isNew) {
  116. s.position = 'absolute';
  117. s.zIndex = -999;
  118. if (elm.firstChild) {
  119. elm.insertBefore(d, elm.firstChild);
  120. } else {
  121. elm.appendChild(d);
  122. }
  123. }
  124. this.fix(d, pngSrc, 0);
  125. count++;
  126. }
  127. }
  128. }
  129. while (count < tiles.cache.length) {
  130. this.fix(tiles.cache[count], '', 0);
  131. tiles.cache[count++].style.display = 'none';
  132. }
  133. this.hook.enabled = 1;
  134. // Cache so updates are infrequent.
  135. tiles.old = {
  136. w: elmW,
  137. h: elmH,
  138. x: bgX,
  139. y: bgY,
  140. r: bgR
  141. };
  142. };
  143. IEPNGFix.update = function() {
  144. // Update all PNG backgrounds.
  145. for (var i in IEPNGFix.data) {
  146. var t = IEPNGFix.data[i].tiles;
  147. if (t && t.elm && t.src) {
  148. IEPNGFix.tileBG(t.elm, t.src);
  149. }
  150. }
  151. };
  152. IEPNGFix.update.timer = 0;
  153. if (window.attachEvent && !window.opera) {
  154. window.attachEvent('onresize', function() {
  155. clearTimeout(IEPNGFix.update.timer);
  156. IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
  157. });
  158. }