Jquery height()滚动问题
所以我有一个很大的长文本块,我试图使用 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>
每当用户尝试显示/隐藏内容时,浏览器都会自动滚动到页面顶部。当用户单击显示/隐藏链接时,防止滚动位置发生变化的最佳方法是什么?
So I have this big long block of text that I am trying to hide/reveal using jquery to change the css height for the text's containing div.
<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>
Whenever the user tries to reveal/hide the content, the browser automatically scrolls to the top of the page. What is the best method to keep the scroll position from changing when the user clicks the reveal/hide links?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的链接上可能有
href="#"
。这将使链接将您带到页面顶部。尝试将其更改为href="javascript:void(0)"
或其他内容。You probably have
href="#"
on your links. This will cause the link to bring you to the top of the page. Try changing that tohref="javascript:void(0)"
or something.您是否尝试过存储
scrollTop
值并恢复它?最重要的是,如果您的链接使用#
作为其href
,则需要在点击函数中return false;
。Have you tried storing the
scrollTop
value and restoring it? On top of that, if your links are using#
as theirhref
, you need toreturn false;
in your click functions.假设您确实有 href="#"。你不需要 javascript:void(0);或者scrollTop废话。只需在 onclick 处理程序末尾返回 false 即可。
返回 false 将停止传播并取消单击带有哈希的链接时发生的正常事件,即,如果名称为空,它将跳转到指定的锚点或页面顶部。
Assuming you indeed have href="#". You don't need javascript:void(0); or scrollTop nonsense. Just return false at the end of your onclick handler(s).
Returning false will stop propagation and cancel the normal event that occurs for clicking on links with a hash, ie it will jump to the named anchor or the top of the page in case of an empty name.
go_to_here() 函数将 center_slide_down 滚动到 < div id="此处">
go_to_here() function scroll center_slide_down to < div id="here">
使用返回 false
Use return false