pull/9/head
tangjinzhou 2018-03-01 14:06:30 +08:00
parent 88a2ac7eb7
commit 96c523b40e
1 changed files with 29 additions and 13 deletions

View File

@ -19,11 +19,11 @@ export default {
mounted () { mounted () {
this.$nextTick(() => { this.$nextTick(() => {
this.updatedCal() this.updatedCal()
const debouncedResize = debounce(() => { this.debouncedResize = debounce(() => {
this.setNextPrev() this.setNextPrev()
this.scrollToActiveTab() this.scrollToActiveTab()
}, 200) }, 200)
this.resizeEvent = addDOMEventListener(window, 'resize', debouncedResize) this.resizeEvent = addDOMEventListener(window, 'resize', this.debouncedResize)
}) })
}, },
@ -37,6 +37,9 @@ export default {
if (this.resizeEvent) { if (this.resizeEvent) {
this.resizeEvent.remove() this.resizeEvent.remove()
} }
if (this.debouncedResize && this.debouncedResize.cancel) {
this.debouncedResize.cancel()
}
}, },
watch: { watch: {
tabBarPosition (val) { tabBarPosition (val) {
@ -59,28 +62,32 @@ export default {
}, },
setNextPrev () { setNextPrev () {
const navNode = this.$refs.nav const navNode = this.$refs.nav
const navNodeWH = this.getOffsetWH(navNode) const navNodeWH = this.getScrollWH(navNode)
const navWrapNode = this.$refs.navWrap const containerWH = this.getOffsetWH(this.$refs.container)
const navWrapNodeWH = this.getOffsetWH(navWrapNode) const navWrapNodeWH = this.getOffsetWH(this.$refs.navWrap)
let { offset } = this let { offset } = this
const minOffset = navWrapNodeWH - navNodeWH const minOffset = containerWH - navNodeWH
let { next, prev } = this let { next, prev } = this
if (minOffset >= 0) { if (minOffset >= 0) {
next = false next = false
this.setOffset(0, false) this.setOffset(0, false)
offset = 0 offset = 0
} else if (minOffset < offset) { } else if (minOffset < offset) {
next = (true) next = true
} else { } else {
next = (false) next = false
this.setOffset(minOffset, false) // Fix https://github.com/ant-design/ant-design/issues/8861
offset = minOffset // Test with container offset which is stable
// and set the offset of the nav wrap node
const realOffset = navWrapNodeWH - navNodeWH
this.setOffset(realOffset, false)
offset = realOffset
} }
if (offset < 0) { if (offset < 0) {
prev = (true) prev = true
} else { } else {
prev = (false) prev = false
} }
this.setNext(next) this.setNext(next)
@ -100,6 +107,15 @@ export default {
return node[prop] return node[prop]
}, },
getScrollWH (node) {
const tabBarPosition = this.tabBarPosition
let prop = 'scrollWidth'
if (tabBarPosition === 'left' || tabBarPosition === 'right') {
prop = 'scrollHeight'
}
return node[prop]
},
getOffsetLT (node) { getOffsetLT (node) {
const tabBarPosition = this.$props.tabBarPosition const tabBarPosition = this.$props.tabBarPosition
let prop = 'left' let prop = 'left'
@ -195,7 +211,7 @@ export default {
return return
} }
const activeTabWH = this.getOffsetWH(activeTab) const activeTabWH = this.getScrollWH(activeTab)
const navWrapNodeWH = this.getOffsetWH(navWrap) const navWrapNodeWH = this.getOffsetWH(navWrap)
let { offset } = this let { offset } = this
const wrapOffset = this.getOffsetLT(navWrap) const wrapOffset = this.getOffsetLT(navWrap)