Skip to content

Commit 705b9cc

Browse files
committed
add(tags-view): moveToCurrentTag
1 parent c84964d commit 705b9cc

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/components/ScrollPane/index.vue

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</template>
88

99
<script>
10+
const padding = 15 // tag's padding
11+
1012
export default {
1113
name: 'scrollPane',
1214
data() {
@@ -21,19 +23,37 @@ export default {
2123
const $containerWidth = $container.offsetWidth
2224
const $wrapper = this.$refs.scrollWrapper
2325
const $wrapperWidth = $wrapper.offsetWidth
26+
2427
if (e.wheelDelta > 0) {
2528
this.left = Math.min(0, this.left + e.wheelDelta)
2629
} else {
27-
if ($containerWidth - 100 < $wrapperWidth) {
28-
if (this.left < -($wrapperWidth - $containerWidth + 100)) {
30+
if ($containerWidth - padding < $wrapperWidth) {
31+
if (this.left < -($wrapperWidth - $containerWidth + padding)) {
2932
this.left = this.left
3033
} else {
31-
this.left = Math.max(this.left + e.wheelDelta, $containerWidth - $wrapperWidth - 100)
34+
this.left = Math.max(this.left + e.wheelDelta, $containerWidth - $wrapperWidth - padding)
3235
}
3336
} else {
3437
this.left = 0
3538
}
3639
}
40+
},
41+
moveToTarget($target) {
42+
const $container = this.$refs.scrollContainer
43+
const $containerWidth = $container.offsetWidth
44+
const $targetLeft = $target.offsetLeft
45+
const $targetWidth = $target.offsetWidth
46+
47+
if ($targetLeft < -this.left) {
48+
// tag in the left
49+
this.left = -$targetLeft + padding
50+
} else if ($targetLeft + padding > -this.left && $targetLeft + $targetWidth < -this.left + $containerWidth - padding) {
51+
// tag in the current view
52+
// eslint-disable-line
53+
} else {
54+
// tag in the right
55+
this.left = -($targetLeft - ($containerWidth - $targetWidth) + padding)
56+
}
3757
}
3858
}
3959
}

src/views/layout/components/TagsView.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<scroll-pane class='tags-view-container'>
3-
<router-link class="tags-view-item" :class="isActive(tag)?'active':''" v-for="tag in Array.from(visitedViews)" :to="tag.path":key="tag.path">
2+
<scroll-pane class='tags-view-container' ref='scrollPane'>
3+
<router-link ref='tag' class="tags-view-item" :class="isActive(tag)?'active':''" v-for="tag in Array.from(visitedViews)" :to="tag.path":key="tag.path">
44
{{$t('route.'+tag.title)}}
55
<span class='el-icon-close' @click='closeViewTags(tag,$event)'></span>
66
</router-link>
@@ -49,12 +49,24 @@ export default {
4949
},
5050
isActive(route) {
5151
return route.path === this.$route.path || route.name === this.$route.name
52+
},
53+
moveToCurrentTag() {
54+
const tags = this.$refs.tag
55+
this.$nextTick(() => {
56+
for (const tag of tags) {
57+
if (tag.to === this.$route.path) {
58+
this.$refs.scrollPane.moveToTarget(tag.$el)
59+
break
60+
}
61+
}
62+
})
5263
}
5364
5465
},
5566
watch: {
5667
$route() {
5768
this.addViewTags()
69+
this.moveToCurrentTag()
5870
}
5971
}
6072
}

0 commit comments

Comments
 (0)