Bug on fullscreen scrolling website

Asked

Viewed 40 times

1

I’m trying to make a fullscreen scrolling website. That is, I do not want the scroll to stop in the middle of a section but identify if it comes from below or from above and animate to the top of the respective section. I have the following code however, it seems that the animation scroll (scrollTop) conflicts with the page scroll and I can’t get out of section 1.

var lastScrollTop = 0;
$(window).on('scroll', function(event){
   var st = $(this).scrollTop();
   if (st > lastScrollTop){
       $('html, body').stop().animate({
       		scrollTop: $('section').eq(1).offset().top
       }, 100);
   } else {
   		$('html, body').stop().animate({
       		scrollTop: $('section').eq(0).offset().top
       }, 100);
   }
   lastScrollTop = st;
});
section {
  height: 600px;
}
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="red">
  
</section>
<section class="blue">
  
</section>

1 answer

0


I think the logic of your code storing a value in lastScrollTop is causing conflict. Look, even though section from below any scroll which, at the very least, is less than the lastScrollTop, will do the scroll go up to the first section, which should not occur because of the content of the question.

You can use the plugin Scrollify that already has that effect:

$.scrollify({
   section : ".blue",
   interstitialSection : ".red",
   easing: "linear",
   scrollSpeed: 500,
   offset : 0,
   scrollbars: true,
   setHeights: true,
   overflowScroll: true,
   updateHash: false,
   touchScroll:true
});
section {
  height: 600px;
}
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/scrollify/1.0.19/jquery.scrollify.min.js"></script>
<section class="red">
  
</section>
<section class="blue">
  
</section>

  • I understand your logic but it seems to me that you continue to create conflicts. I still can’t scroll properly after the animation. I just edited my code with your solution and only with the animation in one of the sections and when I try to leave the blue section (section with the animation) I can’t either.

  • @Clarafrazao You cannot change your question to match my answer. I have reverted to the original.

  • @Clarafrazao Maybe I don’t understand exactly what you want. The page will only have these two sections or have more content below the blue section?

  • @dvd I think is wanted something like this: https://projects.lukehaas.me/scrollify/#Configuration or https://alvarotrigo.com/fullPage/. That is, it should be in the next/previous section depending on the direction of the scroll (the top of the Section should be at the top of the jenela)

  • @dvd what I want is exactly what these plugins do. However, I just want to put the animation in one section, the last one (in the example, in blue). That is, when I get to the last section I don’t want there to be the possibility of it being in the middle. Thank you

  • @Clarafrazao I used the plugin from the first link you reported. See if this is it: https://jsfiddle.net/eunbmnak/

  • this solution helped, thank you. Put in the answer here also that I already accept it

  • @Clarafrazao Updated response. Obg!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.