2012-11-08 13 views
0

텍스트 레이블이 클릭 한 요소와 일치하는 요소의 색인 번호는 어떻게 결정합니까?요소로 스크롤

나는 목록을 가지고 :

LI들 중 하나가 내 화면을 클릭 한 후 페이지의 일치 된 DIV 중 하나를 아래로 스크롤 할 필요가있다
<ul> 
    <li data-cont="item one">this is one</li> 
    <li data-cont="item 2">this is second</li> 
    <li data-cont="one more">this is another one</li> 
</ul> 

... 

<div>item one</div> 

<div>item 2</div> 

<div>one more</div> 

. 사업부의 인덱스를 얻기

$('#li').click(function(){ 
    var scrollNow = $(this).attr('data-cont'); 
    $('html, body').animate({ 
     scrollTop: $( 

     // here I need to scroll down to the item by index... 

     ).offset().top 
    }, 500); 
}); 
+0

가능한 중복 http://stackoverflow.com/questions/6677035/jquery-scroll-을 할 필요가 요소 간) –

답변

1

쉽게 :

var index = $('div').filter(function(){ 
    return $(this).text().trim()==scrollNow} 
).index(); 

를 (너무 많은 DIV 일치 수 있기 때문에 내가 :contains를 사용하지 않습니다)하지만 난 당신이 원하는 이유를 실패 이 지수. 당신은 사업부로 이동하려면

, 당신은 단지

var div = $('div').filter(function(){ 
    return $(this).text().trim()==scrollNow} 
); 
$('html, body').animate({ 
    scrollTop: div.offset().top 
}, 500); 
[요소에 jQuery를 스크롤] (의
관련 문제