<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单穿梭框示例</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.container {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
}
.box {
width: 200px;
height: 300px;
border: 1px solid #ccc;
overflow-y: auto;
}
.box-header {
background-color: #f5f5f5;
padding: 8px;
text-align: center;
font-weight: bold;
border-bottom: 1px solid #ddd;
}
.item {
padding: 8px;
border-bottom: 1px solid #eee;
cursor: pointer;
}
.item:hover {
background-color: #f0f0f0;
}
.item.selected {
background-color: #d4e6f1;
}
.buttons {
display: flex;
flex-direction: column;
gap: 10px;
}
button {
padding: 8px 12px;
cursor: pointer;
}
.footer {
margin-top: 20px;
text-align: center;
color: #666;
}
.highlight {
color: #e74c3c;
font-weight: bold;
}
</style>
</head>
<body>
<h1>简单穿梭框示例</h1>
<div class="container">
<div class="box">
<div class="box-header">可选项目</div>
<div id="left-box">
<div class="item" data-value="1">项目1</div>
<div class="item" data-value="2">项目2</div>
<div class="item" data-value="3">项目3</div>
<div class="item" data-value="4">项目4</div>
<div class="item" data-value="5">项目5</div>
<div class="item" data-value="6">项目6</div>
</div>
</div>
<div class="buttons">
<button id="to-right">></button>
<button id="to-left"><</button>
<button id="to-right-all">>></button>
<button id="to-left-all"><<</button>
</div>
<div class="box">
<div class="box-header">已选项目</div>
<div id="right-box"></div>
</div>
</div>
<div class="footer">
<p>这个穿梭框示例使用了jQuery实现。</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 选择项目
$('#left-box, #right-box').on('click', '.item', function() {
$(this).toggleClass('selected');
});
// 向右移动选中的项目
$('#to-right').click(function() {
$('#left-box .selected').each(function() {
$(this).removeClass('selected').appendTo('#right-box');
});
});
// 向左移动选中的项目
$('#to-left').click(function() {
$('#right-box .selected').each(function() {
$(this).removeClass('selected').appendTo('#left-box');
});
});
// 全部向右移动
$('#to-right-all').click(function() {
$('#left-box .item').appendTo('#right-box');
});
// 全部向左移动
$('#to-left-all').click(function() {
$('#right-box .item').appendTo('#left-box');
});
// 双击项目快速移动
$('#left-box').on('dblclick', '.item', function() {
$(this).appendTo('#right-box');
});
$('#right-box').on('dblclick', '.item', function() {
$(this).appendTo('#left-box');
});
});
</script>
</body>
</html>