Липкий div в CSS - PullRequest
       0

Липкий div в CSS

1 голос
/ 23 марта 2020

Я бы хотел, чтобы div был нажат (см. «Поиск при перемещении карты» на снимке экрана), чтобы он плавал над картой, но чтобы при использовании любых констант для margin-top или top, тогда это относительно родительского div (map), а не окна браузера.

Как я могу это сделать? Ссылка на сайт Я пытался добавить position: relative; к родителю #map, но вот что я получаю (карта скрыта):

enter image description here

Это мой CSS код:

#map {
    #searchCheckboxContainer {
        position: absolute;
        display: inline-table;
        white-space: nowrap;
        margin-top: 24px;  // sure, this works, but it's 24px *from the browser window*
        top: 0px;          // any way to make it relative to the parent div (map)?
        left: 50%;
        transform: translateX(-50%);
        background: rgb(255, 255, 255) !important;
    }

    #searchCheckboxContainer {
        height: 40px;
    }
}

HTML:

<div id="map" v-cloak>
      <div id="searchCheckboxContainer">
        <div id="searchCheckbox">
            <input id="checkbox" class="form-check-input" type="checkbox" value="" id="defaultCheck1">
            <label class="form-check-label" for="defaultCheck1">
              Search as I move the map
            </label>
        </div>
      </div>
      <div id="mapid"></div>
</div>

enter image description here

Ответы [ 2 ]

2 голосов
/ 23 марта 2020

Добавление в качестве ответа, чтобы избежать шума комментариев:

Все дочерние элементы #map позиционируются как абсолютные. Таким образом, по существу они не входят в обычный поток документов и не влияют на высоту #map div.

. Вам необходимо добавить:

position: relative;
height: 100vh (or whatever)

К вашему #map div.

Затем, к вашему #searchCheckboxContainer, добавьте z-index: 100 //could be anything but that worked

Это поместит поле над картой.

1 голос
/ 24 марта 2020

Полагаю, вам нужно, чтобы оно выглядело так:

enter image description here

В этом случае вам необходимо изменить следующее:

#map {
    position: relative;
    height: calc(100vh - 86px); // The height of header on mobile and you need to add responsive media queries to handle it.
}

#map #searchCheckboxContainer{
    position: absolute;
    display: inline-table;
    white-space: nowrap;
    margin-top: 0;
    bottom: 0;
    left: 0;
    top: auto;
    transform: none;
    background: #ffffff !important;
    z-index: 2;
    width: 100%;
    padding: 15px;
}

#searchCheckbox .form-check-input{
    position: relative;
    margin-top: 0;
    margin-left: 0;
}

#map #mapid{
    height: 100%;
    width: 100%;
    position: absolute;
    background-color: yellow;
    z-index: 1;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...