Я хочу сделать 2 липких заголовка div вверху при прокрутке страницы.
FIRST DIV
попадает в палка только на некоторое время, пока SECOND DIV
не появится в определенной точке экрана.
Когда SECOND DIV
при прокрутке вверх достигает половины страницы (например), FIRST DIV
перестает быть липкий, отталкивается, а затем заменяется SECOND DIV
как следующий липкий элемент вверху.
Мне удалось заставить первый div прилипнуть к вершине с помощью моего CSS, но я понятия не имею, что делать с second div
.
.body {
margin: 0;
}
h3 {
background-color: #E9BDF5;
width: 100%;
}
.firstDiv {
text-align: center;
background-color: yellow;
width: 100%;
height: 70px;
position: sticky;
top: 0;
}
.secondDiv {
text-align: center;
background-color: yellow;
width: 100%;
height: 60px;
}
<h3>These text don't stick</h3>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and
</p>
<div class = "firstDiv">
<h2>First Div - Becomes sticky (temporarily)</h2>
</div>
<h3>These text don't stick</h3>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<div class = "secondDiv">
<h2>Second Div - Becomes the sticky header</h2>
</div>
<h3>These text don't stick</h3>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
Любые советы по этому поводу будут высоко оценены. Заранее благодарю вас за вашу любезную помощь ??♥️
Попробуйте это: stackoverflow.com/questions/26663602/…
Хитрость заключается в использовании обертки div, которая действует как граница для position: sticky;
html {
font-size: 150%;
}
.firstDiv,
.secondDiv {
position: sticky;
top: 0;
background-color: white;
}
<h3>These text don't stick</h3>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and
</p>
<div>
<div class = "firstDiv">
<h2>First Div - Becomes sticky (temporarily)</h2>
</div>
<h3>These text don't stick</h3>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
</div>
<div>
<div class = "secondDiv">
<h2>Second Div - Becomes the sticky header</h2>
</div>
<h3>These text don't stick</h3>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
<p>
This paragraph will scroll up and will not stick at the top
</p>
</div>
Оууу, ты лучший @christiansany. Просто и эффективно. Ваше решение только что спасло мою проблему. Большое тебе спасибо
Рад помочь :D
Можете ли вы показать мне, как, пожалуйста?