javascript - 将元素停靠到 div 的底部以及屏幕底部

标签 javascript jquery html css twitter-bootstrap

老实说,我真的不知道如何解释我的问题。我试图将一个元素停靠到 div 的底部(效果很好),但如果 div 的底部低于屏幕底部,它也应该停靠到屏幕底部。

例子:

<div id="slide" style="height:1000px">
    ...
    <div id="arrow-down">
        <i class="fa fa-arrow-down"></i>
    </div>
</div>
<div id="content">
    ...
</div>

我希望它如何表现:

+---+--------+---+
|   |        |   |
|   |        |   |
|   |        |   |
|   |   VV   |   |    // Docked to screen
+---+--------+---+
    |        |
    +--------+

    +--------+
    |        |
    |        |
    |        |
+---+--------+---+
|   |        |   |
|   |   VV   |   |    // Docked to DIV
|   +--------+   |
|                |
+----------------+

是否有纯 CSS 解决方案,或者我是否需要 JavaScript?我正在使用 Bootstrap 和 jQuery。

到目前为止我得到了什么:

#arrow-down {
    color: #FFF;
    position: absolute;
    bottom: 2%;
    left: 50%;
    z-index: 9999;
    font-size: 14px;
    line-height: 21px;
    font-family: Raleway, Open Sans, Arial, sans-serif;
    opacity: 1;
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
}
#arrow-down:hover {
    color: #F56D45;
    position: absolute;
    bottom: 2%;
    left: 50%;
    z-index: 50;
    font-size: 14px;
    line-height: 21px;
    font-family: Raleway, Open Sans, Arial, sans-serif;
    -webkit-transform: rotateY(720deg);
    -webkit-transform-style: preserve-3d;
    transform: rotateY(720deg);
    transform-style: preserve-3d;
}

最佳答案

我相信我有一个想法可以指导您朝着正确的方向前进。 Here is a jsFiddle我构造来说明一个容器停靠在屏幕底部,但当您滚动到 DIV 之外时停靠到 DIV 的底部。为了您的方便,我还将在下面指定 HTML、Javascript 和 CSS:

HTML

<div class="container">
    <div class="docked">
        I am docked.
    </div>
</div>

CSS

.container
{
    background: #ddd;
    width: 500px;
    height: 1000px;
    margin-bottom: 200px;
    position: relative;
}
.docked
{
    width: 150px;
    background: #fff;
    padding: 10px;
    margin: 10px;
    left: 50%;
    margin-left: -75px;
    margin-top: -20px;
    text-align: center;
    position: fixed;
    bottom: 0;
    box-sizing: border-box;
}

jQuery/Javascript

$(document).ready(function () {
    $(window).scroll(function () {
        var scroll = $(window).scrollTop();
        var windowHeight = $(window).height();
        var totalHeight = scroll + windowHeight;
        var containerHeight = $(".container").height();
        if (totalHeight > containerHeight)
            $(".docked").css({ bottom: totalHeight - containerHeight });
        else
            $(".docked").css({ bottom: 0 });
    });
});

关于javascript - 将元素停靠到 div 的底部以及屏幕底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28031728/

相关文章:

javascript - 获取 "SCRIPT438: Object doesn' t 支持此属性或方法“IE 10 上的错误”

javascript - 遍历 table > tr > td > div 下的每个跨度

javascript - html 中的 Jquery <br> 在展开期间导致连续的文本节点

javascript - 所有浏览器中的 Highcharts 渲染问题

javascript - 如何关闭叠加层并清除叠加层中的数据?

javascript - 为什么这段 JavaScript 代码没有执行?

javascript - 搜索时定位两个字段

jquery - 使用 jQuery Validate 插件进行文件上传验证

html - 网格内的中心图像

css - div 容器 : limit to text going on one line to right