HTML的上下翻页效果

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>翻页效果</title>

	<style>
/* 设置页面容器的高度,使其可以滚动 */
    .page-container {
      height:900px;
		width: 100%;
      overflow: hidden;
		
    }

    /* 设置每个页面的高度和样式 */
    .page {display: flex;
      height: 900px;
		overflow: hidden;
		justify-content: center;
		text-align: center;
		align-items: center;
		color:aliceblue;
    }
		#one{
			background: #ED6C6E;
		}#two{
			background: #54C177;
		}#san{
			background: #3175D5;
		}#shi{
			background:#E5630F;
		}
		
		h1{
			transform: translatey(1000px);
			transition: 0.75s;
		}
		
		#one:hover h1{
			transform: translatey(0);
			transition: 0.45s;
			font-size: 10rem;
		}
		#two:hover h1{
			transform: translatey(0);
			transition: 0.45s;
			font-size: 10rem;
		}
		#san:hover h1{
			transform: translatey(0);
			transition: 0.45s;
			font-size: 10rem;
		}
		#shi:hover h1{
			transform: translatey(0);
			transition: 0.45s;
			font-size: 10rem;
		}
	</style>
</head>

<body>
	 <div class="page-container">
    <!-- 第一页 -->	
	
    <div id="one"  class="page"> 
		<h1>翻</h1>
		 </div>
		 
		 <div id="two" class="page"> 
			 <h1>页</h1>
		 </div>
		 
		 <div id="san" class="page">
			 <h1>效</h1>
		 </div>
		 
		 <div id="shi" class="page"> 
			 <h1>果</h1>
		 </div>
		 </div>
	
</body>
</html>
<script>
// 使用JavaScript监听滚动事件,实现上下滚动翻页的效果
    const container = document.querySelector('.page-container');
    const pages = container.querySelectorAll('.page');
    let currentPageIndex = 0;

    // 滚动到下一页
    function scrollToNextPage() {
      if (currentPageIndex < pages.length - 1) {
        currentPageIndex++;
        container.scrollTo({
          top: pages[currentPageIndex].offsetTop,
          behavior: 'smooth'
        });
      }
    }

    // 滚动到上一页
    function scrollToPreviousPage() {
      if (currentPageIndex > 0) {
        currentPageIndex--;
        container.scrollTo({
          top: pages[currentPageIndex].offsetTop,
          behavior: 'smooth'
        });
      }
    }

    // 监听鼠标滚动事件
    container.addEventListener('wheel', function(event) {
      event.preventDefault();

      const delta = event.deltaY;

      if (delta > 0) {
        scrollToNextPage();
      } else {
        scrollToPreviousPage();
      }
    });

    // 监听触摸滑动事件(适用于触摸屏设备)
    let touchStartY = 0;
    container.addEventListener('touchstart', function(event) {
      touchStartY = event.touches[0].clientY;
    });

    container.addEventListener('touchmove', function(event) {
      event.preventDefault();

      const touchMoveY = event.touches[0].clientY;

      if (touchMoveY - touchStartY > 50) {
        scrollToNextPage();
        touchStartY = touchMoveY; // 更新起始触摸位置
      } else if (touchMoveY - touchStartY < -50) {
        scrollToPreviousPage();
        touchStartY = touchMoveY; // 更新起始触摸位置
      }
    });
</script>

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值