【问题标题】:Create an interval to change image in jQuery?创建一个间隔来改变jQuery中的图像?
【发布时间】:2011-03-12 01:04:59
【问题描述】:

我有一个这样的工作脚本:

jQuery(document).ready(function(){

    $('.video-thumb img').bind('mouseover',function(){
        var new = $(this).attr('src').replace(/default.jpg/,'1.jpg');
        $(this).attr('src',new);
    }).bind('mouseout',function(){
        var default = $(this).attr('src').replace(/[0-9].jpg/,'default.jpg');
        $(this).attr('src',default);
    });

});

是的,你猜对了。它用于更改 YouTube 的缩略图间隔。但是,我不知道如何创建间隔。它现在将缩略图更改为 1.jpg,这是另一个缩略图,但接下来应该在 1 秒内将图像更改为 2.jpg,依此类推。

整个 sn-p 可能应该从头开始编写。建议?

希望你能理解:-D

编辑:我改变了芬兰语的变量名,我不使用它们。就在这个例子中。

玛蒂莱恩

【问题讨论】:

    标签: jquery image timer rotation src


    【解决方案1】:

    newdefault 在 javascript 中是 reserved words。你不能使用它们。

    要创建间隔,您应该使用setInterval

    setInterval(function() {
      // do something
      // ...
    }, 1000); // <- 1000ms = 1s
    

    [See it in action]

    jQuery(document).ready(function() {
    
        var timer, imgsrc, cnt = 0;
    
        $('.video-thumb img').bind('mouseover', function() {
    
          // if there is no timer
          if (!timer) {
    
            var $t = $(this);
    
            // get the image src
            imgsrc = $t.attr('src').replace('default.jpg','');
    
            // start a new timer
            timer = setInterval(function() {
    
              // periodically change the src
              $t.attr('src', imgsrc + (cnt+1) + ".jpg");
    
              // and adjust the counter
              cnt = ( cnt + 1 ) % 3; // 0, 1, 2
    
            }, 1000); // <- 1000ms = 1s
          }
        }).bind('mouseout',function() {
    
          // stop rotation
          if (timer) {
            clearInterval(timer);
            timer = null;
          }
    
          // set the default img
          $(this).attr('src', imgsrc + 'default.jpg');
        });
    });
    

    【讨论】:

      【解决方案2】:

      你可以在这里看到我的答案:

      HTML

      <div class="video-thumb"> 
          <img src="http://i2.ytimg.com/vi/Q1yo3mco40U/default.jpg" />
      </div>
      <div id="counter">0</div>​
      

      JavaScript

      $(document).ready(function() {
      
          var images = [];
      
          images[0] = "http://i2.ytimg.com/vi/Q1yo3mco40U/default.jpg";
          images[1] = "http://i2.ytimg.com/vi/ivmoCcYLrEk/default.jpg";
          images[2] = "http://i3.ytimg.com/vi/f7d8luQ6p2Q/default.jpg";
          images[3] = "http://i1.ytimg.com/vi/XzFmOKNf8sc/default.jpg";
          images[4] = "http://i2.ytimg.com/vi/-2m1e4g2MFM/default.jpg";
          images[5] = "http://i1.ytimg.com/vi/lK2TSYBh7fw/default.jpg";
      
          var loop;
          var i = 0;
      
          var counter = $("#counter");
      
          $('.video-thumb img').mouseover(function() {
              var image = this;
              loop = setInterval(function() {
                  if (i < images.length - 1) {
                      i++;
                      $(image).attr('src',images[i]);
                  } else {
                      i = 0;
                      $(image).attr('src',images[i]);
                  } 
                  counter.html(i);
              }, 1000); 
      
          }).mouseout(function() {
              clearInterval(loop);
              i = 0;
              $(this).attr('src', images[i]);
              counter.html(i);
          });
      
      });
      

      【讨论】:

        【解决方案3】:
         var newImg = $(this).attr('src').replace(/default.jpg/,'1.jpg');
         $(this).attr('src',newImg);
         window.setTimeout(function(){
             newImg = $(this).attr('src').replace(/1.jpg/,'2.jpg');
             $(this).attr('src',newImg);
         },1000);
        

        【讨论】:

          【解决方案4】:

          感谢大家的精彩回答! (Un) 幸运的是我已经通过 jQuery 的 Timers-plugin 创建了这个。这是我的工作代码:

          $j(document).ready(function(){
          
              $j('.video-thumb img').bind('mouseover',function(){
                  var c = 1;
                  $j(this).everyTime(1000,'interval',function(i){
                      $j(this).attr('src',$j(this).attr('src').replace(/(default|[0-9]).jpg/,c+'.jpg'));
                      if(c<3) {
                          c++;
                      } else {
                          c = 1;
                      }
                  });
              }).bind('mouseout',function(){
                  $j(this).attr('src',$j(this).attr('src').replace(/(default|[0-9]).jpg/,'default.jpg'));
                  $j(this).stopTime('interval');
              });
          
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-12-09
            • 2023-03-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多