gpt4 book ai didi

javascript - 滚动到通过 jquery 切换打开的 div 的底部

转载 作者:行者123 更新时间:2023-11-28 01:49:46 24 4
gpt4 key购买 nike

我尝试滚动到通过 jquery 切换打开的 Div 底部。

我尝试过这样的:

    $(document).ready(
function() {
$(".menuitem").click(function() {
$('#'+$(this).attr("id")+'sub').toggle(1000);
$('html, body').animate({"scrollTop": $('#'+$(this).attr("id")+'sub').offset().top}, 'fast');
}
);

使用scrollTop之后的代码:$('#'+$(this).attr("id")+'sub')我得到了从其扩展的div的正确id切换开关。

问题:我位于 div 的顶部而不是底部,因此用户看不到内容 - 只能看到第一行。我必须以某种方式将 div 的最终大小添加到滚动中,但是如何?

最佳答案

获取元素的高度并将其添加到滚动条中

 $('#'+$(this).attr("id")+'sub').offset().top 
+ $('#'+$(this).attr("id")+'sub').height()

但是,您需要在显示元素后执行此操作,否则它将无法获得正确的高度,因此将其添加到切换回调中,如下所示:

$(".menuitem").click(function() {
$('#'+$(this).attr("id")+'sub').toggle(1000, function() {
$('html, body').animate({
"scrollTop": $('#'+$(this).attr("id")+'sub').offset().top
+ $('#'+$(this).attr("id")+'sub').height()
}, 'fast')
});
}

关于javascript - 滚动到通过 jquery 切换打开的 div 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19826594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com