Skip to content

Commit

Permalink
feat: 窗口大小变化时,重新计算弹出框位置
Browse files Browse the repository at this point in the history
  • Loading branch information
sunbinbin committed Aug 11, 2018
1 parent 1e12b43 commit d06c679
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/popup/Popup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
</template>

<script>
import { throttle } from './helper'
export default {
name: 'Popup',
Expand Down Expand Up @@ -102,11 +104,15 @@ export default {
// 鼠标离开弹出框时,弹出框消失
this.unTriggerEl.addEventListener(this.unTriggerEvent, this.handlePopupInvisible)
this.handleResizeThrottle = throttle(this.handleResize, 16)
window.addEventListener('resize', this.handleResizeThrottle)
},
beforeDestroy () {
this.$el.removeEventListener(this.triggerEvent, this.handlePopupVisible)
this.unTriggerEl.removeEventListener(this.unTriggerEvent, this.handlePopupInvisible)
window.removeEventListener('resize', this.handleResizeThrottle)
},
destroyed () {
Expand Down Expand Up @@ -286,25 +292,25 @@ export default {
},
// 处理弹出框可见时
handleVisible (value, el, e) {
handleVisible (value, el, forceShow) {
// 第二次点击引用元素会隐藏弹出框
if (this.trigger === 'click' && this.display && forceShow !== true) {
return this.handleInvisible(value)
}
this.willHide = false
this.currentElement = el
if (this.trigger === 'click') {
this.$emit('update:display', !this.display)
this.$emit(this.display ? 'hide' : 'show', value)
} else {
this.$emit('update:display', true)
this.$emit('show', value)
}
this.currentValue = value
this.$emit('update:display', true)
this.$emit('show', value)
this.$nextTick(() => {
this.computePosition(this.$el, this.currentElement)
})
},
// 处理弹出框不可见时
handleInvisible (value, e) {
handleInvisible (value) {
if (this.trigger === 'click') {
this.$emit('update:display', false)
this.$emit('hide', value)
Expand Down Expand Up @@ -360,6 +366,12 @@ export default {
}
},
handleResize () {
if (this.display) {
this.handleVisible(this.value, this.currentElement, true)
}
},
// 展示弹出框
show (el, value) {
this.handleVisible(value, el)
Expand Down
10 changes: 10 additions & 0 deletions src/popup/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function throttle (fn, delay) {
let last = 0
return (...args) => {
const curr = +new Date()
if (curr - last > delay) {
fn.apply(this, args)
last = curr
}
}
}

0 comments on commit d06c679

Please sign in to comment.