h5中鼠标滚动每次滚动一整屏页面

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Full Page Scroll with Footer and Navigation</title>
  <style>
    body,
    html {
      margin: 0;
      padding: 0;
      overflow: hidden;
      height: 100%;
    }

    .section {
      height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 2rem;
    }

    #footer {
      height: 300px;
      background: #ccc;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .page {
      transition: transform 0.5s ease;
      position: relative;
    }

    .navbar {
      position: fixed;
      top: 0;
      width: 100%;
      background: #333;
      color: white;
      padding: 10px;
      display: flex;
      justify-content: center;
      z-index: 1000;
    }

    .navbar ul {
      list-style: none;
      margin: 0;
      padding: 0;
      display: flex;
    }

    .navbar li {
      margin: 0 15px;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="navbar">
    <ul>
      <li class="halfmenu_item_empty"></li>
      <li class="halfmenu_item">
        <span class="menu-item-span" onclick="scrollToSection(0)">首页</span>
      </li>
      <li class="halfmenu_item">
        <span class="menu-item-span" onclick="scrollToSection(1)">照片修复</span>
      </li>
      <li class="halfmenu_item">
        <span class="menu-item-span" onclick="scrollToSection(2)">网图修复</span>
      </li>
      <li class="halfmenu_item">
        <span class="menu-item-span" onclick="scrollToSection(3)">折痕修复</span>
      </li>
    </ul>
  </div>

  <div class="page">
    <div class="section" style="background: #ff6f61;">Page 1</div>
    <div class="section" style="background: #6b5b95;">Page 2</div>
    <div class="section" style="background: #88b04b;">Page 3</div>
    <div class="section" style="background: #f7cac9;">Page 4</div>
    <div id="footer">Footer</div>
  </div>

  <script>
    let currentSection = 0;
    const sections = document.querySelectorAll('.section');
    const footer = document.getElementById('footer');
    const totalSections = sections.length;
    let isScrolling = false; // 用于节流控制

    window.addEventListener('wheel', (event) => {
      if (isScrolling) return;
      isScrolling = true;

      if (event.deltaY > 0) {
        if (currentSection < totalSections) {
          currentSection++;
        }
      } else {
        if (currentSection > 0) {
          currentSection--;
        }
      }
      scrollToSection(currentSection);

      setTimeout(() => {
        isScrolling = false;
      }, 500); // 滚动动画时间
    });

    window.addEventListener('touchstart', handleTouchStart, false);
    window.addEventListener('touchmove', handleTouchMove, false);

    let xDown = null;
    let yDown = null;

    function handleTouchStart(evt) {
      const firstTouch = evt.touches[0];
      xDown = firstTouch.clientX;
      yDown = firstTouch.clientY;
    };

    function handleTouchMove(evt) {
      if (!xDown || !yDown || isScrolling) {
        return;
      }
      isScrolling = true;

      const xUp = evt.touches[0].clientX;
      const yUp = evt.touches[0].clientY;

      const xDiff = xDown - xUp;
      const yDiff = yDown - yUp;

      if (Math.abs(xDiff) > Math.abs(yDiff)) {
        /* most significant */
        if (xDiff > 0) {
          /* left swipe */
        } else {
          /* right swipe */
        }
      } else {
        if (yDiff > 0) {
          /* up swipe */
          if (currentSection < totalSections) {
            currentSection++;
          }
        } else {
          /* down swipe */
          if (currentSection > 0) {
            currentSection--;
          }
        }
      }
      scrollToSection(currentSection);

      setTimeout(() => {
        isScrolling = false;
      }, 500); // 滚动动画时间

      /* reset values */
      xDown = null;
      yDown = null;
    };

    function scrollToSection(sectionIndex) {
      currentSection = sectionIndex;
      if (currentSection < totalSections) {
        document.querySelector('.page').style.transform = `translateY(-${currentSection * 100}vh)`;
      } else {
        const footerHeight = footer.offsetHeight;
        const offset = (totalSections - 1) * 100 + (footerHeight / window.innerHeight * 100);
        document.querySelector('.page').style.transform = `translateY(-${offset}vh)`;
      }
    }
  </script>
</body>

</html>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值