<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery穿梭框实现</title>
<script src="/jquery.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.transfer-container {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
}
.box {
width: 200px;
height: 300px;
border: 1px solid #ccc;
border-radius: 5px;
overflow-y: auto;
}
.box-header {
background-color: #f5f5f5;
padding: 10px;
border-bottom: 1px solid #ddd;
font-weight: bold;
text-align: center;
}
.item {
padding: 8px 10px;
border-bottom: 1px solid #eee;
cursor: pointer;
}
.item:hover {
background-color: #f0f0f0;
}
.item.selected {
background-color: #d4e6f7;
}
.buttons {
display: flex;
flex-direction: column;
gap: 10px;
}
button {
padding: 8px 12px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
</style>
</head>
<body>
<h1>jQuery穿梭框示例</h1>
<div class="transfer-container">
<!-- 左侧框 -->
<div class="box" id="leftBox">
<div class="box-header">可选项目</div>
<div class="item">项目1</div>
<div class="item">项目2</div>
<div class="item">项目3</div>
<div class="item">项目4</div>
<div class="item">项目5</div>
<div class="item">项目6</div>
<div class="item">项目7</div>
<div class="item">项目8</div>
</div>
<!-- 按钮区域 -->
<div class="buttons">
<button id="moveRight">></button>
<button id="moveAllRight">>></button>
<button id="moveLeft"><</button>
<button id="moveAllLeft"><<</button>
</div>
<!-- 右侧框 -->
<div class="box" id="rightBox">
<div class="box-header">已选项目</div>
</div>
</div>
<script>
$(document).ready(function() {
// 项目选择功能
$('.item').click(function() {
$(this).toggleClass('selected');
});
// 向右移动选中项
$('#moveRight').click(function() {
$('#leftBox .item.selected').each(function() {
$(this).removeClass('selected').appendTo('#rightBox');
});
});
// 向右移动所有项
$('#moveAllRight').click(function() {
$('#leftBox .item').appendTo('#rightBox');
});
// 向左移动选中项
$('#moveLeft').click(function() {
$('#rightBox .item.selected').each(function() {
$(this).removeClass('selected').appendTo('#leftBox');
});
});
// 向左移动所有项
$('#moveAllLeft').click(function() {
$('#rightBox .item').appendTo('#leftBox');
});
// 禁用按钮逻辑
function updateButtons() {
$('#moveRight').prop('disabled', $('#leftBox .item.selected').length === 0);
$('#moveAllRight').prop('disabled', $('#leftBox .item').length === 0);
$('#moveLeft').prop('disabled', $('#rightBox .item.selected').length === 0);
$('#moveAllLeft').prop('disabled', $('#rightBox .item').length === 0);
}
// 初始按钮状态
updateButtons();
// 监听选择变化
$(document).on('click', '.item', updateButtons);
// 监听移动操作
$('.buttons button').click(updateButtons);
});
</script>
</body>
</html>