50

I want to fade the images when I do this code:

$("#large-img").css('background-image', 'url('+$img+')');

I've tried putting fade in so many places.

Thanks

moffeltje
  • 4,521
  • 4
  • 33
  • 57
fadejquery
  • 503
  • 1
  • 4
  • 4
  • @fade.. whats your question? are you asking how to do fade or are u tried how to fade the images by that code..please mention clearly. – Mihir Feb 15 '11 at 10:19
  • If `#large-img` has no content in, then I suggest you fade out the whole element, because you can't fade out an individual background image. – Anriëtte Myburgh Feb 15 '11 at 10:28
  • @mihir I am trying to fade the old background image out and the new background image in – fadejquery Feb 15 '11 at 12:03
  • @anriette it doesnt have any content in, so i should fade it out, change background image and fade it back in? – fadejquery Feb 15 '11 at 12:04

8 Answers8

79

This is probably what you wanted:

$('#elem').fadeTo('slow', 0.3, function()
{
    $(this).css('background-image', 'url(' + $img + ')');
}).fadeTo('slow', 1);

With a 1 second delay:

$('#elem').fadeTo('slow', 0.3, function()
{
    $(this).css('background-image', 'url(' + $img + ')');
}).delay(1000).fadeTo('slow', 1);
MacMac
  • 34,294
  • 55
  • 151
  • 222
  • I understand exactly where you are coming from but I am changing the background on a different element. I click the thumbnail and then it changes the large image to the image from the thumbnail – fadejquery Feb 15 '11 at 11:30
  • 3
    ok i got that working but it sometimes gets stuck on fading and appears darkened or it jumps/cuts between images. not as smooth as i'd like – fadejquery Feb 15 '11 at 11:36
  • ok, i've fixed it getting stuck. how can i make it fade the new image in as well as fading the other image out? – fadejquery Feb 15 '11 at 11:44
  • I believe you have your delay function in the wrong place; after adding it to the first line the script runs as desired. – Jim22150 Feb 19 '13 at 05:15
64

Can I offer an alternative solution?

I had this same issue, and fade didn't work because it faded the entire element, not just the background. In my case the element was body, so I only wanted to fade the background.

An elegant way to tackle this is to class the element and use CSS3 transition for the background.

transition: background 0.5s linear;

When you change the background, either with toggleClass or with your code, $("#large-img").css('background-image', 'url('+$img+')'); it will fade as defined by the class.

Bill Keller
  • 793
  • 7
  • 22
  • 4
    how is it working for you? I thought background-images could not be transitioned... – ithil Jun 17 '13 at 09:16
  • That was a elegant soloution, annoying css3 doesnt support bg image transitions, here is an alternative css method: http://www.mightymeta.co.uk/fading-button-background-images-with-css3-transitions/ – Alex Jun 17 '13 at 14:33
  • 1
    At the moment: only working in chrome for me. Hope browsersupport will catch up with this one. – cumul Jun 09 '15 at 21:54
  • This is the best solution ever, you don't even need jQuery for the transition. It's also great, because if you do change the background image, it won't fade it if the new background image is the same. This is really useful for JS sliders. – Daniel Dec 09 '15 at 04:44
29

This was the only reasonable thing I found to fade a background image.

<div id="foo">
  <!-- some content here -->
</div>

Your CSS; now enhanced with CSS3 transition.

#foo {
  background-image: url(a.jpg);
  transition: background 1s linear;
}

Now swap out the background

document.getElementById("foo").style.backgroundImage = "url(b.jpg)";

Voilà, it fades!


Obvious disclaimer: if your browser doesn't support the CSS3 transition property, this won't work. In which case, the image will change without a transition. Given <1% of your users' browser don't support CSS3, that's probably fine. Don't kid yourself, it's fine.

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • The best solution, I had a lot of code combining transitions of background-color s , changing the background-image and more transitions.... this solution makes it better and easier – Albert Català Feb 16 '17 at 15:04
  • 1
    But I can't make it working in FF ¿? Although I use -moz-transition, why? – Albert Català Feb 16 '17 at 15:15
6

Building on Rampant Creative Group's solution above, I was using jQuery to change the background image of the body tag:

e.g.

$('body').css({'background': 'url(/wp-content/themes/opdemand/img/bg-sea.jpg) fixed', 'background-size': '100% 100%'});

$('body').css({'background': 'url(/wp-content/themes/opdemand/img/bg-trees.jpg) fixed', 'background-size': '100% 100%'});

I had a javascript timer that switched between the two statements.

All I had to do to solve the issue of creating a fadeOut -> fadeIn effect was use Rampant Creative Group's suggestion and add

transition: background 1.5s linear;

to my code. Now it fades out and in beautifully.

Thanks Rampant Creative Group's and SoupEnvy for the edit!!

Ben
  • 5,085
  • 9
  • 39
  • 58
1

Someone pointed me to this thread because I had this same issue but it didn't work for me. After hours of searching I found a solution using this - https://github.com/rewish/jquery-bgswitcher#readme

It has a few other options other than fade too.

Bizzaro
  • 65
  • 2
  • 12
  • That's nicely working. Today we have CSS transitions and can achieve that with 1 line of code [ https://stackoverflow.com/a/23166579/195812 ] – Augusto Oct 22 '20 at 09:58
0

Opacity serves your purpose?

If so, try this:

$('#elem').css('opacity','0.3')
Soulbe
  • 444
  • 8
  • 14
0

If your trying to fade the backgound image but leave the foreground text/images you could use css to separate the background image into a new div and position it over the div containing the text/images then fade the background div.

iandayman
  • 4,357
  • 31
  • 38
  • i don't need that. i have a set of thumbnail images and when i click the thumbnail it will change the large img from the href of the thumbnail. i want this changeover of the large image to fade in/out. – fadejquery Feb 15 '11 at 10:35
  • ok i see. i've used the lightbox addin for something similar http://leandrovieira.com/projects/jquery/lightbox/ but again i dont think that's exactly what you're after. good luck. – iandayman Feb 15 '11 at 10:43
0

This may help

HTML

<div id="textcontainer">        
    <span id="sometext">This is some information </span>
    <div id="container">

    </div>
</div>

CSS

#textcontainer{
    position:relative;
    border:1px solid #000;
    width :300px;
    height:300px;
}
#container{
    background-image :url("http://dcooper.org/gallery/cf_appicon.jpg");
    width :100%;
    height:100%;
}
#sometext{
    position:absolute;
    top:50%;
    left:50%;
}

Js

$('#container').css('opacity','.1');
Anooj
  • 286
  • 3
  • 13