This commit is contained in:
Pan 2018-10-16 10:53:51 +08:00
parent d732862f67
commit 8c543dfd7a
1 changed files with 9 additions and 9 deletions

View File

@ -24,24 +24,26 @@ export default {
const $container = this.$refs.scrollContainer.$el const $container = this.$refs.scrollContainer.$el
const $containerWidth = $container.offsetWidth const $containerWidth = $container.offsetWidth
const $scrollWrapper = this.$refs.scrollContainer.$refs.wrap const $scrollWrapper = this.$refs.scrollContainer.$refs.wrap
const tagList = this.$parent.$refs.tag const tagList = this.$parent.$refs.tag
let firstTag = null let firstTag = null
let lastTag = null let lastTag = null
let prevTag = null let prevTag = null
let nextTag = null let nextTag = null
// find first tag and last tag // find first tag and last tag
if (tagList.length > 0) { if (tagList.length > 0) {
firstTag = tagList[0] firstTag = tagList[0]
lastTag = tagList[tagList.length - 1] lastTag = tagList[tagList.length - 1]
} }
// find preTag and nextTag // find preTag and nextTag
for (let i = 0; i < tagList.length; i++) { for (let i = 0; i < tagList.length; i++) {
if (tagList[i] === currentTag) { if (tagList[i] === currentTag) {
if (i === 0) { if (i === 0) {
nextTag = tagList[i].length > 1 ? tagList[i + 1] : null nextTag = tagList[i].length > 1 && tagList[i + 1]
} else if (i === tagList.length - 1) { } else if (i === tagList.length - 1) {
prevTag = tagList[i].length > 1 ? tagList[i - 1] : null prevTag = tagList[i].length > 1 && tagList[i - 1]
} else { } else {
prevTag = tagList[i - 1] prevTag = tagList[i - 1]
nextTag = tagList[i + 1] nextTag = tagList[i + 1]
@ -53,17 +55,15 @@ export default {
if (firstTag === currentTag) { if (firstTag === currentTag) {
$scrollWrapper.scrollLeft = 0 $scrollWrapper.scrollLeft = 0
} else if (lastTag === currentTag) { } else if (lastTag === currentTag) {
$scrollWrapper.scrollLeft = $scrollWrapper.scrollLeft = $scrollWrapper.scrollWidth - $containerWidth
$scrollWrapper.scrollWidth - $containerWidth
} else { } else {
// the tag's offsetLeft after of nextTag // the tag's offsetLeft after of nextTag
const afterNextTagOffsetLeft = nextTag.$el.offsetLeft + nextTag.$el.offsetWidth + tagAndTagSpacing const afterNextTagOffsetLeft = nextTag.$el.offsetLeft + nextTag.$el.offsetWidth + tagAndTagSpacing
// the tag's offsetLeft before of prevTag // the tag's offsetLeft before of prevTag
const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing
if (
afterNextTagOffsetLeft > if (afterNextTagOffsetLeft > $scrollWrapper.scrollLeft + $containerWidth) {
$scrollWrapper.scrollLeft + $containerWidth
) {
$scrollWrapper.scrollLeft = afterNextTagOffsetLeft - $containerWidth $scrollWrapper.scrollLeft = afterNextTagOffsetLeft - $containerWidth
} else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) { } else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) {
$scrollWrapper.scrollLeft = beforePrevTagOffsetLeft $scrollWrapper.scrollLeft = beforePrevTagOffsetLeft