0

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!

2 Answers2

1

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.

tmartineau
  • 110
  • 1
  • 8
  • Thank you for your response...I do not think that will work for what I need because I want to keep it as a background so I can use "background-size:cover". This scales the image proportionally with no distortion and does so for every browser except IE8. That filter I added only effects IE and works fine (stretches the image) but it just breaks the fading functionality. – Rusty Rayner Apr 03 '12 at 15:00
  • Keeping it as an image "layered" in the back you can use `width: 100%` and `height: auto`. That should scale proportionality and I think this even works as far back as IE8. Plus I have found that in some cases setting the background scaling can affect performance when doing some animating. – tmartineau Apr 03 '12 at 18:12
  • That worked. Thank you very much. Here is the updated code. HTML: CSS: img#bgFade { position:fixed; top:0px; left:0; width:100%; height:auto; z-index:-1; } JavaScript function changeImg(){ $('#bgFade').animate({opacity: 0}, 1000, function(){ $(this).attr('src',preloadArr[currImg++%preloadArr.length].src); }).animate({opacity: 1}, 1000); } Thank you again!!!!! – Rusty Rayner Apr 04 '12 at 18:12
0

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'"
Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34
  • Thank you for your response.... That line of code works fine, it just breaks the fading functionality. If I remove that line it fades fine -- If I leave it in it stretches the image correctly but no fading animation. – Rusty Rayner Apr 03 '12 at 14:48