I am trying to create a simple, "one-page" scroll.
the goal is to scroll window.innerHeight on scroll - just like fullPage.js and simillar libraries do.
- lastScrollTop is for determining the direction of scroll.
- wh is for adding and subtracting the window.innerHeight from the current ScrollTop.
The problem is, that once you scroll (once), it scrolls to the very bottom. My attempt to solve this was to create var A, which is turned to 1 when the scroll animation is going, doesn't seem to be working.
var lastScrollTop = 0;
var a = 0;
var wh = 0;
$(window).scroll(function () {
var st = $(window).scrollTop();
if (a == 0) {
a = 1;
if (st > lastScrollTop) {
/* SCROLLING DOWN */
wh = $(window).scrollTop() + window.innerHeight;
$("html, body").stop().animate({scrollTop: wh}, 700, function () {
a = 0;
});
} else {
/* SCROLLING UP */
wh = $(window).scrollTop() - window.innerHeight;
$("html, body").stop().animate({scrollTop: wh}, 700, function () {
a = 0;
});
}
lastScrollTop = st;
}
});
body {
margin: 0;
}
p {
margin: 0;
font-size: 70px;
color: #fff;
}
.section {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section" style="background:rgb(100,100,255)">
<p>Blue</p>
</div>
<div class="section" style="background:rgb(60,150,60)">
<p>Green</p>
</div>
<div class="section" style="background:rgb(180,30,30)">
<p>Red</p>
</div>
<div class="section" style="background:rgb(80,0,0)">
<p>Brown</p>
</div>
<div class="section" style="background:rgb(0,0,0)">
<p>Black</p>
</div>