所以我有一个很大很长的文本块,我试图用jquery来改变文本包含div的css高度来隐藏/显示它。
<script>
$(document).ready(function() {
$('#center_slide_down_link').click(function() {
$('.center_slide_down').animate({
height: 1200
}, 1000 );
$(this).hide();
$('#center_slide_up_link').show();
});
$('#center_slide_up_link').click(function() {
$('.center_slide_down').animate({
height: 450
}, 1000 );
$(this).hide();
$('#center_slide_down_link').show();
});
});
</script>每当用户试图显示/隐藏内容时,浏览器都会自动滚动到页面顶部。当用户单击显示/隐藏链接时,防止滚动位置更改的最佳方法是什么?
发布于 2011-04-25 02:36:00
你的链接上可能有href="#"。这将导致该链接将您带到页面的顶部。尝试将其更改为href="javascript:void(0)"或其他名称。
发布于 2011-04-25 02:36:52
您是否尝试过存储scrollTop值并恢复它?最重要的是,如果你的链接使用#作为点击的href,你需要在点击函数中加入return false;。
发布于 2011-04-25 02:37:21
<script>
$(document).ready(function()
{
$('#center_slide_down_link').click(function()
{
$('.center_slide_down').slideUp('fast',function()
{
$(this).css('height','1200px');
$(this).slideDown('fast');
});
});
$('#center_slide_up_link').click(function()
{
$('.center_slide_down').slideUp('fast',function()
{
$(this).css('height','450px');
$(this).slideDown('fast');
});
});
function go_to_here()
{
$(".center_slide_down").animate( { scrollTop: $('#here').offset().top } , 1000 );
}
});
</script>
<div class="center_slide_down">
Some Text<br />
Some Text<br />
<div id="here">Some Text</div>
</div>go_to_here()函数将center_slide_down滚动到< div id=“此处”>
https://stackoverflow.com/questions/5772404
复制相似问题