jQuery滑动删除按钮是一种常见的用户界面交互设计,它允许用户通过滑动操作来删除列表项。这种设计通常用于移动设备,因为它提供了直观且易于使用的交互方式。
滑动删除按钮常见于各种应用中,如:
以下是一个简单的jQuery滑动删除按钮的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 滑动删除按钮</title>
<style>
.list-item {
position: relative;
padding: 10px;
border-bottom: 1px solid #ccc;
transition: transform 0.3s ease;
}
.delete-btn {
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 100px;
background-color: red;
color: white;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<ul id="list">
<li class="list-item">
Item 1
<div class="delete-btn">删除</div>
</li>
<li class="list-item">
Item 2
<div class="delete-btn">删除</div>
</li>
<li class="list-item">
Item 3
<div class="delete-btn">删除</div>
</li>
</ul>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('.list-item').on('touchstart', function(event) {
let startX = event.originalEvent.touches[0].clientX;
let $this = $(this);
$this.data('startX', startX);
$this.on('touchmove', function(event) {
let moveX = event.originalEvent.touches[0].clientX;
let deltaX = moveX - $this.data('startX');
let deleteBtn = $this.find('.delete-btn');
if (deltaX > 0) {
$this.css('transform', `translateX(${deltaX}px)`);
deleteBtn.css('transform', `translateX(${deltaX - deleteBtn.width()}px)`);
}
});
$this.on('touchend', function(event) {
let endX = event.originalEvent.changedTouches[0].clientX;
let deltaX = endX - $this.data('startX');
if (deltaX > 50) {
$this.remove();
} else {
$this.css('transform', '');
$this.find('.delete-btn').css('transform', '');
}
$this.off('touchmove touchend');
});
});
});
</script>
</body>
</html>
通过以上示例代码和常见问题的解决方法,你可以实现一个基本的jQuery滑动删除按钮功能。根据具体需求,你可以进一步优化和扩展该功能。
没有搜到相关的沙龙