element + vue 实现背景图片设置
<template> <div> <el-row> <el-col :span="3"> <el-upload action="#" list-type="picture-card" :file-list="fileList" :on-preview="onPreview" :limit="limit" :class="{ disabled: fileList.length === limit }" :on-remove="onRemove" :on-change="onChange" :before-upload="beforeUpload" :http-request="httpRequest" > <i class="el-icon-plus" /> </el-upload> </el-col> <el-col :span="10"> <el-button size="medium" type="primary" plain style="width: 168px" >提交</el-button ><br /><br /> <el-button size="medium" type="primary" style="width: 168px" plain >取消</el-button ><br /><br /> <el-button size="medium" type="primary" plain >恢复当前的皮肤背景</el-button > </el-col> </el-row> <el-row style="font-size: 12px"> 只能上传2张jpg/png文件,每张且不超过500Kb </el-row> <br /> 预设背景图: <br /> <br /> <div class="demo-image__preview"> <div class="previewImage"> <el-image style="width: 148px; height: 148px; margin-right: 2px" v-for="(item, index) in imageUrls" :key="index" :src="item.url" @click.native="selectFn(item.url)" > </el-image> </div> </div> <!-- 图片预览 --> <el-dialog title="预览" :visible.sync="showImgDialog"> <img :src="showImgUrl" style="width: 100%" alt="" /> </el-dialog> </div> </template> <script> export default { name: "ImageUpload", props: { // 限定只能上传几个 limit: { type: Number, default: 1, }, }, data() { return { showImgUrl: "", showImgDialog: false, fileList: [], currrentUid: null, // 当前正在上传的uid percentageNum: 0, percentageShow: false, url: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg", // srcList: [ // "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg", // "https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg", // ], // 预览图片的地址 url imageUrls: [ { url: "https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg", }, { url: "https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg", }, ], }; }, methods: { selectFn(url) { console.log(url); console.log(this.fileList); this.fileList = [ { status: "ready", name: "gdnMq.jpg", size: 15097, percentage: 0, uid: 1676478291934, raw: "[object File]", url: url, }, ]; console.log(this.fileList); }, // handleAvatarSuccess(res, file) { // console.log(111); // this.imageUrl = URL.createObjectURL(file.raw); // }, // 图片预览的时候 onPreview(file) { // console.log(file); blob文件 图片暂时的url this.showImgUrl = file.url; this.showImgDialog = true; }, // 图片删除的钩子 onRemove(file, fileList) { // console.log(fileList); // file是删除的那个图片信息,fileList是剩余的最新的fileList this.fileList = fileList; }, // 文件改变的时候====> 1. 添加文件 2. 上传成功或失败都调用 onChange(file, fileList) { // ready 的时候,fileList其实多了一个元素【本地预览的地址】 // fail 的时候,fileList会把刚才没成功的移除了 // success 的时候, fileList 多了个元素 【真实的线上地址,临时预览图片】 // console.log("文件改变的时候", file, file.status); this.fileList = fileList; /// 有没有这句代码的区别在于【是否有上传的方块】 }, // 上传之前的检查钩子 用于校验 beforeUpload({ size, type, uid }) { // 检测大小 const maxSize = 10; // 10 M if (size > 1024 * 1024 * maxSize) { this.$message.warning(`体积不允许超过${maxSize}M`); return false; } // 检测类型 const types = ["image/jpeg", "image/gif", "image/bmp", "image/png"]; if (!types.includes(type)) { this.$message.warning(`格式不正确`); return Promise.reject(); } this.currrentUid = uid; this.percentageShow = true; // 此时进度条结束 // 通过就是true return true; }, // beforeAvatarUpload(file) { // const isJPG = file.type === "image/jpeg"; // const isLt2M = file.size / 1024 / 1024 < 2; // if (!isJPG) { // this.$message.error("上传头像图片只能是 JPG 格式!"); // } // if (!isLt2M) { // this.$message.error("上传头像图片大小不能超过 2MB!"); // } // return isJPG && isLt2M; // }, // 覆盖默认的上传行为 httpRequest(data) { console.log("要上传的文件信息", data.file); // 发起上传文件的请求操作 【自己服务器的还是用第三方的COS,根据项目情况】 // let fd = new FormData(); // fd.append(xx, xxx); }, }, }; </script> <style lang="scss" > .disabled { .el-upload { display: none; } } .previewImage { width: 750px; height: 298px; background-color: pink; } </style>