我的问题解释起来有点棘手,但无论如何我都会尝试。我有两个水平标签,当您单击它们时,打开文本框内容。点击它时我正试着“专注”它们。我在网上找到了很多资料,但除了我在下面显示的代码之外没有任何作用:
$(".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后点击第一个它不起作用:焦点仍然在页面的底部。我不知道可能出现什么问题,我正在尝试使用animate
函数(jQuery tutorial)和offset
函数(jQuery tutorial),但我会感激甚至只知道出了什么问题......
更新:部分解决方案
$(".accordionButton").click(function() {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 500);
});
$(".accordionButtonone").click(function() {
$('html, body').scrollTop(0);
});
答案 0 :(得分:1)
$(".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);
})
});
答案 1 :(得分:0)
解决方案并不优雅,但它有效:
$(".accordionButton").click(function() {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 10);
});
$(".accordionButtonone").click(function() {
$('html, body').scrollTop(458);
});
答案 2 :(得分:-1)
您正在使用偏移向下滚动。删除偏移量,它将停止向下滚动。另外,为什么不写一些利用jquery的'this'的代码,而不是使用单独的选择器
$(this)