穿梭框

穿梭框

先引入jQuery
HTML文件:

<ul id="shuttle_box">
          <li class="shuttle_box_li shuttle_box_near">
              <ul id="shuttle_box_left">
                  <!-- <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> -->
              </ul>
          </li>
      </ul>

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: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;}

JS文件

let data=[
    {"id":1,"title":"1111","check":false},
    {"id":2,"title":"2222","check":false},
    {"id":3,"title":"3333","check":false},
    {"id":4,"title":"4444","check":false},
    {"id":5,"title":"5555","check":false},
    {"id":6,"title":"aaaa","check":true},
    {"id":7,"title":"bbbb","check":true},
    {"id":8,"title":"cccc","check":true},
    {"id":9,"title":"dddd","check":true},
    {"id":10,"title":"eeee","check":true}
];
function showLi(data){
  
    let leftStr="";
    let rightStr="";
    for(let i in data){
        let {id,title,check}=data[i];
        if(check){
            rightStr+=`
                <li _id="${id}">${title}</li>
            `
        }else{
            leftStr+=`
                <li _id="${id}">${title}</li>
            `
        }
    }
    $("#shuttle_box_left").html(leftStr);
    $("#shuttle_box_right").html(rightStr);
    
}
$(function(){
    showLi(data);
})


// //穿梭框左侧选中

$("#shuttle_box_left").on('click', 'li', function () {
    let index=data.findIndex((v)=>{
        return v.id== $(this).attr("_id")
    })

    if ($(this).hasClass('shuttle_box_li_act')) {
        $(this).removeClass('shuttle_box_li_act');
        data[index].check=false;

    } else {
        $(this).addClass('shuttle_box_li_act');
        data[index].check=true;
    }
});
//穿梭框右侧选中
$("#shuttle_box_right").on('click', 'li', function () {
    let index=data.findIndex((v)=>{
        return v.id== $(this).attr("_id")
    })
    if ($(this).hasClass('shuttle_box_li_act')) {
        $(this).removeClass('shuttle_box_li_act');
        data[index].check=true;
    } else {
        $(this).addClass('shuttle_box_li_act');
        data[index].check=false;
    }
});
// //向右移动
$("#shuttle_box_toRight").click(function () {
    if ($("#shuttle_box_left .shuttle_box_li_act").length == 0) return false;

    showLi(data);

});
// //向左移动
$("#shuttle_box_toLeft").click(function () {
    if ($("#shuttle_box_right .shuttle_box_li_act").length == 0) return false;
    showLi(data);
   
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值