-
-
-
-
-
+
@@ -17,10 +14,11 @@
import { addEvent, removeEvent } from '../../../utils/domEvent.js';
import ToolBar from './toolBar';
export default {
- name: 'image-preview',
+ name: 'preview',
components: {
ToolBar
},
+ inject: ['childProp'],
props: {
url: {
type: String,
@@ -33,11 +31,14 @@ export default {
isShowToolBar: {
type: Boolean,
default: true
+ },
+ isMouseWheel: {
+ type: Boolean,
+ default: false
}
},
data() {
return {
- isShowImg: false,
bgSize: 50, // 背景图片默认大小
degree: 0
};
@@ -45,22 +46,39 @@ export default {
mounted() {
// 挂载后,绑定
addEvent(window, 'keyup', this.handleEscape);
+ // 鼠标滚动:120向前滚动,-120向后滚动
+ addEvent(this.$refs.image, 'mousewheel', this.handleMousewheel);
},
beforeDestroy() {
removeEvent(window, 'keyup', this.handleEscape);
+ removeEvent(this.$refs.image, 'mousewheel', this.handleMousewheel);
},
methods: {
+ // 鼠标滚轮缩放
+ handleMousewheel(e) {
+ if (!this.isMouseWheel) return;
+ const delta = e.wheelDelta;
+ const data = {
+ '120': 15,
+ '-120': -15
+ };
+ this.handleZoom(data[delta]);
+ },
+ // esc键退出图片预览
handleEscape(e) {
if (!this.closeOnPressEscape) return;
const key = e.which || e.keyCode;
if (key === 27) {
- this.isShowImg = false;
+ this.$emit('close');
}
},
+ // 点击缩放
handleZoom(num) {
- if (this.bgSize <= 15 && num < 0) return;
+ num = window.parseFloat(num);
+ if ((this.bgSize <= 15 && num < 0) || (this.bgSize >= 230 && num > 0)) return;
this.bgSize += num;
},
+ // 图片旋转
handleSpin(num) {
this.degree += num;
if (this.degree >= 360) this.degree = 0;
diff --git a/src/components/imagePreview/src/toolBar.vue b/src/components/imagePreview/src/toolBar.vue
index 82275dae1ba1e97ff95a8fabc078e0cc0199cd1a..74a51e43c85b1607b0ee82f08b6854d9aedb1778 100644
--- a/src/components/imagePreview/src/toolBar.vue
+++ b/src/components/imagePreview/src/toolBar.vue
@@ -19,6 +19,10 @@ export default {
isDownload: {
type: Boolean,
default: true
+ },
+ downloadName: {
+ type: String,
+ default: '下载图片'
}
},
data() {
@@ -26,8 +30,9 @@ export default {
},
computed: {
getImgName() {
- let imgName = strSplit(strSplit(this.imgUrl, '/')[2], '.')[0];
- return imgName;
+ const arr = strSplit(this.imgUrl, '/');
+ let imgName = strSplit(arr[arr.length - 1], '.')[0];
+ return (imgName || this.downloadName);
}
},
components: {},