element-ui二次封装下拉菜单(支持选中图片)

样式效果

在这里插入图片描述

代码

<template>
  <div>
    <el-dropdown
      trigger="click"
      style="width:80px"
      @visible-change="changeRotate"
      @command="borderStyle"
    >
      <div class="selectColor">
        <div class="borderStyleBox ">
          <svg
            width="40"
            height="1"
            class="svg-path-type"
          >
            <path
              d="M1,0h98"
              stroke-width="222"
              stroke="#DC143C"
              :stroke-dasharray="strokeDasharray"
            ></path>
          </svg>
        </div>
        <div
          class="el-icon-arrow-up change"
          ref="rotate"
        ></div>
      </div>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item
          :command="item.id"
          v-for="item in strokeDasharrayOpitons"
          :key="item.id"
        >
          <div>
            <svg
              width="98"
              height="1"
              class="svg-path-type"
              v-html="item.className"
            >
              {{item.className}}
            </svg>
          </div>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script>
export default {
  data () {
    return {
      strokeDasharray: '0',
      strokeDasharrayOpitons: [
        { id: '0', className: ` <path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="0">`, },
        { id: '8,2', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="8,2"></path>` },
        { id: '6,3', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="6,3"></path>` },
        { id: '4,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="4,4"></path>` },
        { id: '2,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="2,4"></path>` },
        { id: '1', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="1"></path>` },
        { id: '8,4,2,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="8,4,2,4"></path>` },
        { id: '8,4,2,4,2,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="8,4,2,4,2,4"></path>` },
        { id: '8,4,8,4,2,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="8,4,8,4,2,4"></path>` },
        { id: '8,4,2,4,2,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="8,4,2,4,2,4"></path>` },
        { id: '10,4,2,4,2,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="10,4,2,4,2,4"></path>` },
        { id: '20,4,2,4', className: `<path d="M1,0h98" stroke-width="222" stroke="#DC143C" stroke-dasharray="20,4,2,4"></path>` },
      ],
    }
  },
  methods: {
    borderStyle (item) {
      this.strokeDasharray = item
      this.doActions('strokeDasharray', item)
    },
    changeRotate(row){
      if(row){
      this.$refs.rotate.className = "el-icon-arrow-up change rotate"
      }else{
      this.$refs.rotate.className = "el-icon-arrow-up change"
      }
    },
  },
  components: {

  }
}
</script>

<style scoped lang="less">
.selectColor {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  border-radius: 6px;
  cursor: pointer;
  .borderStyleBox{
    width: 48px;
    height: 20px;
    border-radius: 4px;
    margin: 0px 4px;
    display: flex;
    align-items: center;
  }
  .change{
    transition: all .5s;
    transform: rotate(180deg);
    margin-right: 10px !important;
    color:#dccfce ;
  }
  .rotate{
    transform: rotate(0deg);
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的封装示例: 1. 在 `components` 目录下新建一个 `select` 目录,然后在 `select` 目录下新建两个文件 `select.wxml` 和 `select.js`。 2. 在 `select.wxml` 文件中编写下拉选择器的模板代码,示例如下: ``` <view class="el-select" bindtap="onSelectTap"> <view class="el-select__input"> <input type="text" class="el-select__inner" placeholder="{{placeholder}}" disabled="{{disabled}}" value="{{selectedText}}"> <i class="el-select__caret el-icon-arrow-down"></i> </view> <view class="el-select-dropdown" hidden="{{!showDropdown}}" catchtouchmove="catchTouchMove"> <scroll-view scroll-y="{{true}}" class="el-select-dropdown__list"> <view class="el-select-dropdown__item" wx:for="{{options}}" wx:key="{{item.value}}" bindtap="onOptionTap" data-value="{{item.value}}"> {{item.label}} </view> </scroll-view> </view> </view> ``` 其中,`placeholder` 表示选择器的占位符,`disabled` 表示选择器是否禁用,`selectedText` 表示当前选中的文本,`showDropdown` 表示下拉框是否显示,`options` 表示下拉框的选项列表。 3. 在 `select.js` 文件中编写组件的逻辑代码,示例如下: ``` Component({ properties: { options: { type: Array, value: [] }, placeholder: { type: String, value: '' }, disabled: { type: Boolean, value: false }, value: { type: [String, Number], value: '', observer: 'onValueChange' } }, data: { selectedValue: '', selectedText: '', showDropdown: false }, methods: { onValueChange: function(newVal) { var options = this.data.options; var selectedItem = options.find(function(item) { return item.value == newVal; }); if (selectedItem) { this.setData({ selectedValue: selectedItem.value, selectedText: selectedItem.label }); } }, onSelectTap: function() { if (this.data.disabled) { return; } this.setData({ showDropdown: !this.data.showDropdown }); }, onOptionTap: function(event) { var value = event.currentTarget.dataset.value; this.setData({ selectedValue: value, selectedText: this.data.options.find(function(item) { return item.value == value; }).label, showDropdown: false }); this.triggerEvent('change', { value: value }); }, catchTouchMove: function() { // 阻止滚动穿透 } } }); ``` 其中,`options` 表示下拉框的选项列表,`placeholder` 表示选择器的占位符,`disabled` 表示选择器是否禁用,`value` 表示当前选中的值,`selectedValue` 表示当前选中的值,`selectedText` 表示当前选中的文本,`showDropdown` 表示下拉框是否显示。 4. 在需要使用下拉选择器的页面中,引入 `select` 组件并传入相关参数,示例如下: ``` <import src="../../components/select/select.wxml" /> <template is="select" data="{{ options: options, placeholder: '请选择', value: value }}" /> ``` 其中,`options` 表示下拉框的选项列表,`placeholder` 表示选择器的占位符,`value` 表示当前选中的值。 5. 在需要监听选择器值改变的页面中,使用 `bind:change` 绑定事件即可,示例如下: ``` <import src="../../components/select/select.wxml" /> <view class="page"> <template is="select" data="{{ options: options, placeholder: '请选择', value: value }}" bind:change="onSelectChange" /> </view> ``` 其中,`onSelectChange` 表示选择器值改变时触发的事件处理函数。 这只是一个简单的示例,如果需要更多功能可以根据实际需求进行扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值