JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div id="s1">
Секция 1
<a href="#s2">Вниз</a>
</div>
<div id="s2">
Секция 2
<a href="#s1">Вверх</a>
<a href="#s3">Вниз</a>
</div>
<div id="s3">
Секция 3
<a href="#s2">Вверх</a>
<a href="#s4">Вниз</a>
</div>
<div id="s4">
Секция 4
<a href="#s3">Вверх</a>
<a href="#s5">Вниз</a>
</div>
<div id="s5">
Секция 5
<a href="#s4">Вверх</a>
</div>
CSS
body {
margin: 0;
padding: 0;
}
div {
width: 100%;
height: 300px;
padding-top: 10px;
padding-left: 20px;
box-sizing: border-box;
color: #ffffff;
}
#s1 {
background-color: #ff8080;
}
#s2 {
background-color: orange;
}
#s3 {
background-color: yellow;
}
#s4 {
background-color: yellowgreen;
}
#s5 {
background-color: lightgreen;
}
a {
display: block;
margin-top: 50px;
}
JavaScript
var currentAnchor = 0;
var isAnimating = false;
function scrollUpDown(dir) {
var sections = $('#s1, #s2, #s3, #s4, #s5');
var currentScroll = $(document).scrollTop();
var nearestEl = null;
var defaultTop = 0;
sections.each(function(i, el) {
var el = $(el);
if (dir == 'down') {
if (currentScroll < el.offset().top) {
if (nearestEl != null && nearestEl.offset().top > el.offset().top) {
nearestEl = el;
} else if (nearestEl == null) {
nearestEl = el;
}
}
defaultTop = $(document).outerHeight(true);
}
if (dir == 'up') {
if (currentScroll > el.offset().top) {
if (nearestEl != null && nearestEl.offset().top < el.offset().top) {
nearestEl = el;
} else if (nearestEl == null) {
nearestEl = el;
}
}
var defaultTop = 0;
}
});
if (nearestEl != null) {
return(nearestEl.offset().top);
} else {
return(defaultTop);
}
}
$(function(){
$('body').on('mousewheel', function(e){
e.preventDefault();
e.stopPropagation();
if( isAnimating ) {
return false;
}
isAnimating = true;
// Increase or reset current anchor
if( e.originalEvent.wheelDelta >= 0 ) {
currentAnchor = scrollUpDown('up');
}else{
currentAnchor = scrollUpDown('down');
}
isAnimating = true;
$('html, body').animate({
scrollTop: currentAnchor
}, 500, 'swing', function(){
isAnimating = false;
});
});
});