supersleight.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var supersleight = function() {
  2. var root = false;
  3. var applyPositioning = true;
  4. // Path to a transparent GIF image
  5. var shim = 'x.gif';
  6. // RegExp to match above GIF image name
  7. var shim_pattern = /x\.gif$/i;
  8. var fnLoadPngs = function() {
  9. if (root) {
  10. root = document.getElementById(root);
  11. }else{
  12. root = document;
  13. }
  14. for (var i = root.all.length - 1, obj = null; (obj = root.all[i]); i--) {
  15. // background pngs
  16. if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) {
  17. bg_fnFixPng(obj);
  18. }
  19. // image elements
  20. if (obj.tagName=='IMG' && obj.src.match(/\.png$/i) !== null){
  21. el_fnFixPng(obj);
  22. }
  23. // apply position to 'active' elements
  24. if (applyPositioning && (obj.tagName=='A' || obj.tagName=='INPUT') && obj.style.position === ''){
  25. obj.style.position = 'relative';
  26. }
  27. }
  28. };
  29. var bg_fnFixPng = function(obj) {
  30. var mode = 'scale';
  31. var bg = obj.currentStyle.backgroundImage;
  32. var src = bg.substring(5,bg.length-2);
  33. if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
  34. mode = 'crop';
  35. }
  36. obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
  37. obj.style.backgroundImage = 'url('+shim+')';
  38. };
  39. var el_fnFixPng = function(img) {
  40. var src = img.src;
  41. img.style.width = img.width + "px";
  42. img.style.height = img.height + "px";
  43. img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
  44. img.src = shim;
  45. };
  46. var addLoadEvent = function(func) {
  47. var oldonload = window.onload;
  48. if (typeof window.onload != 'function') {
  49. window.onload = func;
  50. } else {
  51. window.onload = function() {
  52. if (oldonload) {
  53. oldonload();
  54. }
  55. func();
  56. };
  57. }
  58. };
  59. return {
  60. init: function() {
  61. addLoadEvent(fnLoadPngs);
  62. },
  63. limitTo: function(el) {
  64. root = el;
  65. },
  66. run: function() {
  67. fnLoadPngs();
  68. }
  69. };
  70. }();
  71. // limit to part of the page ... pass an ID to limitTo:
  72. // supersleight.limitTo('header');
  73. supersleight.init();