How to fade changing background image

后端 未结 8 1967
旧巷少年郎
旧巷少年郎 2020-11-27 14:48

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

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

I\'ve tried putting fade i

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 14:56

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

    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.

提交回复
热议问题