整屏滚动

236 阅读1分钟
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    html,
    body,
    .ul {
      height: 100%;
      width: 100%;
      position: relative;
      margin: 0;
      padding: 0;
      top: 0;
    }

    #app {
      overflow: hidden;
      height: 100%;
      width: 100%;
    }

    li:first-of-type {
      background-color: red;
    }

    li:nth-of-type(2) {
      background-color: blue;
    }

    li:nth-of-type(3) {
      background-color: green;
    }

    li:nth-of-type(4) {
      background-color: yellow;
    }

    li {
      height: 100%;
      width: 100%;
      list-style: none;
      color: #fff;
      font-size: 50px;
      text-align: center;
    }

    .ul {}
  </style>
</head>

<body>
  <div id="app">
    <ul class="ul">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
    </ul>
  </div>
</body>

</html>
<script>

  var kai = true, i = 0;
  var oUl = document.getElementsByTagName("ul")[0];
  document.addEventListener('mousewheel', function (ev) {
    var ev = window.event || ev;
    if (kai) {
      kai = false;
      oUl.style.transition = " 0.5s ease";
      if (ev.wheelDelta < 0 && i < 3) {
        i++;
      }
      if (ev.wheelDelta > 0 && i > 0) {
        i--;
      }
      setTimeout(function () {
        oUl.style.top = -i * 100 + 'vh';
      }, 0);
      // console.log(i)
      setTimeout(function () {
        kai = true;
      }, 1000);
    }
  });
  onresize
  window.onresize = function () {
    oUl.style.transition = "none";
  }

</script>