diff --git a/vue/src/components/PuzzleVerify/index.js b/vue/src/components/PuzzleVerify/index.js index a643332..04d6be1 100644 --- a/vue/src/components/PuzzleVerify/index.js +++ b/vue/src/components/PuzzleVerify/index.js @@ -1,12 +1,15 @@ import Vue from 'vue' import Main from './main.vue' import {PopupManager} from 'element-ui/lib/utils/popup' +import {isEmpty} from "@/utils" let Constructor = Vue.extend(Main) -const PuzzleVerify = function ({left = 0, top = 0} = {}) { - const instance = new Constructor({data: {positionLeft: left}}) - instance.positionTop = top - Math.round((instance.height + 130) / 2) +const PuzzleVerify = function ({left, top} = {}) { + const instance = new Constructor() + const position = getPosition({height: instance.height + 130, width: instance.width + 20, left, top}) + instance.positionTop = position.top + instance.positionLeft = position.left instance.$mount() document.body.appendChild(instance.$el) instance.$el.style.zIndex = PopupManager.nextZIndex() @@ -17,4 +20,27 @@ const PuzzleVerify = function ({left = 0, top = 0} = {}) { }) } +function getPosition({height, width, left, top} = {}) { + const windowHeight = window.innerHeight + const windowWidth = window.innerWidth + + //若任一位置信息为空,直接返回居中位置 + if (isEmpty(left, top)) { + return { + top: center(windowHeight, height), + left: center(windowWidth, width) + } + } + + //判断位置是否超出屏幕外 + if (windowWidth < left + width) left = center(windowWidth, width) + if (windowHeight < top + height) top = center(windowHeight, height) + + return {left, top} +} + +function center(full, part) { + return Math.round((full - part) / 2) +} + export default PuzzleVerify diff --git a/vue/src/components/PuzzleVerify/main.vue b/vue/src/components/PuzzleVerify/main.vue index cc95f42..b6082f3 100644 --- a/vue/src/components/PuzzleVerify/main.vue +++ b/vue/src/components/PuzzleVerify/main.vue @@ -30,7 +30,7 @@
- +