Search code examples
javascriptscroll

On scroll down, scroll 100vh to the bottom


I want to achieve the same effect at http://www.squaredot.eu/#Intro

So if I scroll down, the body must scroll 100vh to the bottom. And also if scroll up, the body must scroll 100vh up. I tried something, but it didn't work out.

HTML:

<!DOCTYPE html>
<html>
<head>
    <title> Log In </title>
    <link href="main.css" rel="stylesheet" type="text/css">
</head>
    <body>
        <div id="e1"></div>
        <div id="e2"></div>
        <div id="e3"></div>
        <div id="e4"></div>
        <div id="e5"></div>
    </body>
</html>

CSS:

body, html {
  height: 100%;
  margin: 0;
  padding: 0;
}

#e1 {
    width: 100%;
    height: 100vh;
    background-color: red;
}

#e2 {
    width: 100%;
    height: 100vh;
    background-color: green;
}

#e3 {
    width: 100%;
    height: 100vh;
    background-color: yellow;
}

#e4 {
    width: 100%;
    height: 100vh;
    background-color: blue;
}

#e5 {
    width: 100%;
    height: 100vh;
    background-color: orange;
}

JAVASCRIPT

document.addEventListener('scroll', function(e) {
    var currScroll = document.body.scrollTop;
    document.body.scrollTop = calc(~"currScroll + 100vh");
 }

);

Solution

  • One solution could be using transform from CSS (like the website you linked is doing).

    Add this as css:

    body {
        transform: translate3d(0px, 0px, 0px);
        transition: all 700ms ease;
    }
    

    And this as javascript

    var pageHeight = window.innerHeight;
    
    document.addEventListener('scroll', function(){
      document.body.scrollTop = 0;
    });
    
    document.addEventListener('wheel', function(e) {
      //console.log(e.deltaY);
      if(e.deltaY > 0) {
        scrollDown();
      } else {
        scrollUp();
      }
     }
    );
    
    function scrollDown() {
      document.body.style.transform = 'translate3d(0px, -'+ pageHeight + 'px, 0px)';
    }
    
    function scrollUp() {
      document.body.style.transform = 'translate3d(0px, 0px, 0px)';
    }
    

    It only works for element 1 and 2 but it's a start, and you can learn how to implement the other steps!

    Working example here: https://jsbin.com/titaremevi/edit?css,js,output


    UPDATE:

    This is the fully working solution:

    var pageHeight = window.innerHeight;
    var isAnimating = false;
    document.body.style.transform = 'translate3d(0px,0px,0px)';
    
    document.addEventListener('scroll', function(e){
      document.body.scrollTop = 0;
    });
    document.addEventListener('wheel', wheelListener);
    
    function wheelListener(e) {
      if(e.deltaY > 0) {
        scrollPage(-pageHeight);
      } else {
        scrollPage(+pageHeight);
      }
    }
    
    function scrollPage(scrollSize) {
      if(isAnimating){
        return;
      }
      isAnimating = true;
      var yPos = getNewYPos(scrollSize);
      document.body.style.transform = 'translate3d(0px,'+ yPos + ',0px)';
    }
    
    function getNewYPos(add){
      var oldYPos = document.body.style.transform.split(',')[1];
      oldYPos = parseInt(oldYPos.replace(/px/,''));
      var newYPos = oldYPos + add;
      if(newYPos > 0){
        isAnimating = false;
      }
      return Math.min(0, newYPos) + 'px';
    }
    
    
    document.body.addEventListener('transitionend', function(){
      setTimeout(function(){ isAnimating = false; }, 500);
      document.addEventListener('wheel', wheelListener);
    })
    

    You can see it working here: https://jsbin.com/foxigobano/1/edit?js,output