2011-04-24 4 views
2

다음 순서로 정렬되지 않은 목록의 각 행을 페이드 아웃하는 다음 함수가 있습니다. 지금은 #skills_hints에서 작동하지만 다른 #named_hints에 사용하고 싶습니다. 코드 전체를 코드로 복사하여 붙여 넣지 않고 여러 개의 순서가 지정되지 않은 목록에이 코드를 재사용 할 수있는 방법이 있습니까?jQuery 함수를 반복하지 않고 쉽게 재사용 할 수 있습니다.

$(document).ready(function(){ 
    $('#skills_hints .hint'); 
    setInterval(function(){ 
     $('#skills_hints .hint').filter(':visible').fadeOut(800,function(){ 
      if($(this).next('li.hint').size()){ 
       $(this).next().fadeIn(800); 
      } 
      else{ 
       $('#skills_hints .hint').eq(0).fadeIn(800); 
      } 
     }); 
    },7000);  
}); 

답변

1

다음은 정확히 무엇을 할 것 인 당신이 게시하고 작은 동전 기능 (이름을 변경해야으로 코드를 변환 할 수 있습니다 이것은 분명히) 다른 이드를 반복해서 호출 할 수 있습니다.

function doit(id){ 
    $('#'+id+' .hint'); 
    setInterval(function(){ 
     $('#'+id+' .hint').filter(':visible').fadeOut(800,function(){ 
      if($(this).next('li.hint').size()){ 
       $(this).next().fadeIn(800); 
      } 
      else{ 
       $('#'+id+' .hint').eq(0).fadeIn(800); 
      } 
     }); 
    },7000);  
} 

$(function(){ 
    doit('skills_hints'); 
}) 
+0

절대적으로 완벽한 등, 경우에 당신이 콜백으로 사용하기 위해 별도의 기능을 필요 폐쇄로이 돌고있다. 고맙습니다. – stewart715

0

당신은 jQuery를 플러그인 (http://docs.jquery.com/Plugin)

1

좋은 트릭은

function doit(id_name){ 
    var id = '#'+id_name + '.hint'; 
    return (function(){ 
     $(id); 
     setInterval(function(){ 
      $(id).filter(':visible').fadeOut(800,function(){ 
       if($(this).next('li.hint').size()){ 
        $(this).next().fadeIn(800); 
       } 
       else{ 
        $(id).eq(0).fadeIn(800); 
       } 
      } 
     },7000); 
    }) 
} 

//examples 
doit_skills = doit('skills_hint'); 
doit(); 
doit_foo = doit('foo'); 
doit_foo() 

names = ['a', 'b']; 
for(var i=0; i<names.length; i++){ 
    doit(names[i])(); 
} 
+0

도움이됩니다. 고맙습니다. – stewart715

관련 문제