* [html5] fix scroll to element for window scroll.
Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/7eb86571 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/7eb86571 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/7eb86571 Branch: refs/heads/0.12-dev Commit: 7eb865715bf7b08eadf37f2965f3457dd5a0a176 Parents: 5ce1c11 Author: MrRaindrop <tekk...@gmail.com> Authored: Fri Apr 21 12:12:40 2017 +0800 Committer: MrRaindrop <tekk...@gmail.com> Committed: Fri Apr 21 12:12:40 2017 +0800 ---------------------------------------------------------------------- html5/render/vue/modules/dom.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/7eb86571/html5/render/vue/modules/dom.js ---------------------------------------------------------------------- diff --git a/html5/render/vue/modules/dom.js b/html5/render/vue/modules/dom.js index 117eaeb..43db8a6 100644 --- a/html5/render/vue/modules/dom.js +++ b/html5/render/vue/modules/dom.js @@ -91,20 +91,30 @@ export default { } const scroller = getParentScroller(vnode) - const scrollDirection = scroller.scrollDirection || 'vertical' + const scrollDirection = scroller && scroller.scrollDirection || 'vertical' - if (scroller && scroller.$el && vnode.$el) { + const isWindow = !scroller + const ct = isWindow ? document.body : scroller.$el + const el = vnode.$el + + if (ct && el) { // if it's a list, then the listVnode.scrollDirection is undefined. just // assum it is the default value 'vertical'. const dSuffix = ({ horizontal: 'Left', vertical: 'Top' })[scrollDirection] - let offset = vnode.$el[`offset${dSuffix}`] + + const ctRect = ct.getBoundingClientRect() + const elRect = el.getBoundingClientRect() + + const dir = dSuffix.toLowerCase() + let offset = el[`scroll${dSuffix}`] + elRect[dir] - ctRect[dir] + // let offset = el[`offset${dSuffix}`] if (options) { - offset += Number(options.offset) || 0 - offset *= weex.config.env.scale /* adapt offset to different screen scales. */ + offset += options.offset && options.offset * weex.config.env.scale || 0 + // offset *= weex.config.env.scale /* adapt offset to different screen scales. */ } else if (process.env.NODE_ENV === 'development') { console.warn('[Vue Render] The second parameter of "scrollToElement" is required, ' @@ -112,14 +122,14 @@ export default { } if (options && options.animated === false) { - return scrollElement.call(scroller.$el, dSuffix, offset) + return scrollElement.call(ct, dSuffix, offset) } step({ - scrollable: scroller.$el, + scrollable: ct, startTime: now(), frame: null, - startPosition: scroller.$el[`scroll${dSuffix}`], + startPosition: ct[`scroll${dSuffix}`], position: offset, method: scrollElement, dSuffix: dSuffix