2013-05-30 50 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()”不滚动给出用户没有实际输入 –