• vue 下拉框自定义 以及点击空白空白处关闭下拉框


    html:

    <div class="divInput" v-close>
        <div class="input" @click="opensort">
          <input v-model="sortvalue" type="text" placeholder="分类" />
          <vicon :type="'triangle'" class="topimg" />
        </div>
        <div class="list" v-show="show">
          <ul>
            <li
              @click="getvalue(index, sortitem)"
              v-for="(sortitem, index) in tableData"
              :key="sortitem.index"
            >
              {{ sortitem.name }}
            </li>
          </ul>
        </div>
      </div>
    View Code

    js:

    <script>
    export default {
      name: "docselect",
      data() {
        return {
          tableData: [
            {
              name: "专业论文"
            },
            {
              name: "植物设计"
            },
            {
              name: "景观设计"
            },
            {
              name: "规划研究"
            },
            {
              name: "生态理念"
            },
            {
              name: "施工技术"
            },
            {
              name: "工程管理"
            }
          ],
          show: false,
          sortvalue: ""
        };
      },
      methods: {
        opensort() {
          this.show = !this.show;
        },
        getvalue(index, sortitem) {
          this.sortvalue = sortitem.name;
          this.show = false;
        }
      },
      components: {
        vicon
      }
    };
    </script>
    View Code

     css:

    <style>
    .divInput {
       78px;
      height: auto;
      /* margin-left: 48px; */
      display: inline-block;
    }
    ul li {
      list-style: none;
    }
    .input {
       74px;
      height: 20px;
      line-height: 40px;
      padding-left: 5px;
      /* border: 1px solid #cccccc; */
      position: relative;
      cursor: pointer;
    }
    .input input:focus {
      outline: 0 !important;
    }
    .input input {
      border: none;
      outline: none;
       55px;
      float: left;
      margin: auto;
      cursor: pointer;
      margin-top: 2px;
    }
    
    .input img {
      position: absolute;
      right: 34px;
      top: 48%;
    }
    .list {
      background: #ffffff;
       72px;
      overflow: hidden;
      position: absolute;
      box-shadow: 0px 3px 6px rgba(191, 191, 191, 1);
      opacity: 1;
      z-index: 1;
      display: block;
    }
    .list.close {
      display: none;
    }
    .topimg {
       18px;
      height: 15px;
      float: left;
    }
    .list ul li {
      height: 30px;
      cursor: pointer;
      margin: 0px 4px 0px -35px;
      font-size: 12px;
    }
    .list ul li:hover {
      background-color: #e6e6e6;
    }
    </style>
    View Code

    然后发现下拉框点击空白处不关闭,然后在加上一个事件

      //点击空白处关闭下拉框的div事件
      directives: {
        close: {
          inserted(el, binding, vnode) {
            window.onclick = function(e) {
              if (!el.contains(e.target)) {
                vnode.context.show = false;
              }
            };
          }
        }
      },


    注意:这个事件和data是同级的

    效果图:

               

    最后发现如果两个下拉框就只能触发一个下拉框点击空白处关闭,之后在给事件修改一下,

    directives: {
        close: {
          inserted(el, binding, vnode) {
            window.addEventListener("click", e => {
              if (!el.contains(e.target)) {
                vnode.context.show = false;
              }
            });
          }
        }
      },


  • 相关阅读:
    javascript 获取鼠标在盒子中的坐标
    jquery中clientY, pageY, screenY的区别,最后三张图一目了然
    javascript 小清新颜色翻页效果
    javascript 缓动返回顶部案例
    原生js轮播图实现
    javascript Math对象
    javascript 获取节点元素的封装
    javascript 转换大小写字母
    2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1010
    2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1009
  • 原文地址:https://www.cnblogs.com/lovebear123/p/12069054.html
Copyright © 2020-2023  润新知