Skip to content

Commit 2c421d0

Browse files
authored
fix(sidebar): scroll animation not working with sidebarDuration prop (#3169)
1 parent 16ee064 commit 2c421d0

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/packages/sidebar/sidebar.taro.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,26 @@ export const SideBar: FC<Partial<TaroSideBarProps>> & {
111111
const titleRectRef = useRef<RectItem[]>([])
112112
const [scrollTop, setScrollTop] = useState(0)
113113
const scrollDirection = (to: number) => {
114+
if (sidebarDuration === 0) {
115+
setScrollTop(to)
116+
return
117+
}
118+
119+
const from = scrollTop
120+
const frames = Math.round(sidebarDuration / 16)
114121
let count = 0
115-
const frames = sidebarDuration === 0 ? 1 : Math.round(sidebarDuration / 16)
122+
116123
function animate() {
117-
setScrollTop(to)
118-
if (++count < frames) {
124+
const progress = count / frames
125+
const current = from + (to - from) * progress
126+
setScrollTop(current)
127+
128+
if (count < frames) {
129+
count++
119130
raf(animate)
120131
}
121132
}
133+
122134
animate()
123135
}
124136
const scrollIntoView = (index: number) => {

0 commit comments

Comments
 (0)