Simple one page scroll example
by Alexey Zuev
HTML
<div class="pages">
<div class="page current">
<div class="page__content">Page 1</div>
</div>
<div class="page">
<div class="page__content">Page 2</div>
</div>
<div class="page">
<div class="page__content">Page 3</div>
</div>
<div class="page">
<div class="page__content">Page 4</div>
</div>
<div class="page">
<div class="page__content">The end</div>
</div>
</div>
CSS
html,
body,
.pages {
height: 100%;
}
body {
background: #000;
margin: 0;
overflow: hidden;
}
.page {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: transform .8s, opacity .8s;
will-change: transform, opacity;
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: cover;
transform: translateY(100%);
z-index: 1;
}
.page:after {
content:"";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .2);
}
.page.current {
z-index: 1;
transform: translateY(0);
}
.page.top {
opacity: 0;
transform: scale(.9) translateY(0);
}
.page__content {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
font: bold 3rem/1 Sans-serif;
}
.page:nth-child(1) {
background-image: url(http://wallpaperbeta.com/wallpaper_800x600/leopard_snow_night_animals_800x600_hd-wallpaper-233959.jpg);
}
.page:nth-child(2) {
background-image: url(http://red-cat23.ru/wp-content/uploads/2013/11/avstriya3.jpg);
}
.page:nth-child(3) {
background-image: url(http://wallpaperbeta.com/wallpaper_800x600/winter_snow_night_shop_trees_park_lantern_800x600_hd-wallpaper-170998.jpg);
}
.page:nth-child(4) {
background-image: url(http://f.mypage.ru/04aff0eeee1f6a01f93c93f347015ddd_fb4b653e8460e8ce87250931f9979ce6.jpg);
}
.page:nth-child(5) {
background-image: url(http://cdn.allwallpaper.in/wallpapers/800x600/14291/paintings-clouds-snow-night-moon-houses-couple-artwork-800x600-wallpaper.jpg);
}
JavaScript
$(function () {
var $pages = $('.page'),
pagesCount = $pages.length,
current = 0,
animating = false;
function animate(page) {
animating = true;
if (page - current > 0) {
$pages.eq(current).addClass('top');
} else {
$pages.eq(page).removeClass('top');
}
$pages.eq(current).removeClass('current');
$pages.eq(page).addClass('current');
current = page;
setTimeout(function () {
animating = false;
}, 800);
}
function up() {
if (current > 0) {
animate(current - 1);
}
}
function down() {
if (current < pagesCount - 1) {
animate(current + 1);
}
}
$(document).on("mousewheel DOMMouseScroll", function (e) {
if (animating) return;
if (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
up();
} else {
down();
}
});
});