|
|
@@ -8,8 +8,8 @@
|
|
|
>
|
|
|
<div class="select-background">
|
|
|
<div class="select-background-top">
|
|
|
- <span class="tab">背景图</span>
|
|
|
- <SelectUpload type="image" @uploadSuccess="uploadSuccess" />
|
|
|
+ <span class="tab">{{ title }}</span>
|
|
|
+ <SelectUpload :is-show-resource="isResource" type="image" @uploadSuccess="uploadSuccess" />
|
|
|
</div>
|
|
|
<div class="background-img">
|
|
|
<div v-if="file_url" class="img-set" :style="{ top: `${imgData.top - 9}px`, left: `${imgData.left}px` }">
|
|
|
@@ -20,7 +20,7 @@
|
|
|
<img
|
|
|
:src="file_url"
|
|
|
draggable="false"
|
|
|
- alt="背景图"
|
|
|
+ :alt="title"
|
|
|
:style="{ width: `${imgData.width}px`, height: `${imgData.height}px` }"
|
|
|
@mousedown="dragStart($event, 'move', 'move')"
|
|
|
/>
|
|
|
@@ -52,32 +52,75 @@ export default {
|
|
|
type: Boolean,
|
|
|
required: true,
|
|
|
},
|
|
|
+ title: {
|
|
|
+ type: String,
|
|
|
+ default: '背景图',
|
|
|
+ },
|
|
|
+ url: {
|
|
|
+ type: String,
|
|
|
+ default: '',
|
|
|
+ },
|
|
|
+ position: {
|
|
|
+ type: Object,
|
|
|
+ default: () => ({ width: 0, height: 0, top: 0, left: 0 }),
|
|
|
+ },
|
|
|
+ // 是否使用资源
|
|
|
+ isResource: {
|
|
|
+ type: Boolean,
|
|
|
+ default: true,
|
|
|
+ },
|
|
|
+ // 图片填充模式
|
|
|
+ imageFillMode: {
|
|
|
+ type: String,
|
|
|
+ default: 'normal', // normal:正常,cover:覆盖
|
|
|
+ },
|
|
|
},
|
|
|
data() {
|
|
|
return {
|
|
|
maxWidth: 576,
|
|
|
maxHeight: 310,
|
|
|
- file_url: '',
|
|
|
+ file_url: this.url,
|
|
|
drag: {
|
|
|
dragging: false,
|
|
|
startX: 0,
|
|
|
startY: 0,
|
|
|
type: '',
|
|
|
},
|
|
|
- imgData: {
|
|
|
- width: 0,
|
|
|
- height: 0,
|
|
|
- top: 0,
|
|
|
- left: 0,
|
|
|
- },
|
|
|
+ imgData: this.position,
|
|
|
};
|
|
|
},
|
|
|
+ watch: {
|
|
|
+ visible(val) {
|
|
|
+ if (val) {
|
|
|
+ this.$nextTick(() => {
|
|
|
+ document.querySelector('.background-img').addEventListener('mousemove', this.mouseMove);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ document.querySelector('.background-img')?.removeEventListener('mousemove', this.mouseMove);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ url(val) {
|
|
|
+ this.file_url = val;
|
|
|
+ },
|
|
|
+ position: {
|
|
|
+ handler(val) {
|
|
|
+ if (val) {
|
|
|
+ this.imgData = {
|
|
|
+ width: (val.width * this.maxWidth) / 100,
|
|
|
+ height: (val.height * this.maxHeight) / 100,
|
|
|
+ top: (val.top * this.maxHeight) / 100,
|
|
|
+ left: (val.left * this.maxWidth) / 100,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ },
|
|
|
+ deep: true,
|
|
|
+ },
|
|
|
+ },
|
|
|
mounted() {
|
|
|
- document.querySelector('.el-dialog__wrapper').addEventListener('mousemove', this.mouseMove);
|
|
|
document.body.addEventListener('mouseup', this.mouseUp);
|
|
|
},
|
|
|
beforeDestroy() {
|
|
|
- document.querySelector('.el-dialog__wrapper')?.removeEventListener('mousemove', this.mouseMove);
|
|
|
+ document.querySelector('.background-img')?.removeEventListener('mousemove', this.mouseMove);
|
|
|
document.body.removeEventListener('mouseup', this.mouseUp);
|
|
|
},
|
|
|
methods: {
|
|
|
@@ -185,29 +228,55 @@ export default {
|
|
|
img.onload = () => {
|
|
|
const { width, height } = img;
|
|
|
|
|
|
- if (width > this.maxWidth || height > this.maxHeight) {
|
|
|
- const wScale = width / this.maxWidth;
|
|
|
- const hScale = height / this.maxHeight;
|
|
|
- const scale = wScale > hScale ? this.maxWidth / 2 / width : this.maxHeight / 2 / height;
|
|
|
-
|
|
|
- this.imgData = {
|
|
|
- width: width * scale,
|
|
|
- height: height * scale,
|
|
|
- top: 0,
|
|
|
- left: 0,
|
|
|
- };
|
|
|
- } else {
|
|
|
- this.imgData = {
|
|
|
- width,
|
|
|
- height,
|
|
|
- top: 0,
|
|
|
- left: 0,
|
|
|
- };
|
|
|
+ if (this.imageFillMode === 'cover') {
|
|
|
+ this.coverComputed(width, height);
|
|
|
+ } else if (this.imageFillMode === 'normal') {
|
|
|
+ this.normalComputed(width, height);
|
|
|
}
|
|
|
+
|
|
|
this.file_url = fileList[0].file_url_open;
|
|
|
};
|
|
|
}
|
|
|
},
|
|
|
+ /**
|
|
|
+ * 正常填充
|
|
|
+ * @param {number} width 图片宽度
|
|
|
+ * @param {number} height 图片高度
|
|
|
+ */
|
|
|
+ normalComputed(width, height) {
|
|
|
+ if (width > this.maxWidth || height > this.maxHeight) {
|
|
|
+ const wScale = width / this.maxWidth;
|
|
|
+ const hScale = height / this.maxHeight;
|
|
|
+ const scale = wScale > hScale ? this.maxWidth / 2 / width : this.maxHeight / 2 / height;
|
|
|
+
|
|
|
+ this.imgData = {
|
|
|
+ width: width * scale,
|
|
|
+ height: height * scale,
|
|
|
+ top: 0,
|
|
|
+ left: 0,
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ this.imgData = {
|
|
|
+ width,
|
|
|
+ height,
|
|
|
+ top: 0,
|
|
|
+ left: 0,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 覆盖填充
|
|
|
+ * @param {number} width 图片宽度
|
|
|
+ * @param {number} height 图片高度
|
|
|
+ */
|
|
|
+ coverComputed(width, height) {
|
|
|
+ this.imgData = {
|
|
|
+ width: width * (this.maxWidth / width),
|
|
|
+ height: height * (this.maxHeight / height),
|
|
|
+ top: 0,
|
|
|
+ left: 0,
|
|
|
+ };
|
|
|
+ },
|
|
|
},
|
|
|
};
|
|
|
</script>
|