jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。上下滑动翻页是一种常见的网页交互方式,通常用于移动设备或触摸屏界面,允许用户通过滑动屏幕来浏览不同的页面内容。
上下滑动翻页可以分为以下几种类型:
上下滑动翻页常用于以下场景:
以下是一个简单的 jQuery 垂直滑动翻页示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vertical Swipe Pagination</title>
<style>
.container {
width: 100%;
height: 100vh;
overflow: hidden;
}
.page {
width: 100%;
height: 100%;
display: none;
}
.page.active {
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="page active" style="background-color: red;">Page 1</div>
<div class="page" style="background-color: green;">Page 2</div>
<div class="page" style="background-color: blue;">Page 3</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
let currentPage = 0;
const pages = $('.page');
const pageCount = pages.length;
function showPage(index) {
pages.removeClass('active');
pages.eq(index).addClass('active');
}
$(document).on('touchstart', function(event) {
let startY = event.originalEvent.touches[0].pageY;
$(document).on('touchmove', function(event) {
event.preventDefault();
let endY = event.originalEvent.touches[0].pageY;
let deltaY = endY - startY;
if (Math.abs(deltaY) > 50) { // 判断滑动距离
if (deltaY > 0 && currentPage > 0) {
currentPage--;
showPage(currentPage);
} else if (deltaY < 0 && currentPage < pageCount - 1) {
currentPage++;
showPage(currentPage);
}
$(document).off('touchmove');
}
});
});
});
</script>
</body>
</html>
通过以上示例代码和常见问题解决方法,你可以实现一个基本的垂直滑动翻页功能,并根据实际需求进行扩展和优化。
没有搜到相关的沙龙