【问题标题】:'Back to top' on scroll top at footer页脚滚动顶部的“返回顶部”
【发布时间】:2015-03-03 00:39:07
【问题描述】:

使用我在周围看到的一些示例,当您向下滚动页面时,我有一个 返回顶部按钮 可以显示和工作,但是有没有办法让按钮粘在屏幕底部,直到到达页脚,它将粘在页脚顶部?

这是我目前的代码:

<script type="text/javascript" defer="defer">
    $(window).scroll(function() {
        if ($(this).scrollTop()) {
            $("#toTop").fadeIn();
        } else {
            $("#toTop").fadeOut();
        }
    });


    $("#toTop").click(function() {
        $("html, body").animate({scrollTop: 0}, 1000);
    });
</script>

<style type="text/css">
    .backtotop_button_wrap {width:100%; background:white; height:auto;}
    .backtotop_button_height {width:100%; height:55px;}
    #toTop {display:none; position: fixed; right:0; bottom:0; width:100px; height:auto; background:white; float:right; padding:10px; text-align:center; border:1px solid black; line-height:12px;}
    #footer {width:100%; height:500px; color:white; text-align:center; background:#313131; border-top:1px solid black;}
</style>

<div class="backtotop_button_wrap">
    <div class="backtotop_button_height">
        <div id="toTop">^<br />Back to the Top</div>
    </div>
</div>
<div id="footer">
Footer
</div>

我在这里也做了一个 Jfiddle:http://jsfiddle.net/0Lge6wqq/

【问题讨论】:

  • 您需要回到顶部才能始终显示
  • 你的意思是让它停在页脚吗?或者也看到背对顶框?因为在 jfiddle 中,如果您向下滚动预览平面,它应该沿底部显示框,这是我想要它做的,但随后在页脚的开头停止滚动。
  • 知道了。所以页脚应该推上面的框?

标签: jquery html css footer


【解决方案1】:

将#toTop 的html 位置更改为#footer。 当窗口达到页脚的高度时。 #toTop 从固定变为相对。

     if($(window).scrollTop() + $(window).height() < $(document).height() - $("#footer").height()){
            $('#toTop').css("position","fixed");    //resetting it
            $('#toTop').css("bottom","0"); //resetting it
}
else {
            $('#toTop').css("position","relative"); // make it related
            $('#toTop').css("bottom","60px"); // 60 px, height of #toTop
 }

jsfiddle

http://jsfiddle.net/0Lge6wqq/4/

【讨论】:

  • 它工作正常,但最好切换类而不是切换样式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 2013-03-25
  • 1970-01-01
  • 2023-04-07
  • 2013-01-30
  • 2012-02-15
  • 2011-09-06
相关资源
最近更新 更多