2013-05-30 80 views
0

我有一組表格,隱藏和顯示內容時點擊他們的th。此外,我使用scrollTop觸及每個點擊表的頂部以更好地顯示其內容。到目前爲止沒有問題... 默認情況下,我想讓第一個表顯示它的內容,但是我想在這種情況下防止頁面加載時的scrollTop。我該怎麼做?jQuery:防止scrollTop模擬點擊()或觸發('點擊')

$(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)')。click()」不滾動給出用戶沒有實際輸入 –