在jquery中使用淡入淡出效果更改正文背景图像

时间:2013-11-28 00:52:49

标签: jquery css background-image

嘿,我想淡出一个新的背景图像,让我们说每60秒。我设置了这样的背景图片:

body {background-image: url(background.jpg);}

现在我想改变它,所以在60秒后它变为background2.jpg,然后在60秒后变为background3.jpg等等。

我发现很多东西没有在身体上改变它,但只是作为一个图像...... 任何快速解决方案?

谢谢!

7 个答案:

答案 0 :(得分:21)

UPDATE的重新更新:

Even NEWER Fiddle (without arguments.callee)

的变化:


BIG UPDATE


this previous answer获取此代码的内容,并添加了一些金光闪闪(使用我的网站背景存储lol)

original fiddle :)

NEW Super Fiddle

<强>使用Javascript:

$(document).ready(function () {
    var img_array = [1, 2, 3, 4, 5],
        newIndex = 0,
        index = 0,
        interval = 5000;
    (function changeBg() {

        //  --------------------------
        //  For random image rotation:
        //  --------------------------

            //  newIndex = Math.floor(Math.random() * 10) % img_array.length;
            //  index = (newIndex === index) ? newIndex -1 : newIndex;

        //  ------------------------------
        //  For sequential image rotation:
        //  ------------------------------

            index = (index + 1) % img_array.length;

        $('body').css('backgroundImage', function () {
            $('#fullPage').animate({
                backgroundColor: 'transparent'
            }, 1000, function () {
                setTimeout(function () {
                    $('#fullPage').animate({
                        backgroundColor: 'rgb(255,255,255)'
                    }, 1000);
                }, 3000);
            });
            return 'url(http://www.fleeceitout.com/images/field.' + img_array[index] + '.jpg)';
        });
        setTimeout(changeBg, interval);
    })();
});

<强> CSS:

body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    position: relative;
    background-image: url(http://www.fleeceitout.com/images/field.2.jpg);
    background-size: cover;
    background-repeat: no-repeat;
    background-position: 0 0;
    background-attachment: fixed;
}
#fullPage {
    position: absolute;
    min-width: 100%;
    min-height: 100%;
    top: 0;
    left: 0;
    background-color: rgb(255, 255, 255);
}

答案 1 :(得分:11)

您可以使用setInterval方法并在CSS中定义的具有不同背景图像的类之间切换:

setInterval(function() {
    var $body = $('body');
    if($body.hasClass('background1'))
    {
        $body.removeClass('background1');
        $body.addClass('background2');
    }
    else {        
        $body.removeClass('background2');
        $body.addClass('background1');
    }
}, 1000);

Fiddle

此示例使用1000的间隔,即1秒。您可以在需要的任何时间段内更改此值。

<强>更新

注意到你的问题要求淡入淡出,所以我在body上添加了一个CSS3属性:

body
{
    transition: background 0.5s linear;
}

小提琴已更新。

答案 2 :(得分:7)

根据Dan-Nolan(以前称为user506980)的答案,您还可以将背景指定给数组,然后使用计数器调用数组中的每个背景

jsFiddle Demo

此外,您可以将setInterval函数指定给变量,然后稍后使用该变量来停止重复。

$(document).ready(function() {
    var cnt=0, bg;
    var $body = $('body');
    var arr = ['bg1.jpg','bg2.jpg','bg3.jpg','bg4.jpg','bg5.jpg','bg6.jpg'];

    var bgrotater = setInterval(function() {
        if (cnt==5) cnt=0;
        bg = 'url("' + arr[cnt] + '")';
        cnt++;
        $body.css('background-image', bg);
    }, 1000);

    //To stop the backgrounds from rotating. Note the critical step above
    //of assigning the setInterval function to a variable, in order to
    //use it again (below) to stop the repeating function
    $('#some_button_id').click(function() {
        clearInterval(bgrotater);
    });

}); //END document.ready

由于我建立了Dan-Nolan的答案,如果你赞成这个答案,请提高他的答案。我看到Deryck已经添加了他的答案,这也是正确的。给予好评。

答案 3 :(得分:2)

https://github.com/srobbin/jquery-backstretch Backstretch是一个简单的jQuery插件,允许您将动态调整大小,支持幻灯片的背景图像添加到任何页面或元素。图像将拉伸以适合页面/元素,并在窗口/元素大小更改时自动调整大小。

答案 4 :(得分:0)

jQuery(document).ready(function() {
var cnt=0, bg;
var $body = jQuery('body');
var arr = ['secondbg.jpg','sartulebis-archeva2.png'];

animate = function(){

}
var bgrotater = setInterval(function() {
    if (cnt==2) cnt=0;
    bg = 'url("http://yalcingroup.ge/test/wp-content/uploads/2015/08/' + arr[cnt] + '")';
    cnt++;
    jQuery('#background_cycler').animate({opacity:1}, 2000, function(){
        $body.css('background-image', bg);
    });


    jQuery('#background_cycler').animate({opacity:0}, 2000);
},10000);

});

答案 5 :(得分:0)

每12秒在presentacion容器中更改图像;它可以是身体标签

<强> HTML

<div class="presentacion">
  <div class="mask"></div>
</div>

<强> JS

延迟(11000)+ setTimeout(1000)= 12秒 转换持续时间= 300 + 300 = 600毫秒

var imgArray = ['image-1', 'image-2', 'image-3'], index = 0;

(function changeBG(){
  // mask element only for transition
  $('.mask').delay(11000).animate({opacity:1}, 300, function(){
    index = (index + 1) % img_array.length;
    // in presentacion element change bg images
    $('.presentacion').css("background-image", "url('images/bg-"+imgArray[index]+".jpg')");
  }).animate({opacity: 0}, 300);
  setTimeout(changeBG, 1000);
})();

<强> CSS

.presentacion {
  background-attachment: fixed;
  background-color: rgba(0, 0, 0, 0.5);
  background-image: url("images/image-1.jpg");
  background-position: center center;
  background-repeat: no-repeat;
  -webkit-background-size: cover;
  background-size: cover;
  position: relative;
  width: 100%;
  z-index: 0;
  opacity: 1;
}
.mask {
  background-color: rgba(0,0,0,0);
  bottom: 0;
  left: 0;
  opacity: 0;
  position: absolute;
  right: 0;
  top: 0;
  height: 100%;
  z-index: 10;
}

答案 6 :(得分:0)

以Deryck的回答为基础......

如果jQuery Color无法正常运行(有时会这样做),您可以改为使用fadeIn()fadeOut()

 $(document).ready(function () {
    var img_array = [1, 2, 3, 4, 5],
        newIndex = 0,
        index = 0,
        interval = 5000;
    (function changeBg() {

        //  --------------------------
        //  For random image rotation:
        //  --------------------------

            //  newIndex = Math.floor(Math.random() * 10) % img_array.length;
            //  index = (newIndex === index) ? newIndex -1 : newIndex;

        //  ------------------------------
        //  For sequential image rotation:
        //  ------------------------------

            index = (index + 1) % img_array.length;

        $('body').css('backgroundImage', function () {
            $('#fullPage').fadeOut(1000, function () {
                setTimeout(function () {
                    $('#fullPage').fadeIn(1000);
                }, 3000);
            });
            return 'url(http://www.fleeceitout.com/images/field.' + img_array[index] + '.jpg)';
        });
        setTimeout(changeBg, interval);
    })();
});
相关问题