【问题标题】:jquery or javascript div background image changejquery 或 javascript div 背景图像更改
【发布时间】:2012-09-21 18:48:18
【问题描述】:

我在构建的网站中遇到问题。我搜索 google 和 stackoverflow,但找不到任何可以帮助我的东西。我有一个带有内容搜索引擎、文本等的 div……而且我有背景图片。那么你能告诉我如何每隔 2-3 秒或任何我设置的淡入淡出效果更改 div 背景图像,请我是 jquery 或 javascript 的菜鸟,所以请耐心等待。谢谢。

【问题讨论】:

    标签: javascript jquery image html background


    【解决方案1】:

    html代码

        <div id="banner">
    
      <img class="img1" height="251" width="650" alt="img1" src="images/banner1.jpg"/>
    
      <img class="img2" height="251" width="650" alt="img2" src="images/banner2.jpg"/>
    
    </div>
    

    jquery 代码

     jQuery.timer = function (interval, callback)
     {
    
     var interval = interval || 100;
    
    if (!callback)
        return false;
    
    _timer = function (interval, callback) {
        this.stop = function () {
            clearInterval(self.id);
        };
    
        this.internalCallback = function () {
            callback(self);
        };
    
        this.reset = function (val) {
            if (self.id)
                clearInterval(self.id);
    
            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };
    
        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);
    
        var self = this;
    };
    
    return new _timer(interval, callback);
    };
    
    var current_panel = 2;
    var animation_duration = 2500;  
    $.timer(6000, function (timer) {  
        switch(current_panel){  
            case 1:  
               $("#banner .img1").fadeIn(800);
    
               $("#banner .img2").fadeOut(800);
                current_panel = 2; 
            break; 
            case 2: 
                $("#banner .img2").fadeIn(800);
    
                 $("#banner .img1").fadeOut(800);
                current_panel = 1; 
            break; 
    
            timer.reset(12000);  
         }  
     });  
    

    【讨论】:

      【解决方案2】:

      据我所知,没有额外的标记是无法改变具有淡入淡出效果的背景图像的。你没有提供任何代码,所以我做了一个综合示例。

      标记:

      <div class="container">
          <div class="bg">
          </div>
          <div class="content">
              ... 
          </div>
      </div>
      ...
      

      CSS:

      /*
          this is your main container 
      */
      .container {
          width:300px;
          height:300px;
      }
      
      /*
          this one is needed to provide 
          independent background, when it fades
          your content will still be available for seeing
      */
      .bg {
          width:300px;
          height:300px;
          background-color:yellow;
          z-index:0;
      }
      
      /*
          your content, obviously :)
      */
      .content {
          position:relative;
          top:-300px;
          z-index:1;
          padding:5px; /*just for better looking*/
      }
      

      还有一些 jquery:

      //in your case, here must be URLs for images
      var arr = ["yellow", "blue", "green", "red"]; 
      var i = 0;
      
      var intervalID = setInterval(function() {
          //when bg fades, we don't want to see  
          //transparent background                             
          $('.container').css('background-color', arr[i]); 
      
          //make transparent, change background, make visible again
          $('.bg').animate({opacity: 0}, 'slow', function() {
              $(this).css({'background-color': arr[i]}).animate({opacity: 1});
          });
      
          //this can be random
          i++;
          if (i >= arr.length)
              i = 0;
      }, 2000); //change 2000 if you want different time between changes
      
      
      //this one is illustating, how to stop background loop
      //with "clearInterval". You might want to use it on :hover or smth like that
      setTimeout(function(){
          clearInterval(intervalID);
          }, 20000);
      

      这不是很灵活的解决方案,因为您需要知道元素的确切大小,但我认为您不会使用不同大小的图像作为背景。

      附言工作示例 - http://jsfiddle.net/7xVAu/115/

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        您可以从JQuery docs 开始。如果您不理解这些内容,请尝试寻找一些好的javascript or JQuery books。另外codecademy 对初级程序员很有帮助。

        【讨论】:

        • 我曾经听说,如果你给一个人生火,你会让他暖一夜,但如果你让一个人着火,他会暖到他的余生。
        • 我稍后再试,但现在我必须构建它
        • 当我学习 javascript 或 JQuery 的新功能时,我总是从文档开始。它们是权威指南。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-09
        • 1970-01-01
        • 2016-05-06
        • 2020-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多