-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
[bugfix] compatible <body />
is the scene of the scrolling container
#3844
Changes from all commits
3ab3fe6
2a07d42
9f567ab
b7d4243
ea67220
b38bec5
1361f6c
ad49c45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,9 +79,14 @@ export default { | |
}, | ||
|
||
methods: { | ||
onLoad(index) { | ||
onLoad(index, isRefresh) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段改动的原因是啥 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个和本次调整 之前 demo 里的 在实际应用中,应该是在接口请求后,旧的数据列表才被「第一页」替换掉,所以,在这里加了个 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get 👌 |
||
const list = this.list[index]; | ||
list.loading = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里丢了个主动将 |
||
setTimeout(() => { | ||
if (isRefresh) { | ||
list.items = []; | ||
} | ||
|
||
for (let i = 0; i < 10; i++) { | ||
const text = list.items.length + 1; | ||
list.items.push(text < 10 ? '0' + text : text); | ||
|
@@ -104,11 +109,10 @@ export default { | |
onRefresh(index) { | ||
const list = this.list[index]; | ||
setTimeout(() => { | ||
list.items = []; | ||
list.error = false; | ||
list.finished = false; | ||
list.refreshing = false; | ||
window.scrollTo(0, 10); | ||
this.onLoad(index, true); | ||
}, 1000); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,26 @@ type ScrollElement = HTMLElement | Window; | |
// get nearest scroll element | ||
// http://w3help.org/zh-cn/causes/SD9013 | ||
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome | ||
const overflowScrollReg = /scroll|auto/i; | ||
export function getScrollEventTarget(element: HTMLElement, rootParent: ScrollElement = window) { | ||
let node = element; | ||
while ( | ||
node && | ||
node.tagName !== 'HTML' && | ||
node.tagName !== 'BODY' && | ||
node.nodeType === 1 && | ||
node !== rootParent | ||
) { | ||
const { overflowY } = window.getComputedStyle(node); | ||
if (overflowY === 'scroll' || overflowY === 'auto') { | ||
return node; | ||
if (overflowScrollReg.test(<string>overflowY)) { | ||
if (node.tagName !== 'BODY') { | ||
return node; | ||
} | ||
|
||
// see: https://github.com/youzan/vant/issues/3823 | ||
const { overflowY: htmlOverflowY } = window.getComputedStyle(<Element>node.parentNode); | ||
if (overflowScrollReg.test(<string>htmlOverflowY)) { | ||
return node; | ||
} | ||
} | ||
node = <HTMLElement>node.parentNode; | ||
} | ||
|
@@ -33,11 +41,16 @@ export function getRootScrollTop(): number { | |
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; | ||
} | ||
|
||
export function setRootScrollTop(value: number) { | ||
setScrollTop(window, value); | ||
setScrollTop(document.body, value); | ||
} | ||
|
||
// get distance from element top to page top | ||
export function getElementTop(element: ScrollElement) { | ||
return ( | ||
(element === window ? 0 : (<HTMLElement>element).getBoundingClientRect().top) + | ||
getScrollTop(window) | ||
getRootScrollTop() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个函数实现逻辑感觉也有点问题,可能需要递归获取下 😹😹。考虑下面这样的结构: <!-- 1. 第一层,window 可以滚动 -->
<body>
<div style="height: 200vh;" />
<!-- 2. 第二层,祖先元素可以滚动 -->
<div style="height: 200px; overflow-y: scroll">
<div style="height: 200px;"></div>
<!-- 3. 第三层,父元素也能滚动 -->
<div style="height: 200px; overflow-y: scroll">
<div style="height: 200px;"></div>
<div style="height: 200px;"></div>
<div style="height: 200px;"></div>
</div>
<div style="height: 200px;"></div>
</div> 如果要获取第三层元素距离页面顶部的距离,就需要一级级遍历下其祖先元素,如果拥有 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
同时,组件内一些容器边界逻辑的判断,建议可以像之前 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我的看法:
|
||
); | ||
} | ||
|
||
|
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.
如果不考虑完善
getElementTop
场景,那这里加个判断改动是最小的