下拉框 组件 select-assembly

2 篇文章 0 订阅

1.组件存在位置

2.组件

<template>
  <div class="a">
    <div class="first"  @click="handleDrop">
      <div class="left"><span>{{active>-1?list[active].label:"请选择"}}</span></div>
      <div class="right"><div :class="visible?'b1':'b'"></div></div>
    </div>
    <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:0
      }
    },
    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: 100%;*/
    display: inline-block;
    text-align: center;
    user-select: none;

    width: 130rpx;
    height: 45rpx;
    background: #FFFFFF;
    box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(77,86,161,0.4), inset 0rpx 0rpx 12rpx 0rpx rgba(173,182,255,0.7);
    border-radius: 4rpx;

    font-size: 24rpx;
    font-family: MicrosoftYaHei;
    color: #2572FF;
    line-height: 45rpx;
  }
  .z>ul{
    list-style: none;
    margin: 0;
    position: absolute;
    /*top:10px;*/
    padding-inline-start: 0;

    /*background-color: lightblue;*/
    left: 0;
    /*left 不包含border*/
    width: 100%;
    /*height: 420rpx;*/
    background: rgba(255,255,255,0.1);
    box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(77,86,161,0.4);
    border-radius: 4rpx;
    border: 2rpx solid;
    border-image: linear-gradient(360deg, rgba(52, 121, 255, 1), rgba(52, 121, 255, 0)) 2 2;
    backdrop-filter: blur(10px);
  }

  .z>ul>li{
    padding: 3px 0;
    cursor: pointer;

    /*width: 72rpx;*/
    /*height: 24rpx;*/
    font-size: 24rpx;
    font-family: MicrosoftYaHei;
    color: #999999;
    line-height: 60rpx;
  }
  .z{
    position: relative;
    z-index: 9999;
  }
  div>ul>li.active{
    background-color:#3A7CFF;
    color: #DEF1FF
  }
  .z>ul>li:hover{
    background-color: #3A7CFF;
    color: #DEF1FF
  }

  /***  第一行显示适配  ****/
  .first{
    display: flex;
    flex-direction: row;
    width: 100%;
    height: 45rpx;
    background: #FFFFFF;
    box-shadow: 0rpx 4rpx 10rpx 0rpx rgba(77,86,161,0.4), inset 0rpx 0rpx 12rpx 0rpx rgba(173,182,255,0.7);
    border-radius: 4rpx;
    border: 1rpx solid #2572FF;
  }
  .left{
    width: 80%;
  }
  .right{
    width: 20%;
    align-items: center;
    display: flex;
    justify-content: space-around;
  }
  .b{
    background-image: url("../../assets/images/downSelect.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 16rpx;
    height: 11rpx;
  }

  .b1{
    background-image: url("../../assets/images/upSelect.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 16rpx;
    height: 11rpx;
  }
</style>

 3.引用及传值

import selectAssembly from "../../../components/select-title/selectAssembly";

export default {
  components:{
    bulletBox,
    selectAssembly
  },
}
<select-assembly @selected="selected" :list="choose"></select-assembly>
  methods: {
    selected:function (o){
      // console.log(o)
      this.city=o.label
    },
  }
data() {
    return {
      city: '溧阳市',
      choose: [
        {
          label: '溧阳市',
          value: '1',
          key: '1'
        },
        {
          label: '金坛区',
          value: '2',
          key: '2'
        },
        {
          label: '武进区',
          value: '3',
          key: '3'
        },
        {
          label: '新北区',
          value: '4',
          key: '4'
        },
        {
          label: '天宁区',
          value: '5',
          key: '5'
        },
        {
          label: '钟楼区',
          value: '6',
          key: '6'
        },
        {
          label: '经开区',
          value: '7',
          key: '7'
        }
      ],
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值