gpt4 book ai didi

javascript - 选择和关注网页元素不起作用(偏移)

转载 作者:行者123 更新时间:2023-11-28 11:17:09 27 4
gpt4 key购买 nike

我的问题解释起来有点棘手,但无论如何我都会尝试。我有两个水平选项卡,当您单击它们时,会打开一个文本框内容。当他们被点击时,我试图“关注”他们。我在网上找到了很多资料,但除了我在下面显示的这段代码外,没有任何效果:

$(".accordionButton").click(function() {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 500);
});
$(".accordionButtonone").click(function() {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 500);

如果我只点击第一个 accordionButton 就可以了。如果我首先单击第二个 accordionButton,它就会起作用。如果我在单击第二个 accordionButton 后单击第一个 accordionButton 它有效,但如果我在单击第一个 accordionButton 后单击第二个它不起作用:焦点保留在页面底部。我不知道可能是什么问题,我正在尝试使用 animate 函数( jQuery tutorial )和 offset 函数( jQuery tutorial )但是即使只是知道出了什么问题,我也会很感激......

更新:部分解决方案是

 $(".accordionButton").click(function() {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 500);
});
$(".accordionButtonone").click(function() {
$('html, body').scrollTop(0);
});

最佳答案

$(".accordionButton").click(function() {
$('html, body').animate({
scrollTop: $(this).nextAll('div .accordionContent').offset().top
}, 500);
});

$(".accordionButtonone").click(function() {
$('html, body').animate({
scrollTop: $(this).nextAll('div .accordionContentone').offset().top
}, 500);
})

你必须把所有这些都放到一个回调中

$('.accordionContent').slideUp('normal', function(){
$(".accordionButtonone").click(function() {
$('html, body').animate({
scrollTop: $(this).nextAll('div .accordionContentone').offset().top
}, 500);
})
});

关于javascript - 选择和关注网页元素不起作用(偏移),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21703950/

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