Repository navigation
fix(h5): 修复页面滚动监听器的容器归属 #19573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Single-Dancer
merged 1 commit into
NervJS:main
from
Jarvis636431:fix/h5-page-scroll-bindings
Oct 30, 2026
Merged
fix(h5): 修复页面滚动监听器的容器归属 #19573
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { createBrowserHistory } from 'history' | ||
|
|
||
| import MultiPageHandler from '../src/router/multi-page' | ||
| import PageHandler from '../src/router/page' | ||
|
|
||
| import type { PageInstance } from '@tarojs/runtime' | ||
| import type { SpaRouterConfig } from '../types/router' | ||
|
|
||
| function createPage (path: string) { | ||
| return { | ||
| path, | ||
| $taroParams: { stamp: 'scroll-test' }, | ||
| onShow: jest.fn(), | ||
| onHide: jest.fn(), | ||
| onLoad: jest.fn((_params, done) => done()), | ||
| onPageScroll: jest.fn(), | ||
| onReachBottom: jest.fn() | ||
| } | ||
| } | ||
|
|
||
| function createContainer (path: string, top = 0) { | ||
| const el = document.createElement('div') | ||
| el.id = path | ||
| el.className = 'taro_page' | ||
| el.scrollTop = top | ||
| Object.defineProperties(el, { | ||
| scrollHeight: { value: 1000 }, | ||
| clientHeight: { value: 200 } | ||
| }) | ||
| document.body.appendChild(el) | ||
| return el | ||
| } | ||
|
|
||
| const config = (): SpaRouterConfig => ({ | ||
| pages: ['a', 'b'], | ||
| routes: [{ path: '/a' }, { path: '/b' }], | ||
| router: { mode: 'browser', basename: '', pathname: '/a' }, | ||
| animation: false, | ||
| window: { navigationStyle: 'custom' } | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| jest.useFakeTimers() | ||
| jest.spyOn(window, 'addEventListener') | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| for (const [type, listener] of (window.addEventListener as jest.Mock).mock.calls) { | ||
| if (type === 'resize' || type === 'popstate') window.removeEventListener(type, listener) | ||
| } | ||
| jest.restoreAllMocks() | ||
| jest.clearAllTimers() | ||
| jest.useRealTimers() | ||
| document.body.innerHTML = '' | ||
| }) | ||
|
|
||
| test('SPA show/hide and return keep page containers isolated without duplicate callbacks', () => { | ||
| const a = createPage('/a') | ||
| const b = createPage('/b') | ||
| const aEl = createContainer('/a', 17) | ||
| const bEl = createContainer('/b', 99) | ||
| const handler = new PageHandler(config(), createBrowserHistory()) | ||
| handler.pathname = '/a' | ||
| handler.show(a as unknown as PageInstance) | ||
| handler.hide(a as unknown as PageInstance) | ||
| handler.pathname = '/b' | ||
| handler.show(b as unknown as PageInstance) | ||
|
|
||
| aEl.dispatchEvent(new Event('scroll')) | ||
| expect(a.onPageScroll).toHaveBeenLastCalledWith({ scrollTop: 17 }) | ||
| expect(b.onPageScroll).not.toHaveBeenCalled() | ||
| handler.hide(b as unknown as PageInstance) | ||
| handler.pathname = '/a' | ||
| handler.show(a as unknown as PageInstance) | ||
| a.onPageScroll.mockClear() | ||
| aEl.dispatchEvent(new Event('scroll')) | ||
| expect(a.onPageScroll).toHaveBeenCalledTimes(1) | ||
| expect(a.onPageScroll).toHaveBeenCalledWith({ scrollTop: 17 }) | ||
| expect(a.onShow).toHaveBeenCalledTimes(2) | ||
| expect(a.onHide).toHaveBeenCalledTimes(1) | ||
| expect(bEl.classList.contains('taro_page_shade')).toBe(true) | ||
| }) | ||
|
|
||
| test('SPA returning to a recreated container detaches the previous container', () => { | ||
| const current = createPage('/a') | ||
| const oldEl = createContainer('/a') | ||
| const handler = new PageHandler(config(), createBrowserHistory()) | ||
| handler.pathname = '/a' | ||
| handler.show(current as unknown as PageInstance) | ||
| handler.hide(current as unknown as PageInstance) | ||
| oldEl.remove() | ||
| const newEl = createContainer('/a', 780) | ||
| handler.show(current as unknown as PageInstance) | ||
|
|
||
| oldEl.dispatchEvent(new Event('scroll')) | ||
| expect(current.onPageScroll).not.toHaveBeenCalled() | ||
| newEl.dispatchEvent(new Event('scroll')) | ||
| expect(current.onPageScroll).toHaveBeenCalledTimes(1) | ||
| expect(current.onPageScroll).toHaveBeenCalledWith({ scrollTop: 780 }) | ||
| expect(current.onReachBottom).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('MPA loading with a replacement container removes the previous binding', () => { | ||
| const current = createPage('/a') | ||
| const oldEl = createContainer('/a') | ||
| const handler = new MultiPageHandler({ | ||
| ...config(), | ||
| pageName: 'a', | ||
| route: { path: '/a', usingWindowScroll: false } | ||
| }, createBrowserHistory()) | ||
| handler.load(current as unknown as PageInstance) | ||
| oldEl.remove() | ||
| const newEl = createContainer('/a', 780) | ||
| handler.load(current as unknown as PageInstance) | ||
|
|
||
| oldEl.dispatchEvent(new Event('scroll')) | ||
| expect(current.onPageScroll).not.toHaveBeenCalled() | ||
| newEl.dispatchEvent(new Event('scroll')) | ||
| expect(current.onPageScroll).toHaveBeenCalledTimes(1) | ||
| expect(current.onPageScroll).toHaveBeenCalledWith({ scrollTop: 780 }) | ||
| expect(current.onReachBottom).toHaveBeenCalledTimes(1) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| import { bindPageScroll } from '../src/events/scroll' | ||
|
|
||
| import type { PageInstance } from '@tarojs/runtime' | ||
|
|
||
| let nextPath = 0 | ||
| const windowScrollY = Object.getOwnPropertyDescriptor(window, 'scrollY')! | ||
| const documentScrollHeight = Object.getOwnPropertyDescriptor(document.documentElement, 'scrollHeight') | ||
| const windowHasInstance = Object.getOwnPropertyDescriptor(Window, Symbol.hasInstance) | ||
|
|
||
| beforeAll(() => { | ||
| // Jest exposes window through a VM proxy that fails the native instanceof check. | ||
| const hasInstance = Window[Symbol.hasInstance] | ||
| Object.defineProperty(Window, Symbol.hasInstance, { | ||
| configurable: true, | ||
| value (instance: unknown) { | ||
| return instance === window || hasInstance.call(this, instance) | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| if (windowHasInstance) { | ||
| Object.defineProperty(Window, Symbol.hasInstance, windowHasInstance) | ||
| } else { | ||
| delete (Window as any)[Symbol.hasInstance] | ||
| } | ||
| }) | ||
|
|
||
| function page (path = `/scroll-${nextPath++}`) { | ||
| return { path, onPageScroll: jest.fn(), onReachBottom: jest.fn() } as unknown as PageInstance & { | ||
| onPageScroll: jest.Mock | ||
| onReachBottom: jest.Mock | ||
| } | ||
| } | ||
|
|
||
| function container (top = 0) { | ||
| const el = document.createElement('div') | ||
| el.scrollTop = top | ||
| Object.defineProperties(el, { | ||
| scrollHeight: { configurable: true, value: 1000 }, | ||
| clientHeight: { configurable: true, value: 200 } | ||
| }) | ||
| document.body.appendChild(el) | ||
| return el | ||
| } | ||
|
|
||
| function scroll (target: HTMLElement | Window, top: number) { | ||
| if (target instanceof Window) { | ||
| Object.defineProperty(target, 'scrollY', { configurable: true, value: top }) | ||
| } else { | ||
| target.scrollTop = top | ||
| } | ||
| target.dispatchEvent(new Event('scroll')) | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| jest.spyOn(window, 'addEventListener') | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| const calls = (window.addEventListener as jest.Mock).mock.calls | ||
| for (const [type, listener] of calls) { | ||
| if (type === 'scroll') window.removeEventListener(type, listener) | ||
| } | ||
| jest.restoreAllMocks() | ||
| Object.defineProperty(window, 'scrollY', windowScrollY) | ||
| if (documentScrollHeight) { | ||
| Object.defineProperty(document.documentElement, 'scrollHeight', documentScrollHeight) | ||
| } else { | ||
| delete (document.documentElement as any).scrollHeight | ||
| } | ||
| document.body.innerHTML = '' | ||
| }) | ||
|
|
||
| describe('page scroll bindings', () => { | ||
| test('each page reads its own scroll container', () => { | ||
| const a = page() | ||
| const b = page() | ||
| const aEl = container(17) | ||
| const bEl = container(99) | ||
| bindPageScroll(a, aEl) | ||
| bindPageScroll(b, bEl) | ||
| aEl.dispatchEvent(new Event('scroll')) | ||
| expect(a.onPageScroll).toHaveBeenCalledWith({ scrollTop: 17 }) | ||
| expect(b.onPageScroll).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('reach-bottom checks and resets use the bound container', () => { | ||
| const a = page() | ||
| const b = page() | ||
| const aEl = container() | ||
| bindPageScroll(a, aEl, 50) | ||
| bindPageScroll(b, container(799), 50) | ||
| scroll(aEl, 100) | ||
| expect(a.onReachBottom).not.toHaveBeenCalled() | ||
| scroll(aEl, 780) | ||
| scroll(aEl, 790) | ||
| expect(a.onReachBottom).toHaveBeenCalledTimes(1) | ||
| scroll(aEl, 100) | ||
| scroll(aEl, 780) | ||
| expect(a.onReachBottom).toHaveBeenCalledTimes(2) | ||
| expect(b.onReachBottom).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('rebinding a path removes its listener from the old container', () => { | ||
| const original = page() | ||
| const replacement = page(original.path) | ||
| const oldEl = container() | ||
| const newEl = container() | ||
| bindPageScroll(original, oldEl) | ||
| bindPageScroll(replacement, newEl) | ||
| scroll(oldEl, 780) | ||
| expect(original.onPageScroll).not.toHaveBeenCalled() | ||
| expect(original.onReachBottom).not.toHaveBeenCalled() | ||
| scroll(newEl, 780) | ||
| expect(replacement.onPageScroll).toHaveBeenCalledTimes(1) | ||
| expect(replacement.onReachBottom).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('repeated binding to the same container keeps one listener', () => { | ||
| const current = page() | ||
| const el = container() | ||
| bindPageScroll(current, el) | ||
| bindPageScroll(current, el) | ||
| bindPageScroll(current, el) | ||
| scroll(el, 780) | ||
| expect(current.onPageScroll).toHaveBeenCalledTimes(1) | ||
| expect(current.onReachBottom).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test.each(['window to element', 'element to window'])('rebinding from %s detaches the previous target', direction => { | ||
| const current = page() | ||
| const el = container() | ||
| const [oldTarget, newTarget] = direction === 'window to element' ? [window, el] : [el, window] | ||
| bindPageScroll(current, oldTarget) | ||
| bindPageScroll(current, newTarget) | ||
| scroll(oldTarget, 100) | ||
| expect(current.onPageScroll).not.toHaveBeenCalled() | ||
| scroll(newTarget, 200) | ||
| expect(current.onPageScroll).toHaveBeenCalledTimes(1) | ||
| expect(current.onPageScroll).toHaveBeenCalledWith({ scrollTop: 200 }) | ||
| }) | ||
|
|
||
| test('a window binding keeps using window metrics after an element is bound', () => { | ||
| const current = page() | ||
| Object.defineProperty(document.documentElement, 'scrollHeight', { configurable: true, value: window.innerHeight + 100 }) | ||
| bindPageScroll(current, window) | ||
| bindPageScroll(page(), container(400)) | ||
| scroll(window, 80) | ||
| expect(current.onPageScroll).toHaveBeenCalledWith({ scrollTop: 80 }) | ||
| expect(current.onReachBottom).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('an element binding keeps its metrics after a window is bound', () => { | ||
| const current = page() | ||
| const el = container() | ||
| bindPageScroll(current, el) | ||
| bindPageScroll(page(), window) | ||
| scroll(el, 780) | ||
| expect(current.onPageScroll).toHaveBeenCalledWith({ scrollTop: 780 }) | ||
| expect(current.onReachBottom).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('preserves the default threshold and strict reach-bottom boundary', () => { | ||
| const current = page() | ||
| const el = container() | ||
| bindPageScroll(current, el) | ||
| scroll(el, 750) | ||
| expect(current.onReachBottom).not.toHaveBeenCalled() | ||
| scroll(el, 751) | ||
| scroll(el, 799) | ||
| expect(current.onReachBottom).toHaveBeenCalledTimes(1) | ||
| scroll(el, 749) | ||
| scroll(el, 751) | ||
| expect(current.onReachBottom).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| test('supports pages without scroll callbacks', () => { | ||
| const el = container() | ||
| bindPageScroll({ path: `/empty-${nextPath++}` } as PageInstance, el) | ||
| expect(() => scroll(el, 780)).not.toThrow() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: NervJS/taro
Length of output: 9550
Script executed:
Repository: NervJS/taro
Length of output: 20379
Script executed:
Repository: NervJS/taro
Length of output: 38618
Script executed:
Repository: NervJS/taro
Length of output: 13250
在运行 Jest 前构建 workspace 依赖。
test和test:ci直接运行 Jest。新旧测试会分别解析@tarojs/runtime和@tarojs/taro-h5,但这两个包的入口都指向未提交的dist文件。Jest 配置没有将这些包映射到源码。因此,普通 fresh checkout 在未先执行根目录构建时会因模块不存在而失败。Suggested fix
Committable suggestion