Skip to content

Commit

Permalink
feat: tag滚动会自动多展示前一个/后一个tag
Browse files Browse the repository at this point in the history
  • Loading branch information
yuntian001 committed Aug 18, 2023
1 parent 64393f1 commit 4d40627
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/layout/components/header/components/tagBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,30 @@ const go = () => {
const jump = (index: number) => {
nextTick(() => {
if (tagsRef.value[index]) {
const offsetLeft = tagsRef.value[index].offsetLeft;
const offsetWidth = tagsRef.value[index].offsetWidth;
const offsetRight = offsetWidth + offsetLeft;
currentTag.value = tags[index];
if (index === 0) {
setScrollLeft(0);
return;
}
if (index === tagsRef.value.length - 1) {
setScrollLeft(tagsRef.value[tagsRef.value.length - 1].offsetLeft);
return;
}
const parentWidth = scrollbarRef.value!.$el.clientWidth;
const parentLeft = scrollLeft.value;
const parentRight = parentWidth + scrollLeft.value;
if (offsetLeft < parentLeft) {
const lastLeft = tagsRef.value[index - 1].offsetLeft;
const offsetLeft = tagsRef.value[index].offsetLeft;
const offsetRight = offsetLeft + tagsRef.value[index].offsetWidth;
const nextLeft = tagsRef.value[index + 1].offsetLeft;
const nextRight = nextLeft + tagsRef.value[index + 1].offsetWidth;
if (parentWidth <= offsetRight - lastLeft || parentWidth <= nextRight - offsetLeft) {
setScrollLeft(offsetLeft);
} else if (lastLeft < parentLeft) {
setScrollLeft(lastLeft);
} else if (nextRight > parentRight) {
setScrollLeft(nextRight - parentWidth);
}
if (offsetRight > parentRight) {
setScrollLeft(offsetLeft + offsetWidth - parentWidth);
}
currentTag.value = tags[index];
}
});
};
Expand Down

0 comments on commit 4d40627

Please sign in to comment.