Skip to content

Commit 7aad8bd

Browse files
authored
fix(tabs): 修复滚动计算问题,优化定位逻辑 (#3183)
1 parent 55a5bb4 commit 7aad8bd

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

src/packages/tabs/demos/h5/demo16.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const Demo16 = () => {
1616
<Tabs.TabPane title="百亿补贴">百亿补贴</Tabs.TabPane>
1717
<Tabs.TabPane title="今日聚超值">今日聚超值</Tabs.TabPane>
1818
<Tabs.TabPane title="真好真便宜">真好真便宜</Tabs.TabPane>
19+
<Tabs.TabPane title="限时秒杀">限时秒杀</Tabs.TabPane>
20+
<Tabs.TabPane title="品牌特惠">品牌特惠</Tabs.TabPane>
21+
<Tabs.TabPane title="折扣专区">折扣专区</Tabs.TabPane>
22+
<Tabs.TabPane title="新品首发">新品首发</Tabs.TabPane>
23+
<Tabs.TabPane title="热销榜单">热销榜单</Tabs.TabPane>
1924
</Tabs>
2025
</>
2126
)

src/packages/tabs/demos/h5/demo17.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Tabs } from '@nutui/nutui-react'
33

44
const Demo17 = () => {
55
const [tabvalue, setTabvalue] = useState<number | string>('0')
6-
const list = Array.from(new Array(10).keys())
6+
const list = Array.from(new Array(20).keys())
77
return (
88
<>
99
<Tabs

src/packages/tabs/demos/taro/demo16.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const Demo16 = () => {
1717
<Tabs.TabPane title="百亿补贴">百亿补贴</Tabs.TabPane>
1818
<Tabs.TabPane title="今日聚超值">今日聚超值</Tabs.TabPane>
1919
<Tabs.TabPane title="真好真便宜">真好真便宜</Tabs.TabPane>
20+
<Tabs.TabPane title="限时秒杀">限时秒杀</Tabs.TabPane>
21+
<Tabs.TabPane title="品牌特惠">品牌特惠</Tabs.TabPane>
22+
<Tabs.TabPane title="折扣专区">折扣专区</Tabs.TabPane>
23+
<Tabs.TabPane title="新品首发">新品首发</Tabs.TabPane>
24+
<Tabs.TabPane title="热销榜单">热销榜单</Tabs.TabPane>
2025
</Tabs>
2126
</>
2227
)

src/packages/tabs/demos/taro/demo17.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Text } from '@tarojs/components'
44

55
const Demo17 = () => {
66
const [tabvalue, setTabvalue] = useState<string | number>('0')
7-
const list = Array.from(new Array(10).keys())
7+
const list = Array.from(new Array(20).keys())
88
return (
99
<>
1010
<Tabs

src/packages/tabs/tabs.taro.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,40 @@ export const Tabs: FunctionComponent<Partial<TaroTabsProps>> & {
144144
const scrollIntoView = (index: number) => {
145145
raf(() => {
146146
Promise.all([
147-
getRect(`#nut-tabs-titles-${name || uuid} .nut-tabs-list`),
147+
getRect(`#nut-tabs-titles-${name || uuid}`),
148148
getAllRect(`#nut-tabs-titles-${name || uuid} .nut-tabs-titles-item`),
149149
]).then(([navRect, titleRects]: any) => {
150150
const titleRect = titleRects[index]
151151
if (!titleRect) return
152152

153153
let to = 0
154154
if (direction === 'vertical') {
155+
const totalHeight = titleRects.reduce(
156+
(sum: number, curr: RectItem) => sum + curr.height,
157+
0
158+
)
155159
const top = titleRects
156160
.slice(0, index)
157161
.reduce((prev: number, curr: RectItem) => prev + curr.height, 0)
158-
to = top - (navRect.height - titleRect.height) / 2
162+
163+
to = Math.min(
164+
Math.max(0, top - (navRect.height - titleRect.height) / 2),
165+
totalHeight - navRect.height
166+
)
159167
} else {
168+
const totalWidth = titleRects.reduce(
169+
(sum: number, curr: RectItem) => sum + curr.width,
170+
0
171+
)
160172
const left = titleRects
161173
.slice(0, index)
162174
.reduce((prev: number, curr: RectItem) => prev + curr.width, 0)
163-
to = left - (navRect.width - titleRect.width) / 2
175+
to = Math.min(
176+
Math.max(0, left - (navRect.width - titleRect.width) / 2),
177+
totalWidth - navRect.width
178+
)
164179
}
180+
165181
scrollDirection(to, direction)
166182

167183
nextTick(() => {

0 commit comments

Comments
 (0)