自定义select-box

创建控件


function newSelectBox(icoCls, items) {
  icoCls = icoCls ? icoCls : 'saveIcon';
  let sb = $(`
  <div class="selectBox" tabindex="1">
    <div class="icon ${icoCls}"></div>
    <div class="textOutterDiv">
      <div class="text"><label>Current Selection</label></div>
    </div>
    <div class="myButton">...Type List</div>
    <div class="dropList">
    </div>  
  </div>`);
  let isVisible = false;
  let dl = sb.children(".dropList");
  let doToggle = genToggleFunc(dl);
  let currentSel = sb.children(".textOutterDiv").children(".text").children("label");
  items.forEach((e) => {
    let item = $(`<div class="dropItem">${e}</div>`);
    dl.append(item);
    item.click(() => {
      // console.log(`You select ${e}`);
      console.log("Current-Select is %s", currentSel);
      currentSel.text(`click on ${e}`);
      doToggle(true);
    });
  });
  let tb = sb.children(".myButton");

  let ut = (isHidden) => {
    tb.text(isHidden ? "UnFold" : "Fold");
    if (!isHidden) {
      sb.focus();
    }
    // if (!isHidden) {
    //   //sb.focus(); // set focus to dl.
    //   //console.log("set focus to all");
    // }
  };
  tb.click(() => ut(doToggle()));
  // Do not use toggle button's blur.
  //tb.blur(() => ut(doToggle(true)));
  sb.blur(() => {
    ut(doToggle(true));
    console.log("lost focus");
  });
  return sb;
}

function initTest() {
  let fv = $(".testFieldView");
  fv.append(newSelectBox("saveIcon", [
    "Uno", "Dos", "Tres", "Cuatro", "Cinco",
    "Seis", "Siete", "Ocho", "Nueve", "Diez"
  ]));
}

相应的css设置:


.testFieldView {
    width: 100%;
    height: 900px;
    margin-top: 50px;
    /* background: lightgreen; */
    border-style: dashed;
    border-width: 1px;
}

.selectBox {
    margin: 10px;
    position: relative;
    width: 600px;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    height: 40px;
}

.selectBox > .icon.saveIcon {
    outline: 1px;
    outline-color: cyan;
    width: 64px;
    height: 100%;
    display: inline-block;
    background: url("./svgs/saveFile.svg") center center no-repeat;
}

.selectBox > .textOutterDiv {
    height: 100%; 
    width: 200px; 
    display: inline-block;
}

.selectBox .textOutterDiv .text {
    position: absolute;
    height: 50%;
    /* text-align: center; */
    top: 50%;
    bottom: 50%;
}

/* .selectBox button {
    height: 100%;
    position: absolute;
    right: 10px;
    border-radius: 9px;
    width: 160px;
    background: lightcyan;
    text-transform: capitalize;
} */

/* .selectBox button:focus {
    outline: 0;
} */

.selectBox .dropList {
    width: 100%;
    display: none;
    position: absolute;
    top: 40px;
}

.selectBox .myButton {
    height: 100%;
    /*display: inline-block;*/
    border-style: solid;
    border: 1px;
    border-radius: 4px;
    border-color: red;
    background: green;
    float: right;
    width: 120px;
}

/* Common */
.selectBox > .dropList .dropItem {
    width: 100%;
    height: 32px;
    border-left: 1px solid;
    border-right: 1px solid;
}

/* LoveHated :link visited hover active */
.selectBox > .dropList .dropItem:hover {
    background: grey;
}

.selectBox > .dropList .dropItem:first-child {
    /* border-radius: 6px; */
    border-top-left-radius: 6px;
    border-top-right-radius: 6px;
    border-top: 1px solid;
}

.selectBox > .dropList .dropItem:last-child {
    /* border-radius: 6px; */
    border-bottom: 1px solid;
    border-bottom-left-radius: 6px;
    border-bottom-right-radius: 6px;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值