1. 程式人生 > 實用技巧 >下拉框的下拉,點選其他地方下拉消失

下拉框的下拉,點選其他地方下拉消失

<template>
  <div ref="dateWrap" class="dateWeek-wrap" @click="showList">
    <div class="date-input">
      <span>{{ defaultVal ? "周" + arr[defaultVal - 1] : defaultVal }}</span>
      <img style="width: 14px" src="@/assets/images/icon-date.png" alt />
      <ul class="list clearfix" v-show="this.isShow">
        <li class="list-1">請選擇每週幾結算</li>
        <li
          :key="item"
          :class="[{ active: item == arr[defaultVal - 1] }]"
          @click.stop="clickItem(item, index)"
          v-for="(item, index) in arr"
        >
          {{ item }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      arr: ["一", "二", "三", "四", "五", "六", "日"],
    };
  },
  props: ["defaultVal"],
  mounted() {
    document.addEventListener("click", (e) => {
      if (!this.$el.contains(e.target)) this.isShow = false;
    });
  },
methods: { showList() { this.isShow = !this.isShow; }, clickItem(val, index) { this.$emit("salaryWeekVal", index + 1); this.isShow = false; }, }, }; </script> <style lang="scss" scoped> .dateWeek-wrap { .date-input { width: 230px; height: 40px; border-radius: 4px; border: 1px solid #eeeeee; position: relative; cursor: pointer; & > span { padding-left: 12px; } img { position: absolute; right: 12px; top: 13px; } .list { position: absolute; top: 42px; left: 0; width: 100%; padding: 7px 4px; background: #fff; z-index: 9; margin-bottom: 0; border: 1px solid #eeeeee; li { width: 31px; height: 24px; line-height: 24px; text-align: center; float: left; cursor: pointer; &.active, &:hover { color: #fff; background: #648df5; box-shadow: 0px 2px 6px 0px rgba(171, 171, 171, 0.5); border-radius: 1px; } } .list-1 { width: 100%; padding: 7px 0 14px; text-align: center; height: auto; font-size: 14px; color: #999999; &:hover { color: #999999; background: #fff; box-shadow: none; border-radius: 0; } } } } } </style>