H5 项目页面向左滑动展示下一页,向右滑动展示上一页,左右滑动需有左右倾斜的动画,第一页不能右滑,最后一页不能左右滑动...
为了实现您所描述的需求,我们可以使用HTML、CSS和JavaScript来创建这个功能。以下是一个简单的示例,展示了如何使用这些技术实现您的需求。
HTML部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Swipe Pages</title>
<style>
.container {
overflow: hidden;
position: relative;
}
.page {
width: 100%;
height: 200px;
position: absolute;
left: 0;
transition: transform 0.5s ease;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
font-size: 3em;
}
</style>
</head>
<body>
<div class="container">
<div class="page" id="page1" style="transform: translateX(0);">1</div>
<div class="page" id="page2" style="transform: translateX(100%);">2</div>
<!-- Add more pages here if needed -->
</div>
<script>
let x1 = 0, x2 = 0, dx = 0, dy = 0, dragging = false;
const container = document.querySelector('.container');
const pages = document.querySelectorAll('.page');
let currentPage = 0;
container.addEventListener('touchstart', (e) => {
x1 = e.touches[0].pageX;
y1 = e.touches[0].pageY;
dragging = true;
}, { passive: true });
container.addEventListener('touchmove', (e) => {
if (!dragging) return;
e.preventDefault();
x2 = e.touches[0].pageX;
y2 = e.touches[0].pageY;
dx = x2 - x1;
dy = y2 - y1;
if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 30) {
e.preventDefault();
container.style.cursor = 'pointer';
dx = dx * 0.2;
container.style.transform = `translateX(${dx}px)`;
}
});
container.addEventListener('touchend', (e) => {
dragging = false;
if (dx > 0) {
// Swipe left
if (currentPage > 0) {
navigateToPage(currentPage - 1);
}
} else {
// Swipe right
if (currentPage < pages.length - 1) {
navigateToPage(currentPage + 1);
}
}
container.style.transition = 'transform 0.5s ease';
container.style.transform = '';
});
function navigateToPage(newPageIndex) {
pages[currentPage].style.transform = `translateX(${100 * (newPageIndex - currentPage)}%)`;
currentPage = newPageIndex;
}
</script>
</body>
</html>
在这个示例中,我们使用了触摸事件来处理滑动。当用户滑动时,我们计算出滑动的距离并更新容器的transform
属性来实现滑
评论已关闭