将视口底部滚动到元素底部

时间:2016-05-19 15:18:04

标签: javascript jquery html

我正在使用以下jQuery:

$(".car-hub-header-help").click(function() {
    $('html, body').animate({
        scrollTop: $(".footer").offset().top
    }, 2000);
});

目前,这将向下滚动页面,当视口顶部到达元素时,它将停止,但是我希望视口滚动,直到视口底部与我所定位的元素内联。 / p>

到目前为止,我尝试过从上到下进行更改,但是没有用,任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:0)

请试试这个:

$(".car-hub-header-help").click(function() {
     $('html, body').animate({
       scrollTop: $(".footer").offset().top - $(".footer").height() + $(window).height()
     }, 2000);
   });

答案 1 :(得分:0)

这个想法是滚动,直到元素出现在页面底部。

顶部的滚动位置= offset position of the element from the top - current window height + height of element

代码看起来像

$(".car-hub-header-help").click(function() {

    var elTopOffset = $(".footer").offset().top;
    var elHeight = $(".footer").height();
    var windowHeight = $(window).height();

    $('html, body').animate({
        scrollTop: elTopOffset - windowHeight + elHeight
    }, 2000);

});

答案 2 :(得分:0)

document.getElementById("div1").scrollIntoView(false);