vue自定义选择器

//组件部分
<template>
  <div>
    <div>
      <div class="SelectBackground" :class="{
        SelectBackgrounds: BackgroundShow == 2,
        SelectBackgroundss: BackgroundShow == 3,
      }" @click.stop="ShowSelects">
        <span class="SelectText" :style="{ width: label.length == 5 ? '70px' : '' }"> {{ labels ? label+'/'+labels : label }} </span>
        <div class="SelectIcons" v-if="ShowSelect"></div>
        <div class="SelectIcon" v-else></div>
        <span class="SelectLinear" :style="{ right: right }"></span>
        <span class="SelectLinears" :style="{ left: left }"></span>

        <!-- 下拉选择器 -->
        <div class="ShowBackground scale-up-ver-top" v-show="ShowSelect" :class="{
          show: ShowSelect,
          'scale-down-ver-bottom ': !ShowSelect,
          hide: !ShowSelect,
        }" :style="{ width: width, height: height }">
          <div class="ShowTitle">{{ title }}</div>
          <div class="ShowItem" v-for="(item, index) in options" :key="index">
            <div @click.stop="selectItem(item)" :class="{ selected: item.value == selectedOption }">
              {{ item.value }}
            </div>
          </div>
        </div>
        <!-- 二级选择器 -->
        <div class="ShowBackgrounds scale-up-hor-left" v-show="ShowSelectClass" :class="{showLine: ShowSelectClass,}" >
          <div class="ShowItem" v-for="(item, index) in clickData.gradeInfo" :key="index">
            <div @click.stop="selectItemS(item)" :class="{ selected: item.value == selectedOptions }">
              {{ item.value }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    // label: String,
    options: {
      type: Array,
      default: () => [],
    },
    title: String,
    BackgroundShow: {
      type: Number,
      default: 1,
    },
    left: String,
    right: String,
    width: String,
    height: String,
  },
  data() {
    return {
      label: '',
      labels:'',
      selectedOption: null,
      selectedOptions: null,
      ShowSelect: false,
      ShowSelectClass:false,
      clickData: '',
      optionsData: [{
        value: '暂无数据',
      }],
    };
  },
  mounted() {
    document.addEventListener('click', this.handleDocumentClick);
  },

  created() {
    this.getPosition();
  },

  watch: {
    options() {
      this.getPosition();
    },
  },

  beforeDestroy() {
    document.removeEventListener('click', this.handleDocumentClick);
  },
  methods: {
    /**
     * 获取传递的数据
     * **/
    getPosition() {
      this.label = this.options ?this.options[0].gradeInfo?this.options[0].value+'/'+this.options[0].gradeInfo[0].value : this.options[0].value : '暂无数据';
      this.selectedOption = this.options ? this.options[0].value : '暂无数据';
      this.selectedOptions = this.options ? this.options[0].value : '暂无数据';
    },
    /**
     * 点击任意位置关闭
     * */
    handleDocumentClick(event) {
      const dropdownElement = this.$el.querySelector('.SelectBackground');
      if (dropdownElement && !dropdownElement.contains(event.target)) {
        this.ShowSelect = false;
        this.ShowSelectClass = false
      }
    },
    /**
     * 点击回收
     * **/
    ShowSelects() {
       // 触发关闭其他下拉框的事件
      this.$emit('closeOtherDropdowns');
      this.ShowSelect = !this.ShowSelect;
      this.ShowSelectClass = false;
    },
    selectItem(item) {
      this.clickData = item
      this.labels=''
      this.$emit('changeData', item);
      this.selectedOption = item ? item.value : '暂无数据';
      this.label = this.selectedOption;

      // 判断是否有二级
      if (item.gradeInfo && item.gradeInfo[0]) {
      this.selectItemS(item.gradeInfo[0],1)
      this.ShowSelectClass = true; // Close the dropdown after selecting an item
      } else {
        this.ShowSelect = false;
      }
      console.log(item,456)
    },
    selectItemS(item,data) {
      this.$emit('changeClassData', item);
      this.selectedOptions = item ? item.value : '暂无数据';
      this.labels = this.selectedOptions;
      if (data!=1) {
      this.ShowSelect = false;
      }
      this.ShowSelectClass = false
    }
  },
};
</script>

<style scoped lang="scss">
.SelectBackground {
  background: url(../../assets/image/SelectBackground.png);
  width: 90px;
  height: 30px;
  background-size: 100%;
  /* 防止背景图在页面滚动时重复显示 */
  background-attachment: fixed;
  display: flex;
  align-items: center;
  justify-content: space-around;

  position: relative;
  z-index: 9999;
  cursor: pointer;
}

.SelectBackgrounds {
  background: url(../../assets/image/BigSelectBackground.png);
  width: 123px;
  height: 36px;
  background-size: 100%;
  /* 防止背景图在页面滚动时重复显示 */
  background-attachment: fixed;
  display: flex;
  align-items: center;
  justify-content: space-around;
  position: relative;
  // margin-right: 20px;
}

.SelectBackgroundss {
  background: url(../../assets/image/LongSelectBackground.png);
  width: 183px;
  height: 36px;
  background-size: 100%;
  /* 防止背景图在页面滚动时重复显示 */
  background-attachment: fixed;
  display: flex;
  align-items: center;
  justify-content: space-around;
  position: relative;
}

.SelectText {
  color: #c7d6ec;
  text-align: center;
  font-family: PingFang SC;
  font-size: 12px;
  font-style: normal;
  font-weight: 400;
  line-height: 13.2px;
  /* 110.002% */
  letter-spacing: 0.721px;
}

.SelectIcon {
  background: url(../../assets/image/SelectBottom.png);
  width: 28px;
  height: 20px;
  background-size: 100%;
}

.SelectIcons {
  background: url(../../assets/image/SelectTop.png);
  width: 28px;
  height: 20px;
  background-size: 100%;
}

.SelectLinear {
  width: 37px;
  height: 1px;
  position: absolute;
  top: 0;
  right: 45px;
  background: linear-gradient(90deg,
      rgba(60, 89, 207, 0) 0%,
      rgba(1, 132, 252, 0) 0.01%,
      #00fcda 45.83%,
      rgba(1, 134, 253, 0.01) 100%);
  filter: blur(0.3604423999786377px);
}

.SelectLinears {
  width: 37px;
  height: 1px;
  position: absolute;
  bottom: 0;
  left: 46px;
  background: linear-gradient(90deg,
      rgba(60, 89, 207, 0) 0%,
      rgba(1, 132, 252, 0) 0.01%,
      #00fcda 45.83%,
      rgba(1, 134, 253, 0.01) 100%);
  filter: blur(0.3604423999786377px);
}

.ShowBackground {
  position: absolute;
  top: 40px;
  background: url(../../assets/image/ShowBackground.png);
  width: 90px;
  height: 180px;
  // max-height: 0;
  background-size: 100%;
  overflow: auto;
  -webkit-animation: scale-up-ver-top 0.3s ease;
  animation: scale-up-ver-top 0.3s ease;
  -webkit-transform-origin: top;
  transform-origin: top;
  -webkit-transform: scaleY(0);
  transform: scaleY(0);
  z-index: 99;
   /* 隐藏滚动条 */
 scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE 10+ */

  .ShowTitle {
    // text-align: center;
    color: #e7f1ff;
    // text-align: center;
    font-family: PingFang SC;
    font-size: 12px;
    font-style: normal;
    font-weight: 400;
    // line-height: 13.2px;
    /* 110.002% */
    letter-spacing: 0.721px;
    margin: 10px;
  }

  .ShowItem {
    // height: 24px;
    // text-align: center;
    color: #e7f1ff;
    font-family: PingFang SC;
    font-size: 12px;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
    /* 110.002% */
    letter-spacing: 0.721px;
    // margin: 0 0 0 12px;
    padding: 10px;
  }


  .selected {
    color: #1890ff;
  }
}


@-webkit-keyframes scale-up-ver-top {
  0% {
    -webkit-transform: scaleY(0.4);
    transform: scaleY(0.4);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }

  100% {
    -webkit-transform: scaleY(1);
    transform: scaleY(1);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
}

@keyframes scale-up-ver-top {
  0% {
    -webkit-transform: scaleY(0.4);
    transform: scaleY(0.4);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }

  100% {
    -webkit-transform: scaleY(1);
    transform: scaleY(1);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
}

.ShowBackground.show {
  /* 展开状态 */
  // max-height: 180px; /* 根据实际内容的最大高度调整 */
  -webkit-transform: scaleY(1);
  transform: scaleY(1);
}

@-webkit-keyframes scale-down-ver-bottom {
  0% {
    -webkit-transform: scaleY(1);
    transform: scaleY(1);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }

  100% {
    -webkit-transform: scaleY(0.3);
    transform: scaleY(0.3);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
}

@keyframes scale-down-ver-bottom {
  0% {
    -webkit-transform: scaleY(1);
    transform: scaleY(1);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }

  100% {
    -webkit-transform: scaleY(0.3);
    transform: scaleY(0.3);
    -webkit-transform-origin: 100% 0%;
    transform-origin: 100% 0%;
  }
}

.ShowBackground.hide {
  -webkit-animation: scale-down-ver-bottom 0.5s ease;
  animation: scale-down-ver-bottom 0.5s ease;
  -webkit-transform: scaleY(0);
  transform: scaleY(0);
}

.ShowBackground::-webkit-scrollbar {
  width: 0;
}
.ShowBackgrounds{
  background: url(../../assets/image/ShowBackground.png);
  width: 123px;
  height: 246px;
  // max-height: 0;
  background-size: 100%;
  overflow: auto;
  position: absolute;
  top: 40px;
  right: -130px;
  -webkit-animation: scale-up-hor-left 0.3s ease;
  animation: scale-up-hor-left 0.3s ease;
  -webkit-transform-origin: left;
  transform-origin:left;
  -webkit-transform: scaleX(0);
  transform: scaleX(0);
  z-index: 99;
   /* 隐藏滚动条 */
 scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE 10+ */
  .ShowItem {
    // height: 24px;
    // text-align: center;
    color: #e7f1ff;
    font-family: PingFang SC;
    font-size: 12px;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
    /* 110.002% */
    letter-spacing: 0.721px;
    // margin: 0 0 0 12px;
    padding: 10px;
  }


  .selected {
    color: #1890ff;
  }
}
@-webkit-keyframes scale-up-hor-left {
  0% {
    -webkit-transform: scaleX(0.4);
            transform: scaleX(0.4);
    -webkit-transform-origin: 0% 0%;
            transform-origin: 0% 0%;
  }
  100% {
    -webkit-transform: scaleX(1);
            transform: scaleX(1);
    -webkit-transform-origin: 0% 0%;
            transform-origin: 0% 0%;
  }
}
@keyframes scale-up-hor-left {
  0% {
    -webkit-transform: scaleX(0.4);
            transform: scaleX(0.4);
    -webkit-transform-origin: 0% 0%;
            transform-origin: 0% 0%;
  }
  100% {
    -webkit-transform: scaleX(1);
            transform: scaleX(1);
    -webkit-transform-origin: 0% 0%;
            transform-origin: 0% 0%;
  }
}
.ShowBackgrounds.showLine {
  /* 展开状态 */
  // max-height: 180px; /* 根据实际内容的最大高度调整 */
  -webkit-transform: scaleX(1);
  transform: scaleX(1);
}
</style>

 父组件使用

  <Select
                ref="firstSelect"
                  v-if="selectorOptionss"
                  :options="selectorOptionss"
                  style="margin-right: 25px;"
                  :BackgroundShow="3"
                  :left="'132px'"
                  :right="'132px'"
                  :width="'183px'"
                  :height="'366px'"
                  @changeData="getterm"//传递数据的方法
                  @closeOtherDropdowns="closeOtherDropdowns"//异步关闭的方法
                />
                <Select
                ref="secondSelect"

                  v-if="selectorOptions"
                  :options="selectorOptions"
                  style="margin-right: 25px;"
                  :BackgroundShow="2"
                  :left="'72px'"
                  :right="'72px'"
                  :width="'123px'"
                  :height="'246px'"
                  @changeData="getGradeData"
                  @changeClassData="getclass"
                  @closeOtherDropdowns="closeOtherDropdowns"
                />
                <Select
                ref="thirdSelect"

                  v-if="selectorOptionsSubject"
                  :options="selectorOptionsSubject"
                  :BackgroundShow="2"
                 
                  :width="'123px'"
                  :height="'246px'"
                  @changeData="getsubject"
                  @closeOtherDropdowns="closeOtherDropdowns"
                />
  /**
     * 处理数据
     * **/
    handleData() {
      // let gradeData = this.$store.state.gradeData;
      const subjectInfo = this.GradeData.gradeInfo.map((gradeItem) => ({
        ...gradeItem,
        value: gradeItem.gradeName,
        subjectInfo: gradeItem.subjectInfo
          ? gradeItem.subjectInfo.map((subjectItem) => ({
              ...subjectItem,
              value: subjectItem.subjectName,
            }))
          : [],
      }));

      this.selectorOptionsClass =
        subjectInfo.length > 0 ? subjectInfo : [{ value: '暂无数据' }];
      this.selectorOptionsSubject = subjectInfo[0]
        ? subjectInfo[0].subjectInfo
        : [{ value: '暂无数据' }];
      // this.getData();
      // console.log(subjectInfo, '4566');
    },  /**
     * 处理数据
     * **/
    handleData() {
      // let gradeData = this.$store.state.gradeData;
      const subjectInfo = this.GradeData.gradeInfo.map((gradeItem) => ({
        ...gradeItem,
        value: gradeItem.gradeName,
        subjectInfo: gradeItem.subjectInfo
          ? gradeItem.subjectInfo.map((subjectItem) => ({
              ...subjectItem,
              value: subjectItem.subjectName,
            }))
          : [],
      }));

      this.selectorOptionsClass =
        subjectInfo.length > 0 ? subjectInfo : [{ value: '暂无数据' }];
      this.selectorOptionsSubject = subjectInfo[0]
        ? subjectInfo[0].subjectInfo
        : [{ value: '暂无数据' }];
      // this.getData();
      // console.log(subjectInfo, '4566');
    },  
 /***
     * 选择的班级
     * ***/
    getclass(data) {
      // this.selectorOptionsSubject = data.subjectInfo;//控制是否重置科目
      // 班级
      this.$store.commit('setClassData', data);
      // this.$store.commit('setSubjectData', data.subjectInfo[0]);//控制是否重置科目
        // 作业负担
        this.$refs.WorkCheck.getData();
        // 作业质量监测
        this.$refs.WorkQuality.getData();
        // 作业精辅导
        this.$refs.WorkBear.getData();
        // 教师积极性
        this.$refs.TeacherActive.getData();
        // 教师教学行为统计
        this.$refs.TeachingBehavior.getData();
        // 教学质量和作业质量对比
        this.$refs.TeacherBehavior.getData();
        // 学生层次分布
        this.$refs.StudentGradation.getData();

        // 单元测试
        this.$refs.TeachTest.getData();
        // 数字资源建设
        this.$refs.TeacherNumber.getData();

      console.log(data);
    },
      closeOtherDropdowns() {
    // 关闭其他下拉框
    const dropdowns = [
    this.$refs.firstSelect,
    this.$refs.secondSelect,
    this.$refs.secondSelectClass,
    this.$refs.thirdSelect,
  ];


  dropdowns.forEach(dropdown => {
    if (dropdown) {
      dropdown.ShowSelect = dropdown.ShowSelect ? true : false;
    }
  });
    },

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值