Alpha PNG Backgrounds in IE, another sleight update…

A couple years ago Drew McLellan created his version of Youngpup’s Sleight script for enabling PNG transparency in Internet Explorer. It's a handy little thing, it's called bgsleight.js and I found myself using it for a recent project that required a decent amount of transparency in background images throughout. The great thing about bgsleight is that you don't have to generally create a separate css file for IE6 filled with progid:DXImageTransform.Microsoft.AlphaImageLoader garbage.

Some of these background images were specifically positioned, and some were repeating (not for pattern, but just coverage of their element's area). After putting Drew's work to use, I found that the background images that were being repeated in my css, were now basically gone (or just being displayed as the very small versions of themselves that they are, just a couple of pixels large). I looked into the pngfix function, and added a check against whether or not the background image was supposed to be repeating or not.

JavaScript:
  1. function fnFixPng(obj) {
  2.     var bg  = obj.currentStyle.backgroundImage;
  3.     var src = bg.substring(5,bg.length-2);
  4.     var sizingMethod = (obj.currentStyle.backgroundRepeat == "no-repeat") ? "crop" : "scale";
  5.     obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
  6.     obj.style.backgroundImage = "url(path/to/transparent.gif)";
  7. }

One thing that I forgot to mention is that a transparent 1x1 pixel gif is required to make this script work. i changed the references to it which had previously been x.gif and blank.gif to path/to/transparent.gif... a generic reference (for you understand to replace with whatever path you'd like).

You can grab my copy of bgsleight.js here.

  • The isbottom variable is not used for anything, did you forget something?

    i have a png on my page but it doesn't appear at the bottom when using the code in your copy of bgsleight

    I can't figure out what to do with isbottom in order to make the image appear at the bottom if its meant to
  • ALEX: So sorry about that, isBottom was left in as a mistake, my current implementation of this script includes additional functionality, and scans the page for certain elements that have a background image positioned at the bottom, so provide a visual "bottom" to their child element. i removed it from my snippet as well as from the download.

    the function that i put together to fix the bottom positioned bg images is this.

    [js]
    function fixBoxes(){
    var boxes = document.getElementsByClassName("box-wrapper");
    for (var i = 0; i < boxes.length; i++){
    var boxBottom = document.createElement("div");
    var src = boxes[i].currentStyle.backgroundImage.substring(5,boxes[i].currentStyle.backgroundImage.length - 2);
    boxBottom.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
    boxBottom.className = "box-bottom";
    boxes[i].style.backgroundImage = "none";
    boxes[i].appendChild(boxBottom);
    }
    fnLoadPngs(); // now let's fix the pngs.
    }
    [/js]

    and it's called like so..

    [js]
    //...
    return {
    init: function() {
    if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
    if (document.getElementsByClassName("box-wrapper")){ // check to see if we have any 'boxes' on the page
    addLoadEvent(fixBoxes);
    } else {
    addLoadEvent(fnLoadPngs);
    }
    }
    }
    }
    }();
    bgsleight.init();
    [/js]
  • tHANKS!
  • I’m working on a personal portfolio and I’ve run into the IE png glitch, both for regular pngs as well as repeating background ones. I’ve looked around for lots of different png hacks and fixes but can’t seem to find any that actually Work. I've read lots of positive reviews about the sleight hacks but yet when i try them on my site they don't help at all. i've tried asking around on different sites for suggestions but no one seems to ever reply back. my site is currently at:

    http://www.pinstripepresentations.com/index.php

    Any help would be appreciated if you can spare the time. thank you very much.
  • Hello

    I was using an old version of sleight+sleightbg, but I have a big problem with png background in DIV (the picture is scaled, so your script seems to be the solution, but, what are these pictures : "x.gif and "../images/blank.gif" ?

    Thans a lot
  • Paul
    Hi Nater,
    I'm eager to try out your png fix - i'm not a coder, so bear with me: the link back to the instructions on Youngpup’s site doesn't work - he seems to have removed the archived post? so now there doesn't seem to be any instructions?

    i've linked the .js file to my page, placed an x.gif in the root, and added a "blank.gif" in root as you've noted. (and in both cases corrected the urls in the js file to match.

    no luck!
    any thoughts?
    thanks,
    paul
  • dano
    what are these pictures : "x.gif and "../images/blank.gif" ?

    Thanks!
  • Your script modifications did a great job fixing the repeatable background PNG issue...but now none of my links are clickable...any ideas?

    wedding.kedinger.com

    Thanks,
    Daniel Kedinger
  • spencerb
    Hey Nater, great fix! Your hack worked for me.

    In regards to Daniel Kedinger's comment, wondering how to fix the unclickable links, try putting position:relative on the links that are unclickable, and it should work.

    Thanks Nater!
  • Joel
    I wonder why this still doesn't work for my repeating png backgrounds?? All my other png's are fixed with the script, but not the repeat-y. I edited the transparent.gif path to the correct path - but still no luck. Am I missing something??
  • Joel
    oops, I needed a height on the element that has the png background!
  • Wow great site! Some really helpful information there.
    I’m sorry for little off-topic, but I want to ask you about design of this site.
    Did you make this template yourself or got from any templates website?
    Looks pretty cool for me. Wonderful well this reading.
  • PaySo
    Any chance that i can see this on a working example?

    I use the superslight all the time but am sick of the limitations to be honest.
  • Imran Hossain
    Hi, thanks for the code. For me the code works only for a split second. As in I can see the repeating background transparency working - but then it goes back to previous state. It works perfectly for the non-repeating backgrounds.
  • To Imran Hossain:

    The object that the filter is applied to must have layout before the filter effect will display. You can give the object layout by setting the height or width property, setting the position property to absolute, setting the writingMode property to tb-rl, or setting the contentEditable property to true.

    Take from: http://msdn.microsoft.com/en-us/library/ms53296...
  • Tejas
    can you please post your code for background based HTML ?
    as i have tried your code, place transparent.gif file but it didnt work.

    please reply me on my mail-id so i can show you my code also.
blog comments powered by Disqus

Similar Posts

Categories

Archives

About

Nater Kane is freelance developer and user experience & technology consultant based in Brooklyn, NY.

Nater's focus is on creating a semantic and accessible web, and having delighted clients with happy customers.

He likes to spend time playing with his cats, playing drums, working on his diesel vw rabbit and his motorcycle, and enjoying a decent espresso.

RSS

Twitter » What I'm Up To

  • taking a 20 min break, then I guess I'll get back to work at 8. no rest for the weary! 23 mins ago
  • Sarah Palin is resigning? She'll no longer be the governor of Alaska? Good for her, better for Alaskans. #tcot 2 hrs ago
  • it's very much definitely lunch time. so what to have for lunch? @EatTime is making breakfast for me! woot! 5 hrs ago
  • More updates...

How I'm Social

Basecamp

Meta


Nater Kane naterkane personal http://www.naterkane.com LinkedIn Profile Web Technologist personal nater@naterkane.com 1978-09-12 voice 845.234.6698 | fax 707.922.0593
964 Flushing Ave. Brooklyn, NY 11206