Vue ElementUI el-select 图片选择效果

该文章展示了如何利用ElementUI的el-select组件结合Vue.js,创建一个显示图片的下拉选择列表,选中后还能展示已选图片的效果。通过在HTML中嵌入图片,并用CSS预处理语言Stylus进行样式调整,实现了选中项的高亮和定位。
摘要由CSDN通过智能技术生成

概述

使用 Element UI el-select 组件制作图片选择效果。两个要点:

  1. 拉下列表option展示图片;
  2. 选中后展示已选图片;

效果

在这里插入图片描述

实现

Html

  • 代码第 4-19 行,这是一个整体;
  • 代码第 6-13 行,select 选择界面;
  • 代码第 15-18 行,选中后的展示界面;
<template>
<div class="main">
    <!-- 这是一个整体 -->
    <div class="img-select">
        <!-- select 效果 -->
        <el-select v-model="selectedIdx">
            <el-option v-for="index in iconIdxs" :key='index' :value="index">
                <div class="img-option">
                    <img :src="require(`@images/select/zoomin${index}.png`)"/>
                    <img :src="require(`@images/select/zoomout${index}.png`)"/>
                </div>
            </el-option>
        </el-select>
        <!-- 选中后的列表 -->
        <div class="img-option img-selected">
            <img :src="require(`@images/select/zoomin${selectedIdx}.png`)"/>
            <img :src="require(`@images/select/zoomout${selectedIdx}.png`)"/>
        </div>
    </div>
</div>
</template>

CSS

  • 使用 CSS预处理语言 stylus,也可以是其它语言,如:sassscss等;
  • 代码第 4 行,必须使用 position: relative,为选中样式做准备;
  • 代码第 19 行,选中样式必须使用 position: absolute,相对class="img-select"做定位;
  • 代码第 8 行,将选中值颜色做透明,特别是图片有透明背景的情况下;
<style lang="stylus" scoped>
.img-select {
    margin:30px;
    position: relative;
    .el-select {
        width:150px
        >>> .el-input__inner {
            color: rgba(0,0,0,0);
        }
    }
    .img-option {
        img {
            width: 30px;
            height: 30px;
            margin-right: 2px;
        }
    }
    .img-selected {
        position:absolute;
        top:5px;
        left:10px
    }
}
</style>

完整代码

<template>
<div class="main">
    <!-- 这是一个整体 -->
    <div class="img-select">
        <!-- select 效果 -->
        <el-select v-model="selectedIdx">
            <el-option v-for="index in iconIdxs" :key='index' :value="index">
                <div class="img-option">
                    <img :src="require(`@images/select/zoomin${index}.png`)"/>
                    <img :src="require(`@images/select/zoomout${index}.png`)"/>
                </div>
            </el-option>
        </el-select>
        <!-- 选中后的列表 -->
        <div class="img-option img-selected">
            <img :src="require(`@images/select/zoomin${selectedIdx}.png`)"/>
            <img :src="require(`@images/select/zoomout${selectedIdx}.png`)"/>
        </div>
    </div>
</div>
</template>

<script>
export default {
    data() {
        return {
            // 图片下标数组
            iconIdxs: [0,1,2,3],
            // 选中的下标
            selectedIdx: 0,
        }
    },
    watch: {
        selectedIdx(i) {
            console.log(`当前选中第 ${i+1} 个`);
        }
    },
}
</script>
<style lang="stylus" scoped>
.img-select {
    margin:30px;
    position: relative;
    .el-select {
        width:150px
        >>> .el-input__inner {
            color: rgba(0,0,0,0);
        }
    }
    .img-option {
        img {
            width: 30px;
            height: 30px;
            margin-right: 2px;
        }
    }
    .img-selected {
        position:absolute;
        top:5px;
        left:10px
    }
}
</style>

重点

可在此下载项目资源

ElementUI 提供了一个 `el-upload` 组件来实现图片上传功能,以下是一个简单的示例: ```html <template> <el-upload class="upload-demo" action="//jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :on-success="handleSuccess" :before-upload="beforeUpload" :file-list="fileList" :auto-upload="false" list-type="picture" :headers="{'Authorization': token}" :data="{userId: userId}" :multiple="true" :limit="3" :on-exceed="handleExceed" :show-file-list="false" > <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </template> <script> export default { data() { return { fileList: [], token: 'your_token', userId: 'your_userId' } }, methods: { handleSuccess(response, file, fileList) { console.log(response, file, fileList); }, handlePreview(file) { console.log(file); }, handleRemove(file, fileList) { console.log(file, fileList); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('上传头像图片只能是 JPG/PNG 格式!'); } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!'); } return isJPG && isLt2M; }, handleExceed(files, fileList) { this.$message.warning(`当前限制选择 ${this.limit} 张图片,本次选择了 ${files.length} 张图片,共选择了 ${files.length + fileList.length} 张图片`); } } } </script> <style> .upload-demo { margin-top: 20px; } </style> ``` 上面的示例中,我们通过配置 `el-upload` 组件的不同属性来实现以下功能: - `action`:上传图片的接口地址 - `on-preview`:点击预览图片时的回调函数 - `on-remove`:点击删除图片时的回调函数 - `on-success`:图片上传成功时的回调函数 - `before-upload`:上传图片之前的校验函数 - `file-list`:已上传的图片列表 - `auto-upload`:是否自动上传 - `list-type`:文件列表的类型,这里设置为 `picture`,表示只显示图片 - `headers`:请求头信息 - `data`:上传图片时需要携带的参数 - `multiple`:是否支持多选 - `limit`:限制上传图片的数量 - `on-exceed`:超过上传图片数量限制时的回调函数 - `show-file-list`:是否显示文件列表 以上是 ElementUI 实现上传图片的基本示例,你可以根据实际需求来配置组件的不同属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值