# jquery版本的下拉多选框 不受父元素的overflow影响

jquery版本的下拉多选框 不受父元素的overflow影响

之前在网上找了一圈 全是受父元素影响的下拉多选 很难受 只能自己写一个 现在分享出来

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
</head>

<body>
  <div class='box'>
    <div class='td'>选项1;选项2</div>
  </div>
</body>
<script>
  const list = [
  {
    value: '选项1',
    text: '选项1',
  },{
    value: '选项2',
    text: '选项2',
  },{
    value: '选项3',
    text: '选项3',
  }
  ]
  createCheckBox(list);
  function createCheckBox(list) {
    const button = '<button class="showBox" />';
    const choiceDom = '<span class="active"></span>';
    const $td = $('.td');
    const $box = $('.box');
    const text = $td.text();
    let choiceArr = text.split(';');
    $td.empty().append(button);
    const $button = $td.find('button');
    $button.text(text);
    let liDom = '';
    list.forEach(li => {
      let choiceState = '';
      choiceArr.indexOf(li.text) > -1 && (choiceState = choiceDom)
      liDom += `<li>${li.text}${choiceState}</li>`
    })
    const checkbox = document.createElement('div');
    const checkboxDom = $(checkbox).append(`<div class="multiple" style='display: none;'>
    <div>
      <input type='text' placeholder='请输入'/>
    </div>
    <ul class="checkList">
      ${liDom}
    </ul>
  </div>`);
    $box.append(checkboxDom);
    
    $(checkboxDom).on('click',function(e){
      e.stopPropagation()
    })
    $(checkboxDom).find('input').on('input',function(){
      var val = $(this).val();
      let liDom = ''
      list.forEach(li => {
        let choiceState = '';
        choiceArr.indexOf(li.text) > -1 && (choiceState = choiceDom)
        li.text.indexOf(val) > -1 && (liDom += `<li>${li.text}${choiceState}</li>`)
      })
      $(checkboxDom).find('ul').html(liDom);
    })
    $(checkboxDom).on('click.item','li',function(){
      if($(this).find('.active').length) {
        $(this).find('.active').remove();
      }else {
        $(this).append(choiceDom);
      }
      let textArr = []
      $($(checkboxDom).find('li')).each(function(index,item) {
        const val = $(item).text();
        $(item).find('.active').length && textArr.push(val);
      })
      choiceArr = textArr;
      const text = textArr.join(';')
      $button.text(text);
      $button.attr('title',text);
    })
    $button.on('click', function (e) {
      e.stopPropagation();
      e.preventDefault();
      // 获取元素位置
      let offset = $(this).offset();
      $(checkboxDom).css({'left':offset.left,'top':offset.top+28,'position':'absolute'}).find('.multiple').show();
    })
    $('.box').on('click', function () {
      console.log($box.find('.multiple').is(':hidden'))
      if ($box.find('.multiple').is(':hidden')) {
        const text = $button.text();
        $td.empty().text(text);
        $('body').unbind('click');
      } else {
        $(checkboxDom).find('.multiple').hide();
      }
    })
  }
</script>
<style>
  button {
    position: absolute;
    top: 200px;
    left: 400px;
  }
  .box {
    height: 100vh;
  }


.checkList{
  margin: 0;
  padding: 0;
}
.checkList li{
  padding: 3px 10px 3px 3px;
  list-style:none;
  color: #676a6c;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.checkList li .active{
  float: right;
}
.checkList .active::after{
  content:'√';
}
.multiple {
  padding: 8px 6px;
  border-radius: 4px;
  border: 1px solid #efeeee;
}
.multiple input{
  margin: 2px 4px;
}
.showBox {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 1px solid #676a6c;
  background-color: #fff;
  border-radius: 2px;
  outline: none;
  width: 80px;
  height: 21px;
  text-align: left;
}
</style>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值