简体   繁体   English

将元素停靠在div的底部以及屏幕的底部

[英]Dock an element to the bottom of a div as well to the bottom of the screen

To be honest, I don't really know how to explain my problem. 老实说,我真的不知道如何解释我的问题。 I'm trying to dock an element to the bottom of a div (which works great), but it should also dock to the bottom of the screen if the bottom of the div is below the bottom of the screen. 我正在尝试将元素停靠在div的底部(效果很好),但是如果div的底部在屏幕底部以下,它也应该停靠在屏幕的底部。

Example: 例:

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

How I'd like it to behave: 我希望它如何表现:

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

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

Is there a pure CSS solution or do I need JavaScript for that? 是否有纯CSS解决方案,或者我需要JavaScript? I'm using Bootstrap and jQuery. 我正在使用Bootstrap和jQuery。

What I've got so far: 到目前为止,我得到的是:

#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;
}

I believe I have an idea to guide you in the right direction. 我相信我有一个指导您正确方向的想法。 Here is a jsFiddle I constructed to illustrate a container being docked at the bottom of the screen, but docking to the bottom of a DIV when you scroll beyond the DIV. 这是我构造的jsFiddle,用于说明停靠在屏幕底部的容器,但是当您滚动到DIV之外时,停靠在DIV的底部。 For your convenience I'll also specify the HTML, Javascript and CSS below: 为了方便起见,我还将在下面指定HTML,Javascript和CSS:

HTML 的HTML

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

CSS 的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 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 });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM