【问题标题】:Scroll bottom of viewport to bottom of element将视口底部滚动到元素底部
【发布时间】:2016-05-19 15:18:03
【问题描述】:

我正在使用以下 jQuery:

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

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

到目前为止,我已经尝试从上到下更改,但没有奏效,任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

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

    从顶部的滚动位置 = 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】:

      请试试这个:

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

      【讨论】:

        【解决方案3】:
        document.getElementById("div1").scrollIntoView(false);
        

        【讨论】:

        猜你喜欢
        • 2023-04-06
        • 2020-03-09
        • 2015-08-12
        • 1970-01-01
        • 2021-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多