使用jQuery交叉淡入淡出背景图像 [英] Cross fade background-image with jQuery

查看:183
本文介绍了使用jQuery交叉淡入淡出背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些图像,我想显示为幻灯片在背景中。然而,我想让图像在当前图像和下一个图像之间交叉淡入淡出。到目前为止,我只能在图像之间切换:

so I have some images and I would like to show them as a slideshow in the background. However, I want the image to cross-fade between the current image and the next image. So far, I have only been able to switch between the images:

$(document).ready(function () {

    var images = ["landing_background_1.jpg", "landing_background_2.jpg", "landing_background_3.jpg", "landing_background_4.jpg"];

    var currentImage = 0;


    function changeBackgroundImage() {


        $("html").fadeIn().css({

            "background-image": "url('img/backgrounds/" + images[++currentImage] + "')",

        });


        if (currentImage >= images.length - 1) {

            //set it back to the begining
            currentImage -= images.length;

        }

    }


    setInterval(changeBackgroundImage, 1500);

});

任何帮助将非常感谢! :)

Any help would be much appreciated! :)

推荐答案

你需要做的是将两个元素叠加在一起。

What you have to do is layer two element on top of each other. Then have one fadeout and the other fadein.

这是我要怎么做...

Here is how I would go about doing it ...

css ...

#background-images {
    position: fixed; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    z-index: 1;
    }

#bImg1 {
    position: absolute; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    z-index: 3; 
    background: url(starting-img1.jpg) no-repeat; 
    background-size: contain;
    }

#bImg2 {
    display: none;
    position: absolute; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    z-index: 2; 
    background: url(starting-img2.jpg) no-repeat; 
    background-size: contain;
    }

.container {
    width: 960px;
    height: 900px;
    background: rgba(255,255,255,.7);
    margin: auto;
    position: relative;
    z-index: 10;
}

html ...

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<html>
<body>
    <div id="background-images">
        <div id="bImg1"></div>
        <div id="bImg2"></div>
    </div>
    <div class="container">
        Content Here
    </div>
</body>
</html>

脚本...

var imageSet1 = ["image1.jpg", "image2.jpg", "image3.jpg"];
var currentImageSet1 = 0;

var imageSet2 = ["image4.jpg", "image5.jpg", "image6.jpg"];
var currentImageSet2 = 0;

function changeBackgroundImages() {
    img1Fade();
    setTimeout(img2Fade, 2000);

}

function img1Fade(){

    $('#bImg1').fadeOut('slow', function(){$('#bImg1').css({background: 'url(' + imageSet1[++currentImageSet1] + ')'})});
    $('#bImg2').fadeIn('slow');
    if (currentImageSet1 >= imageSet1.length - 1) {

            currentImageSet1 -= imageSet1.length;
        };
}

function img2Fade(){

    $('#bImg2').fadeOut('slow', function(){$('#bImg2').css({background: 'url(' + imageSet2[++currentImageSet2] + ')'})});
    $('#bImg1').fadeIn('slow');
    if (currentImageSet2 >= imageSet2.length - 1) {

            currentImageSet2 -= imageSet2.length;
        };
}

$(document).ready(function(){

    setInterval(changeBackgroundImages, 5000);
});

你将需要混乱的时机让它看起来不错。请确保将URL设置为图像数组中的图像,或者在css中创建了sting。

You will need to mess with the timing to get it to look good. Make sure to set your urls to the images in the image array or when the sting in the css is built.

这篇关于使用jQuery交叉淡入淡出背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆