右侧滑动菜单是一种常见的移动端UI模式,通常用于导航或显示辅助功能。它从屏幕右侧滑出,覆盖或推动主内容区域。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>右侧滑动菜单</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="main-content">
<button class="menu-toggle">打开菜单</button>
<!-- 主内容区域 -->
</div>
<div class="side-menu">
<div class="menu-header">
<button class="close-menu">×</button>
<h2>菜单</h2>
</div>
<ul class="menu-items">
<li><a href="#">首页</a></li>
<li><a href="#">关于我们</a></li>
<li><a href="#">服务</a></li>
<li><a href="#">联系我们</a></li>
</ul>
</div>
<div class="overlay"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</body>
</html>
body {
margin: 0;
padding: 0;
overflow-x: hidden;
font-family: Arial, sans-serif;
}
.main-content {
transition: transform 0.3s ease;
position: relative;
z-index: 1;
}
.side-menu {
position: fixed;
top: 0;
right: -300px; /* 初始位置在屏幕外 */
width: 300px;
height: 100%;
background: #fff;
box-shadow: -2px 0 5px rgba(0, 0, 0, 0.2);
z-index: 100;
transition: right 0.3s ease;
}
.menu-header {
padding: 15px;
background: #333;
color: white;
display: flex;
justify-content: space-between;
align-items: center;
}
.close-menu {
background: none;
border: none;
color: white;
font-size: 24px;
cursor: pointer;
}
.menu-items {
list-style: none;
padding: 0;
margin: 0;
}
.menu-items li a {
display: block;
padding: 15px;
color: #333;
text-decoration: none;
border-bottom: 1px solid #eee;
}
.menu-items li a:hover {
background: #f5f5f5;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
z-index: 99;
pointer-events: none;
transition: opacity 0.3s ease;
}
/* 菜单打开时的样式 */
.menu-open .main-content {
transform: translateX(-300px);
}
.menu-open .side-menu {
right: 0;
}
.menu-open .overlay {
opacity: 1;
pointer-events: auto;
}
$(document).ready(function() {
// 打开菜单
$('.menu-toggle').click(function() {
$('body').addClass('menu-open');
});
// 关闭菜单
$('.close-menu, .overlay').click(function() {
$('body').removeClass('menu-open');
});
// 防止点击菜单内部时关闭菜单
$('.side-menu').click(function(e) {
e.stopPropagation();
});
// 处理滑动关闭
let startX = 0;
let currentX = 0;
let isDragging = false;
$('.side-menu').on('touchstart', function(e) {
startX = e.originalEvent.touches[0].clientX;
currentX = startX;
isDragging = true;
});
$('.side-menu').on('touchmove', function(e) {
if (!isDragging) return;
currentX = e.originalEvent.touches[0].clientX;
let diff = startX - currentX;
if (diff > 0) {
$(this).css('right', -diff + 'px');
}
});
$('.side-menu').on('touchend', function() {
if (!isDragging) return;
isDragging = false;
let threshold = 100; // 滑动超过100px则关闭菜单
if (startX - currentX > threshold) {
$('body').removeClass('menu-open');
} else {
$(this).css('right', '0');
}
});
});
原因:可能是由于复杂的CSS选择器或过多的重绘/回流 解决:
transform
和opacity
等属性进行动画原因:触摸事件处理不够精确 解决:
原因:没有禁用背景滚动 解决:
// 在打开菜单时添加
$('body').css('overflow', 'hidden');
// 关闭菜单时恢复
$('body').css('overflow', 'auto');
原因:iOS对某些CSS属性的优化不足 解决:
.side-menu {
-webkit-overflow-scrolling: touch;
-webkit-transform: translate3d(0,0,0);
}
这个实现方案提供了完整的右侧滑动菜单功能,包括基本的打开/关闭交互和触摸滑动支持,可以根据具体需求进一步扩展和定制。
没有搜到相关的文章