I have a horizontal page with multiple sections on it. On section 2 I have three images. When I scroll section 2 into view, I want the images to move 50px in the opposite scroll direction.
I have two problems which I can't figure out due to the layout of this page (horizontal instead of vertical):
how to detect when I reach section 2
how to move the images by ~50px in the opposite direction of the scroll and make it as smooth as possible
I use this code to figure out the direction of the scroll
var $scrollWrapper = $('.scroll_wrapper');
var $scrollBtn = $('#scrollBtn');
var $scrollOuterWrapper = $('.scroll_outer-wrapper');
$scrollWrapper.scrollTop(0)
$('#scrollBtn').on('click', function() {
$scrollWrapper.scrollTop($scrollWrapper.scrollTop() + 100)
});
var lastScrollTop = 0;
$scrollOuterWrapper.on('scroll', function() {
var st = $(this).scrollTop();
var endOfWrapper = $(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight;
if (st > lastScrollTop){
// down scroll
console.log('downscroll');
// parallax elements - move to front
// ??
$moveElement = $('.move-on-scroll');
$moveElement.each(function() {
var firstTop = $(this).offset().top;
var wrapperScrollTop = $scrollOuterWrapper.scrollTop();
var shiftDistance = (firstTop - wrapperScrollTop)*0.02;
$(this).css("transform","translateX("+shiftDistance+"px)");
});
} else {
// upscroll
console.log('upscroll');
// parallax elements - move to back
// ??
}
lastScrollTop = st;
});
Here's also a snippet:
var $scrollWrapper = $('.scroll_wrapper');
var $scrollBtn = $('#scrollBtn');
var $scrollOuterWrapper = $('.scroll_outer-wrapper');
$scrollWrapper.scrollTop(0)
$('#scrollBtn').on('click', function() {
$scrollWrapper.scrollTop($scrollWrapper.scrollTop() + 100)
});
var lastScrollTop = 0;
$scrollOuterWrapper.on('scroll', function() {
var st = $(this).scrollTop();
var endOfWrapper = $(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight;
if (st > lastScrollTop){
// down scroll
console.log('downscroll');
// parallax elements - move to front
// ??
$moveElement = $('.move-on-scroll');
$moveElement.each(function() {
var firstTop = $(this).offset().top;
var wrapperScrollTop = $scrollOuterWrapper.scrollTop();
var shiftDistance = (firstTop - wrapperScrollTop)*0.2;
$(this).css("transform","translateX("+shiftDistance+"px)");
});
} else {
// upscroll
console.log('upscroll');
// parallax elements - move to back
// ??
}
lastScrollTop = st;
});
.scroll_outer-wrapper {
width: 100vh;
height: 100vw;
transform: rotate(-90deg) translateX(-100vh);
transform-origin: top left;
overflow-y: scroll;
overflow-x: hidden;
position: absolute;
}
.scroll_wrapper {
display: flex;
flex-direction: row;
width: 400vw;
transform: rotate(90deg) translateY(-100vh);
transform-origin: top left;
transition: transform .5s ease;
}
.scroll_section {
width: 100vw;
height: 100vh;
}
.scroll_section.one{background: black; color: white;}
.scroll_section.two{background: white; color: black;}
.scroll_section.three{background: black; color: white;}
.scroll_section.four{background: pink; color: black;}
#scrollBtn {
position: absolute;
bottom: 20px;
right: 20px;
background-color: darkblue;
color: white;
border: none;
width: 80px;
height: 80px;
border-radius: 50%;
text-transform: uppercase;
font-size: 12px;
line-height: 20px;
cursor: pointer;
}
.move-on-scroll {
width: 150px;
height: 150px;
border: 2px solid red;
margin: 0 20px;
}
.move-on-scroll img {
width: 100%;
height: 100%;
object-fit: cover;
}
.two_inner {
display: flex;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="scroll_outer-wrapper">
<div class="scroll_wrapper">
<section class="scroll_section one">
<h2>section 1</h2>
</section>
<section class="scroll_section two">
<h2>section 2</h2>
<div class="scroll_section two two_inner">
<div class="move-on-scroll">
<img src="https://images.unsplash.com/photo-1590336751349-f65720fee481?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=80" >
</div>
<div class="move-on-scroll">
<img src="https://images.unsplash.com/photo-1590336751349-f65720fee481?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=80" >
</div>
<div class="move-on-scroll">
<img src="https://images.unsplash.com/photo-1590336751349-f65720fee481?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=80" >
</div>
</div>
</section>
<section class="scroll_section three">
<h2>section 3</h2>
</section>
<section class="scroll_section four">
<h2>section 4</h2>
</section>
</div>
</div>
<button id="scrollBtn">Click to Scroll</button>