I have developed an example, just below, that basically does what you said in the comments of your doubt. I created a scroll event listener that checks the ranges, which are the offsetTop of sections
of the page, to know in which section
function scrollTo should position scroll
.
// Object usado para regra
var oScroll = {
y: 0,
sec1: document.getElementById('sec1').offsetTop,
sec2: document.getElementById('sec2').offsetTop,
sec3: document.getElementById('sec3').offsetTop,
prevent: true
}
window.addEventListener('scroll', function(e) {
if(this.scrollY < oScroll.y) {
// subindo
if(this.scrollY > oScroll.sec1 && this.scrollY < oScroll.sec2) {
this.scrollTo(0, oScroll.sec1);
} else if(this.scrollY > oScroll.sec2 && this.scrollY < oScroll.sec3) {
this.scrollTo(0, oScroll.sec2);
} else if(this.scrollY > oScroll.sec3) {
this.scrollTo(0, oScroll.sec3);
}
} else if(this.scrollY > oScroll.y) {
// descendo
if(this.scrollY > oScroll.sec1 && this.scrollY < oScroll.sec2 && oScroll.prevent) {
this.scrollTo(0, oScroll.sec2);
} else if(this.scrollY > oScroll.sec2 && this.scrollY < oScroll.sec3) {
this.scrollTo(0, oScroll.sec3);
}
}
oScroll.y = this.scrollY;
});
body::-webkit-scrollbar {
display: none;
}
.main {
height: 1500px;
margin: 100px 0;
}
.section {
background: lightblue;
height: 500px;
margin: 10px 0;
}
<div class="main">
<div class="section" id="sec1">
<h1>
Section 1
</h1>
</div>
<div class="section" id="sec2">
<h1>
Section 2
</h1>
</div>
<div class="section" id="sec3">
<h1>
Section 3
</h1>
</div>
</div>
See the example in JSFiddle if you prefer.