40,自定义组件

1.自定义下拉菜单

<template>
  <div class="dropdown" @mouseleave.stop="handleMouseenLever">
    <span @mouseenter="hanldeMouseenterEnter">下拉菜单 <i v-show="isShowArrow==false" class="el-icon-arrow-up"></i> <i
        class="el-icon-arrow-down" v-show="isShowArrow==true"></i></span>
    <div class="dropdown-content" v-show="isShowContent">
      <div class="item" v-for="(item,index) in list" :key='index' @click="handleClick(item)">{{item.title}}</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShowContent: false,
      isShowArrow: false,
      list: [
        {
          title: "蛋糕",
          id: "1",
        },
        {
          title: "面包",
          id: "2",
        },
      ],
    };
  },
  methods: {
    hanldeMouseenterEnter() {
      this.isShowContent = true;
      this.isShowArrow = true;
    },
    handleMouseenLever() {
      //this.isShowContent = false;
      this.isShowArrow = false;
    },
    handleClick(item) {
      this.$emit("input", item.id);
    },
  },
};
</script>

<style lang='scss'  scoped>
.dropdown {
  position: relative;
  display: inline-block;
  height: 32px;
  line-height: 32px;
  padding: 0 20px;
  min-width: 120px;
  border: 1px solid #ccc;
  box-sizing: border-box;
  cursor: pointer;
}
.dropdown-content {
  width: 100%;
  display: none;
  position: absolute;
  left: 0;
  top: 33px;
  background-color: #fff;
  //  min-width: 120px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  text-align: left;
  box-sizing: border-box;
  padding: 15px 0;
  .item {
    height: 32px;
    line-height: 32px;
    width: 100%;
    padding-left: 20px;
    box-sizing: border-box;
    &:hover {
      background: rgba(241, 230, 230, 0.5);
    }
  }
}
.dropdown:hover .dropdown-content {
  display: block;
}
</style>

2.父组件

 <DropDown v-model='title' @input="handleInput" />

2.自定义下拉选icon菜单

1.子组件
<template>
  <div class="iconWrap" @mouseleave="handleMouseLeave">
    <div class="dropdown" @mouseenter="handlMouseenter">
      <el-input
        type="text"
        v-model="icon"
        :prefix-icon="icon"
        class="icon"
        @focus="handleFocus"
        placeholder="请选择"
      />
      <div class="dropdown-content" v-show="isShowContent">
        <div class="item">
          <i
            v-for="(item, index) in list"
            :key="index"
            @click="handleClick(item)"
            :class="item.icon"
          ></i>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value:{
      type:String
    }
  },
  //接受父级传入过来的值
  mounted(){
    this.icon = this.value;
  },
  data() {
    return {
      isShowContent: false,
      icon: "",
      list: [
        {
          icon: "el-icon-edit",
          id: "1",
        },
        {
          icon: "el-icon-share",
          id: "2",
        },
        {
          icon: "el-icon-platform-eleme",
          id: "3",
        },
        {
          icon: "el-icon-eleme",
          id: "4",
        },
        {
          icon: "el-icon-delete-solid",
          id: "5",
        },
        {
          icon: "el-icon-delete",
          id: "6",
        },
        {
          icon: "el-icon-s-tools",
          id: "7",
        },
        {
          icon: "el-icon-setting",
          id: "8",
        },
        {
          icon: "el-icon-user-solid",
          id: "9",
        },
        {
          icon: "el-icon-user",
          id: "10",
        },
        {
          icon: "el-icon-phone",
          id: "11",
        },
      ],
    };
  },
  methods: {
    handleFocus() {
      this.isShowContent = true;
    },
    handleBlur() {},
    handlMouseenter() {
      this.isShowContent = true;
    },
    handleMouseLeave() {
      this.isShowContent = false;
    },
    handleClick(item) {
      console.log(item)
      // this.value = "";
      this.icon = item.icon;
      this.$emit("input", item.icon);
      this.isShowContent = false;
    },
  },
};
</script>

<style lang='less'  scoped>
.iconWrap {
  width: 200px;
  .dropdown {
    position: relative;
    box-sizing: border-box;
    cursor: pointer;
    z-index: 99;
    .inputClass {
      line-height: 32px;
      height: 32px;
      border-radius: 2px;
      width: 100%;
    }
    .dropdown-content {
      width: 200px;
      position: absolute;
      left: 0;
      top: 33px;
      background-color: #fff;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
      text-align: left;
      max-height: 200px;
      box-sizing: border-box;
      overflow-y: scroll;
      border-radius: 2px;
      .item {
        width: 100%;
        box-sizing: border-box;
        color: #000;
        display: flex;
        flex-wrap: wrap;
        padding: 0 10px 0 10px;
        i {
          display: inline-block;
          width: 32px;
          height: 32px;
          line-height: 32px;
          text-align: center;
          &:hover {
            background: rgba(241, 230, 230, 0.5);
            height: 32px;
            line-height: 32px;
          }
        }
      }
    }
  }
}
</style>

2.父级组件

  <SelectIcon v-model="title2" @input="handleInput2" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值