2013-05-30 4 views
0

해당 항목을 클릭하면 내용을 숨기고 표시하는 테이블 세트가 있습니다. 또한 scrollTop을 사용하여 클릭 된 각 테이블의 맨 위에 표시하여 내용을 더 잘 보여줍니다. 지금까지 아무런 문제가 없습니다 ... 기본적으로 첫 번째 표의 내용을 표시하고 싶지만이 경우 페이지가로드 될 때 scrollTop을 방지하고 싶습니다. 내가 어떻게 해?jQuery : 시뮬레이션 클릭() 또는 트리거 ('클릭')시 스크롤 탑 방지

$(document).ready(function() { 

$('table tr').nextAll().hide(); 
var $button = $('table th').css('cursor', 'pointer'); 
$button.click(function() { 
$(this).parent().nextUntil('table th').toggle(); 
$('html, body').animate({ 
    scrollTop: $(this).offset().top 
    }, 500); 
}); 
$('table th:eq(0)').trigger('click', animate(false)); 
}); 

.trigger()에는이 첫 번째 스크롤을 방지하는 데 도움이되는 코드가 있어야합니까? 도움 주셔서 감사합니다.

는 편집 :

$(document).ready(function() { 

$('table tr').nextAll().hide(); 
var $button = $('table th').css('cursor', 'pointer'); 
$button.click(function() { 
     $(this).parent().nextUntil('table th').toggle(); 
}); 

$('table th:eq(0)').trigger('click'); 

$button.click(function() { 
     $('html, body').animate({ 
     scrollTop: $(this).offset().top 
     }, 500); 
}); 
}); 
+0

이 코드의 예이다 : http://luxurycruisesgalapagos.com/test.html –

답변

1

는 클릭 처리기 내부의 애니메이션 코드를 이동 예를보십시오 : 음, 이벤트를 분할하고, 애니메이션 앞에 위치 트리거()를 갖는 대부분의 솔루션 우아하지만이 작동 듯 이렇게 될하기 위해 애니메이션 코드를 제거하고 트리거 코드를 변경 :

$('table th:eq(0)').click(function() { 
    $('html, body').animate({ 
     scrollTop: $(this).offset().top 
     }, 500); 
    }); 
}); 
+0

안녕! 음, TH를 클릭하면 애니메이션이 정상적으로 작동하지만 실제로는 "$ ('table th : eq (0)'). –