Can I fade in a background image (CSS: background-image) with jQuery?

后端 未结 9 1154
一生所求
一生所求 2020-11-29 00:52

I have a div element with text in it and a background image, which is set via the CSS property background-image. Is it possible to fade in the back

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 01:06

    i have been searching for ages and finally i put everything together to find my solution. Folks say, you cannot fade-in/-out the background-image- id of the html-background. Thats definitely wrong as you can figure out by implementing the below mentioned demo

    CSS:

    html, body
    
    height: 100%;   /* ges Hoehe der Seite -> weitere Hoehenangaben werden relativ hierzu ausgewertet */
    overflow: hidden;   /*  hide scrollbars */
    opacity: 1.0;
    -webkit-transition: background 1.5s linear;
    -moz-transition: background 1.5s linear;
    -o-transition: background 1.5s linear;
    -ms-transition: background 1.5s linear;
    transition: background 1.5s linear;
    

    Changing body's background-image can now easily be done using JavaScript:

    switch (dummy)
    
    case 1:
    
    $(document.body).css({"background-image": "url("+URL_of_pic_One+")"});
    waitAWhile();
    
    case 2:
    $(document.body).css({"background-image": "url("+URL_of_pic_Two+")"});
    waitAWhile();
    

提交回复
热议问题