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>侧滑页面示例</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
width: 200%;
height: 100vh;
display: flex;
transition: transform 0.3s ease-in-out;
}
.page {
width: 50%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
}
.page1 {
background-color: #f06;
}
.page2 {
background-color: #0f6;
}
</style>
</head>
<body>
<div class="container">
<div class="page page1">页面1</div>
<div class="page page2">页面2</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
let currentPage = 1;
$('.container').css('transform', `translateX(-${currentPage * 50}%)`);
$(document).on('swipeleft', function() {
if (currentPage < 2) {
currentPage++;
$('.container').css('transform', `translateX(-${currentPage * 50}%)`);
}
});
$(document).on('swiperight', function() {
if (currentPage > 1) {
currentPage--;
$('.container').css('transform', `translateX(-${currentPage * 50}%)`);
}
});
});
</script>
</body>
</html>
requestAnimationFrame
优化 JavaScript 动画。通过以上示例和解决方法,你可以实现一个基本的侧滑页面效果,并根据需要进行优化和扩展。
没有搜到相关的文章