How to avoid the scroll lock?

How to avoid the scroll lock?

Hello everyone . I'm trying to create an analogue of this or this plugin. Here is my code :
  1. $(window).scroll(function() {
  2. if($(this).scrollTop() >= $("#Page_1").offset().top/2){
  3. var scroll_page_2 = $('#Page_2').offset().top;
  4. $(document).scrollTop(scroll_page_2);
  5. }
  6. if($(this).scrollTop() <= $("#Page_1").offset().top/2){
  7. var scroll_page_1 = $('#Page_1').offset().top;
  8. $(document).scrollTop(scroll_page_1);
  9. }
  10. });
I have blocked  scrolling . An example is here .
Well   with   my code   so   everything is clear .   I   get   that   if scrolling   is   below   1   page , scrolling should   fall .   But when   I'm on a   2   page and   anything   up ,   I'm still   here   1   page and   my script   me   down   down .   This   error .   Question - how to ?   Enter   the variable   to   determine which page ?   And   then   put   a bunch of   conditions ?
Thanks, Alex