Skip to content

Commit

Permalink
fix: fixed by codecr
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyatong committed Oct 10, 2024
1 parent 98fa2c7 commit e995cb6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/packages/backtop/backtop.taro.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent, useState } from 'react'
import React, { FunctionComponent, useState, useCallback } from 'react'
import type { MouseEvent } from 'react'
import { usePageScroll, pageScrollTo } from '@tarojs/taro'
import { Top } from '@nutui/icons-react-taro'
Expand Down Expand Up @@ -29,14 +29,17 @@ export const BackTop: FunctionComponent<
...props,
}
const classPrefix = 'nut-backtop'
const [backTop, SetBackTop] = useState(false)
const [backTop, setBackTop] = useState(false)
const cls = classNames(classPrefix, { show: backTop }, className)
// 监听用户滑动页面事件
usePageScroll((res) => {
const { scrollTop } = res
scrollTop >= threshold ? SetBackTop(true) : SetBackTop(false)
})
// 返回顶部点击事件
const handleScroll = useCallback(
(res: { scrollTop: number }) => {
const { scrollTop } = res
setBackTop(scrollTop >= threshold)
},
[threshold]
)
usePageScroll(handleScroll)
const goTop = (e: MouseEvent<HTMLDivElement>) => {
onClick && onClick(e)
pageScrollTo({
Expand Down
5 changes: 4 additions & 1 deletion src/packages/menu/menu.taro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export const Menu: FunctionComponent<Partial<MenuProps>> & {
})

const getScrollTop = (el: Element | Window) => {
return Math.max(0, 'scrollTop' in el ? el.scrollTop : el.scrollY)
return Math.max(
0,
el === window ? window.scrollY : (el as Element).scrollTop
)
}
const onScroll = useCallback(() => {
const scrollTop = getScrollTop(window)
Expand Down

0 comments on commit e995cb6

Please sign in to comment.