2014-02-18 4 views
0

div의 배경 이미지를 setInvterval 내에서 변경하고 싶지만 방법을 파악할 수 없습니다.배경 이미지가 변경되지 않습니다

$('.thumbnail').on({ 
     'mouseenter': function(){ 
      var thumbs = $(this).attr('data-num-thumbs'); 
      var thumb_url = $(this).css('background-image'); 
      console.log(thumb_url); 
      var i = 1; 
      setInterval(function(){ 
       var new_thumb_url = thumb_url.replace(/\d{1,2}\.jpg/gi, i + '.jpg'); 
       console.log(new_thumb_url); 
       $(this).css('background-image', new_thumb_url); 
       i++; 
       if(i > 10) i = 1; 
      }, 3000); 
     }); 
}); 

div이 같은 모습입니다 : 여기에 코드입니다 :) 코딩 그것은 setInterval() 내부 $(this)과 같은

<div data-num-thumbs="17" class="thumbnail" style="background: url('http://site/img/11.jpg') no-repeat;"></div> 
+2

'setInterval''$ (this)'내부에 뭔가있을 수 있습니다. 당신이 기대할 수있는 것보다 다른 것. – putvande

+0

네, 맞습니다. 그렇다면 어떻게 setInterval 함수에 인수를 전달할 수 있습니까? –

+0

현재 코드가 어떻게됩니까? – dreamweiver

답변

1

뭔가 다른 매핑은, 노력이

$('.thumbnail').on({ 
    'mouseenter': function(){ 
     $this=$(this); //store $(this) in a local var 
     var thumbs = $(this).attr('data-num-thumbs'); 
     var thumb_url = $(this).css('background-image'); 
     console.log(thumb_url); 
     var i = 1; 
     setInterval(function(){ 
      var new_thumb_url = thumb_url.replace(/\d{1,2}\.jpg/gi, i + '.jpg'); 
      console.log(new_thumb_url); 
      //$(this).css('background-image', new_thumb_url); 
      $this.css('background-image', new_thumb_url); 
      i++; 
      if(i > 10) i = 1; 
     }, 3000); 
    } 
}); 

해피

+0

또한')'을 많이 사용합니다. (두 번째 줄부터 마지막 ​​줄까지) – putvande

+0

@putvande : 그 형제님께 감사드립니다. – dreamweiver

관련 문제