Поблоковая прокрутка на jQuery

Есть скрипт поблоковой прокрутки на jQuery. Как сделать так чтоб он не перекидывал тебя на первый блок после прокрутки до последнего? нашел ответ но я не знаю как этот ответ написать на jquery введите сюда описание изображения

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var anchors = [];
var currentAnchor = -1;
var isAnimating  = false;
$(function(){
    function updateAnchors() {
        anchors = [];
        $('.anchor').each(function(i, element){
            anchors.push( $(element).offset().top );
        });
    }
    $('body').on('mousewheel', function(e){
        e.preventDefault();
        e.stopPropagation();
        if( isAnimating ) {
            return false;
        }
        isAnimating  = true;
        if( e.originalEvent.wheelDelta >= 0 ) {
            currentAnchor--;
        }else{
            currentAnchor++;
        }
        if( currentAnchor > (anchors.length - 1)
           || currentAnchor < 0 ) {
            currentAnchor = 0;
        }
        isAnimating  = true;
        $('html, body').animate({
            scrollTop: parseInt( anchors[currentAnchor] )
        }, 200, 'swing', function(){
            isAnimating  = false;
        });
    });
    updateAnchors();
});


Ответы (1 шт):

Автор решения: highpassion

Разделить условия довольно просто - здесь jquery не затрагивается.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var anchors = [];
var currentAnchor = -1;
var isAnimating  = false;
$(function(){
    function updateAnchors() {
        anchors = [];
        $('.anchor').each(function(i, element){
            anchors.push( $(element).offset().top );
        });
    }
    $('body').on('mousewheel', function(e){
        e.preventDefault();
        e.stopPropagation();
        if( isAnimating ) {
            return false;
        }
        isAnimating  = true;
        if( e.originalEvent.wheelDelta >= 0 ) {
            currentAnchor--;
        } else {
            currentAnchor++;
        }

        if( currentAnchor > (anchors.length - 1) ) {
            currentAnchor = anchors.length - 1;
        }
        
        if (currentAnchor < 0) currentAnchor = 0;
        
        isAnimating  = true;
        $('html, body').animate({
            scrollTop: parseInt( anchors[currentAnchor] )
        }, 200, 'swing', function(){
            isAnimating  = false;
        });
    });
    updateAnchors();
});

→ Ссылка