【问题标题】:How to loop a css background image with Jquery every few seconds?如何每隔几秒钟用Jquery循环一个css背景图像?
【发布时间】:2020-08-18 17:20:43
【问题描述】:

我想每隔几秒更改一个标题 css 背景图像,让它看起来像幻灯片。

例如前 2 秒是:

body#home h1#siteH1 { background:url(../images/header1.jpg) no-repeat;}

接下来的 2 秒是:

body#home h1#siteH1 { background:url(../images/header2.jpg) no-repeat;}

接下来的 2 秒是:

body#home h1#siteH1 { background:url(../images/header3.jpg) no-repeat;}

然后再次循环到 header1。

如果有人知道如何使用淡入淡出效果进行过渡,那将是完美的。

【问题讨论】:

  • body#home h1#siteH1 是一个非常低效的选择器。为什么不直接使用#siteH1
  • 据我所知,jQuery 选择器也是这种情况吗?

标签: jquery css


【解决方案1】:

现在使用淡入淡出

试试这个:

var currentBackground = 0;
var backgrounds = [];
backgrounds[0] = '../images/header1.jpg';
backgrounds[1] = '../images/header2.jpg';
backgrounds[2] = '../images/header3.jpg';

function changeBackground() {
    currentBackground++;
    if(currentBackground > 2) currentBackground = 0;

    $('body#home h1#siteH1').fadeOut(100,function() {
        $('body#home h1#siteH1').css({
            'background-image' : "url('" + backgrounds[currentBackground] + "')"
        });
        $('body#home h1#siteH1').fadeIn(100);
    });


    setTimeout(changeBackground, 2000);
}

$(document).ready(function() {
    setTimeout(changeBackground, 2000);        
});

【讨论】:

  • 您的代码会立即执行。在更改背景之前不等待任何时间,并且不会再次回到初始背景。
  • 基本上,我想在背景图片之间循环
  • 谢谢,它有效。有没有办法淡出背景并淡入下一个?
  • 我不会取消你的投票。但是,如果我两次投票给你,它会删除旧票并说“投票太旧而不能改变,除非这个答案被编辑”
  • 与其使用2次setTimeout,不如使用setInterval。
【解决方案2】:

签出队列功能:

jQuery Queue

【讨论】:

    【解决方案3】:

    聚会迟到了,但这是我刚刚提出的类似要求。

    <script type="text/javascript">
    
        // populate image set
        var imageArray = [
            "1.jpg",
            "2.jpg",
            "3.jpg",
            "4.jpg"
        ];
    
        // in milliseconds
        var fadeSpeed   = 1000;
        var timeout     = 3000;
    
    
        function fadeInFront (i) {
        
            $('#faderFront').css( {
                "background-image" : "url(" + imageArray[i] + ")"
            });
        
            $('#faderFront').fadeIn(fadeSpeed);
        
            i++;
        
            if ( i == imageArray.length ) {
                i=0;
            }
    
            setTimeout(function() {
                fadeOutFront(i);
            },timeout);
        
        }
    
        function fadeOutFront (i){
        
            $('#faderBack').css( {
                "background-image" : "url(" + imageArray[i] + ")"
            });
        
            $('#faderFront').fadeOut(fadeSpeed);
        
            i++;
        
            if ( i == imageArray.length ) {
                i=0;
            }
        
            setTimeout(function() {
                fadeInFront(i);
            },timeout);
        
        }
        
        function preload(arrayOfImages) {
        
            $(arrayOfImages).each(function() {
        
                $('<img/>')[0].src = this;
        
            });
        
        }
    
        $(document).ready(function(){
    
            preload(imageArray);
    
            setTimeout(function() {
                fadeOutFront(0);
            }, timeout);
    
        });
    
    </script>
    
    <style type="text/css">
    
        .imageContainer {
            width: 700px;
            height: 400px;
            position: relative;
        }
        
        #faderBack,
        #faderFront,
        #inFrontOfBoth {
            position: absolute;
            top: 0; left: 0;
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            width: 100%;
            height: 100%;
        }
    
    </style>
       
    <div id="container">
    
        <div class="imageContainer">
            
            <div id="faderBack"></div>
            <div id="faderFront" style="background-image: url('1.jpg');"></div>
            <div id="inFrontOfBoth">Hello World</div>
    
        </div>
    
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 2018-09-24
      • 2012-07-14
      • 2021-03-16
      相关资源
      最近更新 更多