jQuery实现穿梭框

项目需要,做个简单的穿梭框****效果图如下

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>穿梭框</title>
		<link rel="stylesheet" href="index.css" rel="external nofollow" >
		<script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
	</head>
	<body>
		<ul id="shuttle_box">
			<li class="shuttle_box_li shuttle_box_near">
				<ul id="shuttle_box_left">
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
					<li>上海市人民广场</li>
				</ul>
			</li>
			<li class="shuttle_box_li" id="shuttle_box_mid">
				<button id="shuttle_box_toRight">》》</button>
            	<button id="shuttle_box_toLeft">《《</button>
			</li>
			<li class="shuttle_box_li shuttle_box_near">
				<ul id="shuttle_box_right">
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
					<li>上海市徐泾东</li>
				</ul>
			</li>
		</ul>
		<script src="index.js"></script>
	</body>
</html>

body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, input, p, blockquote, th, td {
    margin:0; padding:0; 
    list-style: none;
}
body{background-color: #e3e3e3;margin: 0px;}
#shuttle_box{width:800px;zoom: 1;margin: 100px auto;}
#shuttle_box:after{
   content: ".";
   clear: both;
   display: block;
   height: 0;
   overflow: hidden;
   visibility: hidden;
}
.shuttle_box_li{height: 540px;float: left;}
.shuttle_box_near{width:300px;background-color:#ffffff;overflow-y: scroll;overflow-x:hidden;border-radius: 10px;}
.shuttle_box_li_act{color:#ffffff !important;background-color: #1890ff !important;border-bottom: 1px solid #ffffff;transition: all .01s;}
.shuttle_box_near::-webkit-scrollbar {/*滚动条整体样式*/
       width: 6px;     /*高宽分别对应横竖滚动条的尺寸*/
       height: 1px;
   }
.shuttle_box_near::-webkit-scrollbar-thumb {/*滚动条里面小方块*/
       border-radius: 20px;
       background-color: rgba(0,0,0,0.5);
   }
.shuttle_box_near::-webkit-scrollbar-track {/*滚动条里面轨道*/
       background-color: rgba(0,0,0,0.2);
       border-radius: 20px;
   }
.shuttle_box_near li{
   padding:8px;
   border-bottom: 1px solid #ffffff;
   background-color: #f4f4f4;
   cursor: pointer;
   transition: all .5s;
}
.shuttle_box_li_act:hover{opacity: 0.7;transition: all .01s;}
#shuttle_box_mid{width:80px;text-align: center;}
#shuttle_box_mid button{
    width: 50px;
    height:30px;
    display: block;
    margin:20px auto;
    line-height: 30px;
    color:white;
    cursor: pointer;
    background-color: #1890ff;
    border-radius: 5px;
    transition: all .5s;
    border:none;
}
#shuttle_box_mid button:hover{opacity: 0.7;transition: all .5s;}
#shuttle_box_toRight{margin-top:225px !important;}
//穿梭框左侧选中
$("#shuttle_box_left").on('click', 'li', function () {
    if ($(this).hasClass('shuttle_box_li_act')) {
        $(this).removeClass('shuttle_box_li_act');
    } else {
        $(this).addClass('shuttle_box_li_act');
    }
});
//穿梭框右侧选中
$("#shuttle_box_right").on('click', 'li', function () {
    if ($(this).hasClass('shuttle_box_li_act')) {
        $(this).removeClass('shuttle_box_li_act');
    } else {
        $(this).addClass('shuttle_box_li_act');
    }
});
//向右移动
$("#shuttle_box_toRight").click(function () {
    if ($("#shuttle_box_left .shuttle_box_li_act").length == 0) return false;

    $("#shuttle_box_left").find('.shuttle_box_li_act').appendTo("#shuttle_box_right");
    $("#shuttle_box_right li").removeClass('shuttle_box_li_act');


});
//向左移动
$("#shuttle_box_toLeft").click(function () {
    if ($("#shuttle_box_right .shuttle_box_li_act").length == 0) return false;

    $("#shuttle_box_right .shuttle_box_li_act").appendTo("#shuttle_box_left");
    $("#shuttle_box_left li").removeClass('shuttle_box_li_act');
});

jQuery穿梭框,可上下左右,全选移动

HTML部分

<table align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    可选择表项
                </td>
                <td></td>
                <td>
                    已选择表项
                </td>
            </tr>
            <tr>
                <td>
                    <select multiple="multiple" id="leftselect" name="leftselect"
                        style="width: 100px; height: 160px;">
                        <option value="1">
                            选项1
                        </option>
                        <option value="2">
                            选项2
                        </option>
                        <option value="3">
                            选项3
                        </option>
                        <option value="4">
                            选项4
                        </option>
                        <option value="5">
                            选项5
                        </option>
                        <option value="6">
                            选项6
                        </option>
                        <option value="7">
                            选项7
                        </option>
                        <option value="8">
                            选项8
                        </option>
                    </select>

                </td>
                <td>
                    <input type="button" class="button" id="add" value="选中向右" width="6">
                    <br>
                    <input type="button" class="button" id="remove" value="选中向左">
                    <br>
                    <input type="button" class="button" id="alladd" value="全部向右">
                    <br>
                    <input type="button" class="button" id="allremove" value="全部向左">
                </td>
                <td>
                    <select multiple="multiple" id="rightselect" name="rightselect"
                        style="width: 100px; height: 160px;">
                    </select>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <input id="left_up"  class="button" type="button" value="向上" />
                    &nbsp;&nbsp;&nbsp;
                    <input id="left_down" class="button" type="button" value="向下" />
                </td>
                <td></td>
                <td align="center">
                    <input id="right_up"  class="button" type="button" value="向上" />
                    &nbsp;&nbsp;&nbsp;
                    <input id="right_down" class="button" type="button" value="向下" />
                </td>
            </tr>
        </table>

js部分 

//全部向右
		$('#alladd').click(function() {
			$('#leftselect option').remove().appendTo('#rightselect');
		});
		//全部向左
		$('#allremove').click(function() {
			$('#rightselect option').remove().appendTo('#leftselect');
		});
		//选中向右
		$('#add').click(function() {
			$('#leftselect option:selected').remove().appendTo('#rightselect');
		});
		//选中向左
		$('#remove').click(function() {
			$('#rightselect option:selected').remove().appendTo('#leftselect');
		});
		//左边框双击向右移动
		$('#leftselect').dblclick(function() {
			$("option:selected", this).remove().appendTo('#rightselect');
		});
		//右边框双击向右移动
		$('#rightselect').dblclick(function() {
			$("option:selected", this).remove().appendTo('#leftselect');
		});
		//左边框往上移动
		$('#left_up')
				.click(
						function() {
							var index = $('#leftselect option').index(
									$('#leftselect option:selected:first'));
							var $recent = $('#leftselect option:eq('   (index - 1)   ')');
							if (index > 0) {
								var $options = $('#leftselect option:selected')
										.remove();
								setTimeout(function() {
									$recent.before($options)
								}, 10);
							}
						});
		//左边框往下移动
		$('#left_down')
				.click(
						function() {
							var index = $('#leftselect option').index(
									$('#leftselect option:selected:last'));
							var len = $('#leftselect option').length - 1;
							var $recent = $('#leftselect option:eq('   (index   1)   ')');
							if (index < len) {
								var $options = $('#leftselect option:selected')
										.remove();
								setTimeout(function() {
									$recent.after($options)
								}, 10);
							}
						});
		//右边框往上移动
		$('#right_up')
				.click(
						function() {
							var index = $('#rightselect option').index(
									$('#rightselect option:selected:first'));
							var $recent = $('#rightselect option:eq('   (index - 1)   ')');
							if (index > 0) {
								var $options = $('#rightselect option:selected')
										.remove();
								setTimeout(function() {
									$recent.before($options)
								}, 10);
							}
						});
		//右边框往下移动
		$('#right_down')
				.click(
						function() {
							var index = $('#rightselect option').index(
									$('#rightselect option:selected:last'));
							var len = $('#rightselect option').length - 1;
							var $recent = $('#rightselect option:eq('   (index   1)   ')');
							if (index < len) {
								var $options = $('#rightselect option:selected')
										.remove();
								setTimeout(function() {
									$recent.after($options)
								}, 10);
							}
						});

以上为个人经验,希望能给大家一个参考,也希望大家多多支持Devmax。

使用jQuery实现简单穿梭框方式的更多相关文章

  1. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. 在IOS9中的Cordova应用程序使用JQuery / Javascript的window.history问题

    在两个测试用例中唯一改变的是Cordova.js.解决方法我看到这是几个星期前,但我会发布这个,以防其他人遇到它.听起来它可能与iOS9中的哈希更改生成的导航事件有关.如果是这样,可以将其添加到index.html以禁用哈希侦听:

  3. iOS 5上的jQuery事件

    解决方法在Apple开发论坛上由一个人回答:我需要在将元素添加到DOM之后才绑定(),如下所示:

  4. android – Phonegap本地构建 – jquery ajax错误:readystate 0 responsetext status 0 statustext error

    解决方法您是否在索引文件中包含了内容安全元标记?

  5. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  6. 设置焦点到输入框和显示Android键盘使用jquery手机在pageshow

    我正在设置焦点到输入框,并显示Android键盘使用jquery手机网页显示.我从Web上尝试过很多选项.但是没有一个在模拟器和移动设备中都能按预期工作.这是代码:查找屏幕截图以供参考请咨询…解决方法对我有用的解决方案

  7. android – 如何在焦点()上以编程方式隐藏jquery mobile中的键盘

    我想在Focus()上隐藏键盘,但是当$(“.ui-input-text”).focus();它会自动打开键盘.我只是想隐藏在特定的屏幕上,我用document.activeElement.blur()测试;但它也没有关注()输入.解决方法提交表单时,iOS键盘可能不会自动关闭.这是一个非常实用的问题,因为不应要求用户手动关闭键盘,否则他们不会期望需要这样做.可以通过调用document.acti

  8. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. jQuery实现简单的抽奖游戏

    这篇文章主要为大家详细介绍了jQuery实现简单的抽奖游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. jQuery的Cookie封装,与PHP交互的简单实现

    下面小编就为大家带来一篇jQuery的Cookie封装,与PHP交互的简单实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部