2014-04-01 2 views
1
$('.balloon').animate({top:30} ,1000); 
$('.balloon').animate({top:10} ,1000); 

위 작업을 성공적으로 수행 할 수 있습니다. Plz 좀 봐있다 complete code and execution at fiddle

하지만 위의 일반적인 순위와 함께 일하고 싶습니다. 내가 그것을 제공 할 때 나 TypeError: a.ownerDocument is undefined

$('.balloon').animate({top:$(this).css('top')+20} ,1000); 
$('.balloon').animate({top:$(this).css('top')-20} ,1000); 
+0

'$ (이)'코드에서 당신이 .balloon' 필요'의 부모를 위해입니다 '$ .each' 함수 사용 – Jack

+0

예. 당신이 더 많은 것을 안내 할 수 있다면, 어떻게 말해줘. 나는 그 사이에 나 자신을 시험해 본다. 감사합니다 – Sami

+0

방금 ​​내 대답을 희망이 찾고있어 무엇을 찾고있다;) – Jack

답변

1

가 귀하의 코드를 단순화 할 수있다 오류 : 당신은 정상 위치로 30px을 추가 +=30을 사용할 수 있습니다

$(document).ready(function() { 
    (function foo() { 
     $('.balloon').stop(true).animate({top: "+=30"}, 1000, function() { 
      $(this).stop(true).animate({top: "-=30"}, 1000, foo); 
     }); 
    })(); 
}); 

. -30px와 동일합니다.

JSFIDDLE

+0

그 IIFE에 요점은 없지만, 좋아 보인다. –

+0

@RPM 무슨 뜻인가요? –

+0

'IIFE' = 즉시 호출 된 함수 식 – Jack

0

당신은 $.each JQuery와 기능을 작동 할 필요가

이 예 http://fiddle.jshell.net/cRs9f/1/

$(document).ready(function() 
{ 
    var downs = true; 
    setInterval (function() 
    { 

     $('.balloon').each(function(){ 

      if(downs) 
      { 

       $(this).animate({top:30} ,1000); 
       $(this).animate({top:$(this).css('top')+20} ,1000); 
       downs =false; 

      }else 
      { 
      $(this).animate({top:10} ,1000); 
      $(this).animate({top:$(this).css('top')-20} ,1000); 
      downs =true; 
      } 


     }); 

    },1000); 
}); 
관련 문제