vue下拉选择框

 

 vue下拉选择框,自定义样式,

这是vue组件

<template>
  <div class="a">
    <span @click="handleDrop">{{active>-1?list[active].label:"请选择"}}</span>
    <div class="z">
      <ul v-show="visible" >
        <li v-for="(item,index) in list" :class="index===active?'active':''" @click="choose(item,index)" :key="index">
          {{item.label}}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: "mySelect",
  props:['list'],
  data() {
    return {
      visible: false,
      active:-1
    }
  },
  methods: {
    choose(item,index){
      this.visible=false;
      if(this.active!==index){
        this.active=index;

        this.$emit("selected",{
          index:index,
          label:item.label,
          value:item.value
        })
      }

    },
    handleDrop(){
      this.visible=!this.visible
    },
    outClick(e){
      let dropRef= this.$el
      if(!dropRef.contains(e.target)&&this.visible){
        this.visible=false
      }
    }
  },

  mounted(){
    document.addEventListener('click',this.outClick)
  },
  destroyed(){
    document.removeEventListener('click',this.outClick)
  }

}
</script>

<style scoped>
.a{
  position: relative;
  width: 200px;
  display: inline-block;
  text-align: center;
  user-select: none;

color: #125165;
}
.a>span{
  padding: 10px 20px;
  background-color: lightblue;
  display: block;
  box-shadow: inset 0 0 0 2px #125165;

  color:  #125165;
}
.z>ul{
  list-style: none;
  margin: 0;
   position: absolute;
  top:10px;
  padding-inline-start: 0;

  background-color: lightblue;
  left: 0;
  /*left 不包含border*/
  width: 100%;
  box-shadow: inset 0 0 0 2px #125165;

}

.z>ul>li{
  padding: 10px 0;
}
.z{
  position: relative;
}
.z>ul>li:hover,div>ul>li.active{
  background-color: #125165;
  color: lightblue;
}
</style>

这个是使用方法:

在引用页使用:

  <my-select2 @selected="useItem2" :list="fruits2"/>

数据格式:

      fruits2:[
        {
          label:'苹果',
          value:'apple',
          key:'12'
        },
        {
          label:'梨子',
          value:'pear',
          key:'13'
        },
        {
          label:'香蕉',
          value:'banana',
          key:'14'
        },
        {
          label:'橘子',
          value:'orange',
          key:'15'
        },
        {
          label:'柠檬',
          value:'lemon',
          key:'16'
        },
      ],

接受方法:

useItem2:function (o){
      console.log(o)
    },

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值