手写一个下拉框

本文展示了如何使用Vue编写一个自定义的下拉框组件,包括输入框聚焦显示下拉列表、选中选项更新输入值等功能。通过点击元素实现选中效果,并应用过渡动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!--
 * @Descripttion: 手写一个下拉框
 * @version: 
 * @Author: zhangfan
 * @email: 2207044692@qq.com
 * @Date: 2021-09-28 18:14:16
 * @LastEditors: zhangfan
 * @LastEditTime: 2021-10-19 15:47:07
-->


<template>
  <div class="select-box">
    <el-input
      @focus="selectGoods"
      v-model="input"
      placeholder="请选择"
    ></el-input>
    <transition name="fade">
      <ul class="terminalList" v-show="isShow">
        <li
          v-for="(item, index) in options"
          :key="index"
          class="terminal-item"
          @click="selectEquipment(item, index)"
          :class="{ 'active-class': activeIndex === index }"
        >
          <span>{{ item.label }}</span>
        </li>
      </ul>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      activeIndex: "",
      input: "",
      options: [
        {
          value: "选项1",
          label: "黄金糕",
        },
        {
          value: "选项2",
          label: "双皮奶",
        },
        {
          value: "选项3",
          label: "蚵仔煎",
        },
        {
          value: "选项4",
          label: "龙须面",
        },
        {
          value: "选项5",
          label: "北京烤鸭",
        },
      ],
    };
  },
  methods: {
    selectGoods() {
      this.isShow = !this.isShow;
    },
    selectEquipment(item, index) {
      this.activeIndex = index;
      this.input = item.label;
      this.isShow = false;
    },
  },
};
</script>

<style  scoped lang="less">
.select-box {
  width: 125px;
  height: 100%;
}
.terminalList {
  width: 125px;
  overflow: hidden;
  position: absolute;
  left: 0px;
  top: 50px;
  background: linear-gradient(
    0deg,
    rgba(1, 16, 35, 0.8),
    rgba(25, 54, 91, 0.8),
    rgba(1, 16, 35, 0.8)
  );
  .terminal-item {
    width: 171px;
    height: 32px;
    color: #b6f4ff;
    margin: 7px auto;
    padding: 0px 12px;
    display: flex;
    flex-direction: row; /* 子元素横向排列 */
    justify-content: space-between; /* 相对父元素水平居中 */
    align-items: center;
    cursor: pointer;
  }
  .active-class {
    color: red;
    background-color: rgba(32, 156, 252, 0.4);
  }
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.8s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值