Question

I need to create a fading background image that stretches to fill the browser. The following code works perfectly in every single browser except in IE8 and below. The IE8 issue is that I can get it to fade but not stretch or to stretch but not fade - Not both at the same time.

THE JAVASCRIPT

$(document).ready(function(){

    var imgArr = new Array( // relative paths of images
        'images/bg01.jpg',
        'images/bg02.jpg',
        'images/bg03.jpg',
        'images/bg04.jpg',
        'images/bg05.jpg'
    );


var preloadArr = new Array();
var i;


/* preload images */
for(i=0; i < imgArr.length; i++){
    preloadArr[i] = new Image();
    preloadArr[i].src = imgArr[i];
}

var currImg = 0;
changeImg();
var intID = setInterval(changeImg, 7500);

/* image rotator */
function changeImg(){
$('#bgFade').animate({opacity: 0}, 1000, function(){
$(this).css({
    'background':'url(' + preloadArr[currImg++%preloadArr.length].src +') center 49px',
    '-webkit-background-size':'cover',
    '-moz-background-size':'cover',
    '-o-background-size':'cover',
    'background-size':'cover',
    'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+ preloadArr[currImg++%preloadArr.length].src +'\', sizingMethod=\'scale\''
    });
}).animate({opacity: 1}, 1000);
}

});

Here is the line I added to make it stretch in IE8 (and below) but it breaks the fading functionality. It would be great if IE8 supported "background-size" but it does not.

'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+ preloadArr[currImg++%preloadArr.length].src +'\', sizingMethod=\'scale\''

THE CSS/HTML

#bgFade {
    position:fixed;
    top:0;
    left:0;
    height:100%;
    width:100%;
    z-index:-1;
}

<div id="bgFade"></div>

I hope there is a solution to both stretch and fade the background images in IE8. If the solution only works in IE8 but not in IE7 or below that is fine. IE8 and above is my only concern at the moment. Thanks in advance!

Was it helpful?

Solution

From what I can see you are using the background property on an html element. What if you tried instead using an image element and just layering it in behind your content. I would think you should be able to just set width and height on that no problem (even in IE8) and then use jQuery's .fade() or animate the opacity.

OTHER TIPS

This filter value string doesn't look quite right.

'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+ preloadArr[currImg++%preloadArr.length].src +'\', sizingMethod=\'scale\''

Try:

'filter':"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + preloadArr[currImg++%preloadArr.length].src + "'), sizingMethod='scale'"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top