jquery - 页脚滚动顶部的“返回顶部”

标签 jquery html css footer

使用一些我见过的例子,当你向下滚动页面时,我有一个回到顶部按钮出现和工作,但是有没有办法让按钮坚持屏幕底部,直到到达页脚,它会粘在页脚顶部吗?

到目前为止,这是我的代码:

<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/

最佳答案

将#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/

关于jquery - 页脚滚动顶部的“返回顶部”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27780145/

相关文章:

python - 响应不为空时,scrapy xpath 返回空列表

javascript - Chrome 中未应用 Owl Carousel 动画

javascript - JQuery - 当用户将它们放在彼此身上时,将两个或多个 droppables 组合在 div 中

javascript - 获取可见 Div 的 id,所有方法均失败

javascript - 如何在我的 Web 应用程序中检索和显示 Outlook/Exchange 日历事件?

javascript - 如何在jquery中动画后删除属性

javascript - 动态调用Javascript变量

html - 垂直对齐 h1 并保持宽度为 100%

jquery - 带滚动条显示/隐藏 div

javascript - 如何在点击函数中使用 jQuery 改回 insidehtml?