Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将视口底部滚动到元素底部_Javascript_Jquery_Html - Fatal编程技术网

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

Javascript 将视口底部滚动到元素底部,javascript,jquery,html,Javascript,Jquery,Html,我正在使用以下jQuery: $(".car-hub-header-help").click(function() { $('html, body').animate({ scrollTop: $(".footer").offset().top }, 2000); }); 此时,这将向下滚动页面,当视口顶部到达元素时,它将停止,但是我希望视口滚动,直到视口底部与我目标的元素对齐 到目前为止,我已经尝试过从上到下进行更改,但没有成功,非常感谢您的帮助。请尝试以下方

我正在使用以下jQuery:

$(".car-hub-header-help").click(function() {
    $('html, body').animate({
        scrollTop: $(".footer").offset().top
    }, 2000);
});
此时,这将向下滚动页面,当视口顶部到达元素时,它将停止,但是我希望视口滚动,直到视口底部与我目标的元素对齐

到目前为止,我已经尝试过从上到下进行更改,但没有成功,非常感谢您的帮助。

请尝试以下方法:

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

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

其想法是滚动直到元素出现在页面底部

从顶部的滚动位置=
元素从顶部的偏移位置-当前窗口高度+元素高度

代码如下所示

$(".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);

});

其想法是滚动直到元素出现在页面底部

从顶部的滚动位置=
元素从顶部的偏移位置-当前窗口高度+元素高度

代码如下所示

$(".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);

});

虽然这个代码片段可以解决这个问题,但它没有解释为什么或者如何回答这个问题。请,因为这确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。标记者/审阅者:虽然这个代码片段可以解决这个问题,但它没有解释为什么或者如何回答这个问题。请,因为这确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。标记者/审阅者: