【问题标题】:how to change div background image with animation如何用动画更改div背景图像
【发布时间】:2015-03-15 11:27:23
【问题描述】:

有没有办法在 JQuery 中通过淡出或动画来更改 DIV 背景?

例如,我想用这段代码改变背景

$('#DIV').css("background", "url(Image1.jpg)")

if (Button1 == true) {
$('#DIV').css("background", "url(Image2.jpg)") }

【问题讨论】:

    标签: html background jquery-animate


    【解决方案1】:

    使用这个 Jquery:

    function test() {
        $("#DIV").each(function(index) {
            $(this).hide();
            $(this).delay(2000* index).fadeIn(2000).fadeOut();
        });
    }
    test();
    

    Live Demo

    或者试试这个:

    Live Demo

    $('.imges').fadeTo('slow', 0.3, function()
    {
        $(this).css('background-image', 'url(http://mintywhite.com/wp-content/uploads/2012/10/fond-ecran-wallpaper-image-arriere-plan-hd-29-HD.jpg)');
    }).delay(1000).fadeTo('slow', 1);
    

    关于您的编辑,我认为这满足您的需求:

    var rotateEvery = 2; //seconds
    var images = [
        "http://placehold.it/148x148",
        "http://placehold.it/150x149"];
    var curImage = 0;
    setInterval(delayFunction, rotateEvery*1000);
    function delayFunction() {
      if(curImage == images.length){
        curImage = 0;
      }
      $('#homeback').fadeOut('slow',function(){
        $(this).css("background-image",'url('+images[curImage]+')').fadeIn('slow');
          curImage++;
      });
    
    }
    

    LIVE DEMO

    【讨论】:

    • 我想将例如'image1.jpg'更改为'image2.jpg'作为背景我认为这段代码不能改变它
    • 我测试了你的演示,但它有 3 个不同的 标签,它确实会与动画一起改变背景,但我想将
      背景从 image1.jpg 更改为 imag2.jpg,我想改变只有一个 DIV 背景
    • 感谢您的更新,我在我的程序中测试了它,但它还没有很好的动画效果,所以如果可以使用不同的代码以获得更好的效果,所以我需要它
    • 完美,非常感谢
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签