Ответ 1
Вот рабочее решение в скрипке.
Описание
- Разместите заголовок в фоновом режиме
- Установите высоту тела для высоты заголовка плюс высота содержимого
- Поместите содержимое в обертку в нижней части тела:
position: absolute; bottom: 0
- Поместите содержимое в верхнюю часть его обертки:
position: absolute; top: 0
- Установите высоту обертки для соответствия высоте содержимого
- Когда верхняя часть обертки содержимого прокручивается в нижней части видимой части, измените ее положение на фиксированное в этой точке:
position: fixed; top: bottom of the visible part
- Если оболочка содержимого
position: fixed
, сдвиньте содержимое внутри своей оболочки, чтобы продолжить прокрутку
Большинство этих значений заданы в JavaScript для получения фактических вычисленных значений, а не процентов. Это также позволяет пересчитать размер окна.
код
HTML
<div id="header">
<video id="bgvid" autoplay loop muted>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
<source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
</video>
</div>
<div id="content_wrapper">
<div id="content">
</div>
</div>
CSS
* {
margin:0;
}
html, body {
position: relative;
width:100%;
height:100%;
}
#header {
position: fixed;
top: 0;
left: 0;
z-index: -1000;
width:100%;
height:100%;
}
#bgvid {
width:auto;
height:auto;
min-width:100%;
min-height:100%;
}
#content_wrapper {
position: absolute;
left: 0px;
bottom: 0px;
width: 100%;
overflow: hidden;
z-index: -10;
}
#content {
background: white;
position: absolute;
left: 0px;
top: 0px;
}
JavaScript (где происходит настоящая магия)
var $window = $(window);
var $body = $('body');
var $contentWrapper = $('#content_wrapper');
var $content = $('#content');
var minHeaderHeight = 250; // the height of the "visible part"
var lastWindowHeight = $window.height(); // save window height to calculate difference in height on resize
checkScroll(); // make sure scroll and all heights are correct on first load
stickyTop(); // make sure sticky top is used if needed on first load
$window.resize(function() {
checkScroll();
stickyTop();
});
$window.scroll(function() {
stickyTop();
});
function checkScroll() {
var newWindowHeight = $window.height();
var windowHeightDif = newWindowHeight - lastWindowHeight;
lastWindowHeight = newWindowHeight; // save current height for next call
var contentHeight = $content.height();
$contentWrapper.height(contentHeight); // make sure wrapper will show entire content
$body.height(newWindowHeight + contentHeight); // allow content to be scrolled off the screen by
// pushing content down by the amount of window height
var windowScrollTop = $window.scrollTop();
if (windowScrollTop > 0) { // don't scroll if at top to avoid video getting covered
$window.scrollTop(windowScrollTop + windowHeightDif); // scroll by window height difference to keep content
// in the same position on window resize
}
}
function stickyTop() {
var windowScrollTop = $window.scrollTop();
var maxScroll = ($window.height() - minHeaderHeight);
if (windowScrollTop >= maxScroll) {
$contentWrapper.css('position', 'fixed').css('top', minHeaderHeight); // stop wrapper top at bottom of header
} else {
$contentWrapper.css('position', 'absolute').css('top', ''); // allow regular scrolling
}
if ($contentWrapper.css('position') === 'fixed') { // if wrapper is fixed,
$content.css('top', -(windowScrollTop - maxScroll)); // continue scrolling by shifting content up
} else {
$content.css('top', 0); // make sure content is lined up with wrapper
}
}