javascript - Smooth fade when changing the background image - Stack Overflow

This is my code, to see it in action, take a look at this FiddleHTML<div id="header">.

This is my code, to see it in action, take a look at this Fiddle

HTML

<div id="header">
.
.
.
</div>

CSS

#header {
background: url(images/img8681.jpg);
background-size: cover;
border-bottom: 8px solid #333333;
height: 620px;
}

Javasript (jQuery)

var imgs = new Array("images/img8681.jpg","","","","");

function changeBg() {
    var imgUrl = imgs[Math.floor(Math.random()*imgs.length)];
    $('#header').css('background-image', 'url(' + imgUrl + ')');
}

setInterval(changeBg,5000);

My question how can I have the change of the images smoothly instead of "just replace" ? And how to avoid continuously appear of the same image ?

This is my code, to see it in action, take a look at this Fiddle

HTML

<div id="header">
.
.
.
</div>

CSS

#header {
background: url(images/img8681.jpg);
background-size: cover;
border-bottom: 8px solid #333333;
height: 620px;
}

Javasript (jQuery)

var imgs = new Array("images/img8681.jpg","","","","");

function changeBg() {
    var imgUrl = imgs[Math.floor(Math.random()*imgs.length)];
    $('#header').css('background-image', 'url(' + imgUrl + ')');
}

setInterval(changeBg,5000);

My question how can I have the change of the images smoothly instead of "just replace" ? And how to avoid continuously appear of the same image ?

Share edited Jul 14, 2014 at 11:27 Theolodis 5,1023 gold badges36 silver badges56 bronze badges asked Jun 8, 2014 at 14:20 EnexoOnomaEnexoOnoma 8,84420 gold badges98 silver badges186 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can get a smoother change of the image if you use the fadeIn and fadeOut functions.

var imgs = new Array("img1.jpg","img2.jpg","img3.jpg","img4.jpg","img5.jpg");
function changeBg() {
    var imgUrl = imgs[Math.floor(Math.random()*imgs.length)];
    $('#header').css('background-image', 'url(' + imgUrl + ')');
    $('#header').fadeIn(1000); //this is new, will fade in smoothly
}

function changeBackgroundSmoothly() {
    $('#header').fadeOut(1000, changeBg); //this is new, will fade out smoothly
}

setInterval(changeBackgroundSmoothly,5000);

See this Fiddle to see the result.

Concerning the randomness of the images you can't do a lot if it does have to be random. Simply because random implies that the same image might appear twice in a row, otherwise it wouldn't be totally random as you could exclude one result.

A solution might be to not display the images randomly, but rather in a predefined sequence, refer to this site for an example.

Just another approach

$("#header").fadeOut(500, //Speed
                     function () { //On fadeOut plete
                            $(this).css("background-image", "url(img2.jpg)") //Change BG
                                   .fadeIn(); //FadeIn
                     });

You can check here: https://jsfiddle/rafaelaca/wwjro184/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745059724a4608891.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信