Skip to content

Commit 07c3211

Browse files
rainydayDYPanJiaChen
authored andcommitted
perf[ScrollPane]: refine moveToTarget code (PanJiaChen#1460)
1 parent afdb893 commit 07c3211

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

src/components/ScrollPane/index.vue

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,22 @@ export default {
2828
2929
let firstTag = null
3030
let lastTag = null
31-
let prevTag = null
32-
let nextTag = null
3331
3432
// find first tag and last tag
3533
if (tagList.length > 0) {
3634
firstTag = tagList[0]
3735
lastTag = tagList[tagList.length - 1]
3836
}
3937
40-
// find preTag and nextTag
41-
for (let i = 0; i < tagList.length; i++) {
42-
if (tagList[i] === currentTag) {
43-
if (i === 0) {
44-
nextTag = tagList[i].length > 1 && tagList[i + 1]
45-
} else if (i === tagList.length - 1) {
46-
prevTag = tagList[i].length > 1 && tagList[i - 1]
47-
} else {
48-
prevTag = tagList[i - 1]
49-
nextTag = tagList[i + 1]
50-
}
51-
break
52-
}
53-
}
54-
5538
if (firstTag === currentTag) {
5639
$scrollWrapper.scrollLeft = 0
5740
} else if (lastTag === currentTag) {
5841
$scrollWrapper.scrollLeft = $scrollWrapper.scrollWidth - $containerWidth
5942
} else {
43+
// find preTag and nextTag
44+
const currentIndex = tagList.findIndex(item => item === currentTag)
45+
const prevTag = tagList[currentIndex - 1]
46+
const nextTag = tagList[currentIndex + 1]
6047
// the tag's offsetLeft after of nextTag
6148
const afterNextTagOffsetLeft = nextTag.$el.offsetLeft + nextTag.$el.offsetWidth + tagAndTagSpacing
6249

0 commit comments

Comments
 (0)