|
|
@@ -68,51 +68,53 @@ const removeTab = (index: number) => {
|
|
|
// 滚动标签
|
|
|
const scrollTab = () => {
|
|
|
setTimeout(() => {
|
|
|
- const el = {
|
|
|
- container: containerElement.value,
|
|
|
- tabbar: tabbarElement.value,
|
|
|
- activeTab: containerElement.value.querySelector('li.is-active'),
|
|
|
- lastTab: containerElement.value.querySelector('li:last-child')
|
|
|
- }
|
|
|
-
|
|
|
- if (el.tabbar.scrollWidth > el.container.clientWidth) {
|
|
|
- showArrow.value = true;
|
|
|
- // 等待箭头元素出现后再计算,不然可能出现计算误差
|
|
|
- setTimeout(() => {
|
|
|
- const rect = {
|
|
|
- container: el.container.getBoundingClientRect(), // 外层容器
|
|
|
- tabbar: el.tabbar.getBoundingClientRect(), // 标签栏
|
|
|
- activeTab: el.activeTab?.getBoundingClientRect(), // 标签栏中被选中的标签
|
|
|
- lastTab: el.lastTab?.getBoundingClientRect() // 标签栏中最后一个标签
|
|
|
- }
|
|
|
-
|
|
|
- if (rect.activeTab && rect.lastTab) {
|
|
|
- let tabbarOffset = rect.container.left - rect.tabbar.left, // 计算标签栏偏移容器距离
|
|
|
- activeTabOffsetLeft = rect.container.left - rect.activeTab.left, // 计算标签偏移容器左边的距离
|
|
|
- activeTabOffsetRight = rect.activeTab.right - rect.container.right; // 计算标签偏移容器右边的距离
|
|
|
-
|
|
|
- // 计算最后一个标签和容器最右边之间的距离
|
|
|
- const lastOffset = rect.container.right - rect.lastTab.right;
|
|
|
- if (activeTabOffsetLeft < lastOffset) {
|
|
|
- activeTabOffsetLeft += lastOffset - activeTabOffsetLeft;
|
|
|
- }
|
|
|
+ if (containerElement.value) {
|
|
|
+ const el = {
|
|
|
+ container: containerElement.value,
|
|
|
+ tabbar: tabbarElement.value,
|
|
|
+ activeTab: containerElement.value.querySelector('li.is-active'),
|
|
|
+ lastTab: containerElement.value.querySelector('li:last-child')
|
|
|
+ }
|
|
|
|
|
|
- // 判断标签是否超出父元素左边界
|
|
|
- if (activeTabOffsetLeft > 0) {
|
|
|
- const scrollX = tabbarOffset - activeTabOffsetLeft;
|
|
|
- el.tabbar.style.transform = 'translate3d(-' + scrollX + 'px,0,0)';
|
|
|
+ if (el.tabbar.scrollWidth > el.container.clientWidth) {
|
|
|
+ showArrow.value = true;
|
|
|
+ // 等待箭头元素出现后再计算,不然可能出现计算误差
|
|
|
+ setTimeout(() => {
|
|
|
+ const rect = {
|
|
|
+ container: el.container.getBoundingClientRect(), // 外层容器
|
|
|
+ tabbar: el.tabbar.getBoundingClientRect(), // 标签栏
|
|
|
+ activeTab: el.activeTab?.getBoundingClientRect(), // 标签栏中被选中的标签
|
|
|
+ lastTab: el.lastTab?.getBoundingClientRect() // 标签栏中最后一个标签
|
|
|
}
|
|
|
|
|
|
- // 判断标签是否超出父元素右边界
|
|
|
- if (activeTabOffsetRight > 0) {
|
|
|
- const scrollX = tabbarOffset + activeTabOffsetRight;
|
|
|
- el.tabbar.style.transform = 'translate3d(-' + scrollX + 'px,0,0)';
|
|
|
+ if (rect.activeTab && rect.lastTab) {
|
|
|
+ let tabbarOffset = rect.container.left - rect.tabbar.left, // 计算标签栏偏移容器距离
|
|
|
+ activeTabOffsetLeft = rect.container.left - rect.activeTab.left, // 计算标签偏移容器左边的距离
|
|
|
+ activeTabOffsetRight = rect.activeTab.right - rect.container.right; // 计算标签偏移容器右边的距离
|
|
|
+
|
|
|
+ // 计算最后一个标签和容器最右边之间的距离
|
|
|
+ const lastOffset = rect.container.right - rect.lastTab.right;
|
|
|
+ if (activeTabOffsetLeft < lastOffset) {
|
|
|
+ activeTabOffsetLeft += lastOffset - activeTabOffsetLeft;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断标签是否超出父元素左边界
|
|
|
+ if (activeTabOffsetLeft > 0) {
|
|
|
+ const scrollX = tabbarOffset - activeTabOffsetLeft;
|
|
|
+ el.tabbar.style.transform = 'translate3d(-' + scrollX + 'px,0,0)';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断标签是否超出父元素右边界
|
|
|
+ if (activeTabOffsetRight > 0) {
|
|
|
+ const scrollX = tabbarOffset + activeTabOffsetRight;
|
|
|
+ el.tabbar.style.transform = 'translate3d(-' + scrollX + 'px,0,0)';
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- }, 0)
|
|
|
- } else {
|
|
|
- showArrow.value = false;
|
|
|
- el.tabbar.style.transform = 'translate3d(0,0,0)';
|
|
|
+ }, 0)
|
|
|
+ } else {
|
|
|
+ showArrow.value = false;
|
|
|
+ el.tabbar.style.transform = 'translate3d(0,0,0)';
|
|
|
+ }
|
|
|
}
|
|
|
}, 0)
|
|
|
}
|