简体   繁体   English

用动画改变div的背景图像

[英]change background-image of div with animation

I would like to create a little slider to change background-image of my div every seconds. 我想创建一个小滑块来每秒更改我div的背景图像。

My code doesn't work for the moment, image is not changed. 我的代码暂时不起作用,图像不会改变。 And ideally, i would like that the script run in infinite mode.. 理想情况下,我希望脚本以无限模式运行。

HTML HTML

<div id="slidesPartenairesHome"></div>

CSS CSS

#slidesPartenairesHome {
    background-size: contain;
    background-position: center center;
    width: 300px;
    height: 170px;
    margin-left: 120px;
}

JS JS

   $( document ).ready(function() {

        var arrayOfPartenaires = [
            "images/partenaires/a.png",
            "images/partenaires/b.jpg",
            "images/partenaires/c.jpg",
            "images/partenaires/d.png",
            "images/partenaires/e.png",
            "images/partenaires/f.jpg",
            "images/partenaires/g.jpg",
            "images/partenaires/h.jpg",
            "images/partenaires/i.png",
            "images/partenaires/j.jpg",
            "images/partenaires/k.jpg",
            "images/partenaires/l.jpg"
        ];


        for (var i=0; i<arrayOfPartenaires.length; i++) {

            var currentPartenaireImg = arrayOfPartenaires[i];

            $('#slidesPartenairesHome').animate({opacity: 0}, 'slow', function() {
                $(this).css({'background-image': 'url("'+currentPartenaireImg+')'}).animate({opacity: 1});
            });

        }

    });

You could use window.setinterval, you could also use setTimeout but setinterval is a litle bit more precise. 你可以使用window.setinterval,你也可以使用setTimeout但是setinterval更精确。

Example with setinteval: setinteval示例:

window.setInterval(function(){
    var url = getCurrent();
    //start animation
    $('#slidesPartenairesHome').delay( 500 ).fadeTo(500, 0.3, function()
    {
        $(this).css('background-image', 'url(' + url + ')');
    }).fadeTo('slow', 1);
}, 1000);

// We start with index of 1 because we want to skip the first image, 
// Else we would be replacing it with the same image.
var index = 1;
     var arrayOfPartenaires = [
            "http://yourdomain.com/images/partenaires/a.png",
            "http://yourdomain.com/images/partenaires/b.png",
            "http://yourdomain.com/images/partenaires/c.png"
        ];

function getCurrent(){
    // We check if the index is higher than the ammount in the array.
    // If thats true set 0 (beginning of array)
    if (index > arrayOfPartenaires.length -1){
        index = 0;
    }
    var returnValue = index;
    index ++;
    return arrayOfPartenaires[returnValue];       
}

Note if you really want to change the image every 1 second the background will be changing very fast. 请注意,如果您真的想每1秒更改一次图像,背景将会非常快速地变化。

Fiddle 小提琴

I hope this may help you 我希望这可以帮到你

html HTML

<div id="slidesPartenairesHome">
        <div id="imags">
        </div>
    </div>

Css CSS

#slidesPartenairesHome
        {
            margin-left: 120px;
        }
        #slidesPartenairesHome, #imags
        {
            background-size: contain;
            background-position: center center;
            width: 300px;
            height: 170px;
        }

Js JS

 $(function () {
            var arrayOfPartenaires = [
            "http://fotos2013.cloud.noticias24.com/animales1.jpg",
            "http://www.schnauzi.com/wp-content/uploads/2013/03/animales-en-primavera.jpg",
            "https://johannagrandac.files.wordpress.com/2015/01/conejos.jpg",
             "http://png-4.findicons.com/files/icons/1035/human_o2/128/face_smile.png",
            "http://icons.iconarchive.com/icons/rokey/the-blacy/128/big-smile-icon.png",
            "http://simpleicon.com/wp-content/uploads/smile-256x256.png"
        ];
            var loaders = 0;
            function cycleImages() {
                var element = arrayOfPartenaires[loaders];
                $("#imags").css({ 'background-image': 'url(' + element + ')' }).animate({ opacity: 1 }).hide().fadeIn("slow");
                if (loaders < arrayOfPartenaires.length) {
                    loaders = loaders + 1;
                    if (loaders >= arrayOfPartenaires.length) {                        
                        loaders = 0;
                    } 
                }
                else {
                    loaders = 0;
                }
                console.log(loaders, arrayOfPartenaires[loaders]);
            }
            cycleImages();
            setInterval(function () { cycleImages() }, 3000);
        });

jsFiddel Demo jsFiddel演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM