具有固定标头的scrollIntoView()

时间:2018-09-07 13:00:37

标签: javascript jquery scroll scrollview

这可能是一个简单的问题,但我需要一些帮助。

我有此代码:

var lastitem='';
(function($) {

var allPanels = $('.accordion> dd').hide();

$('.accordion > dt > a').click(function() {
    allPanels.slideUp();
    if ($(this).text() != lastitem) {
        $(this).parent().next().slideDown();
        lastitem = $(this).text();
        $(this).get(0).scrollIntoView({
            behavior: "smooth", // or "auto" or "instant"
            block: "start" // or "end"
        });
    } else {
        lastitem = '';
    };
    return false;
  });

})(jQuery);

我有一个固定的标题,所以当我单击手风琴时它会滚动到视口中,问题出在我的固定标题上。我需要从顶部滚动到视口+ 100px。

类似的东西:

$(this).get(0).scrollIntoView({
    behavior: "smooth", // or "auto" or "instant"
    block: "start + 100px" // or "end"
});

仅此而已,有什么建议吗?谢谢!!

1 个答案:

答案 0 :(得分:1)

根据我从规范中读取的内容,您无法在块上偏移。

scrollIntoView

您可以检查this solution though

相关问题