JS原生双栏穿梭选择框的实现示例

本文主要介绍了JS原生双栏穿梭选择框的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

何时使用

用直观的方式在两栏中移动元素,完成选择行为。

选择一个或以上的选项后,点击对应的方向键,可以把选中的选项移动到另一栏。 其中,左边一栏为 source,右边一栏为 target,API 的设计也反映了这两个概念。

话不多说,上代码。

结构分支

image-20211026181412467

代码

dataSelection.html

   双栏穿梭选择框 
    • 李白    
    • 苏轼    
    • 王安石
    • 李商隐
    • 王维

dS.css

 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:700px;zoom: 1;margin: 0px 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;border:5px solid #f4f4f4} .shuttle_box_li_act{color:#ffffff !important;background-color: #009688 !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: #009688; 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;} 

ds.js

 $(document).ready(function() { //穿梭框左侧选中 $("#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'); } }); //点击事件选择内部事件 $(".inside").bind('click', function(event1) { event1.stopPropagation(); }); }); //穿梭框右侧选中 $("#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'); });

运行结果

在这里插入图片描述

到此这篇关于JS原生双栏穿梭选择框的实现示例的文章就介绍到这了,更多相关JS 双栏穿梭选择框内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是JS原生双栏穿梭选择框的实现示例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程