diff --git a/src/platforms/app-plus/view/mixins/native.js b/src/platforms/app-plus/view/mixins/native.js index 7f00078436469ab01b5cdacd0ed7a12385741100..b2fe601b262cc6a69fd0af694cf4e03d4c72ef00 100644 --- a/src/platforms/app-plus/view/mixins/native.js +++ b/src/platforms/app-plus/view/mixins/native.js @@ -1,3 +1,32 @@ +const TITLEBAR_HEIGHT = 44 + +function getStatusbarHeight () { + // 横屏时 iOS 获取的状态栏高度错误,进行纠正 + return plus.navigator.isImmersedStatusbar() ? Math.round(plus.os.name === 'iOS' ? plus.navigator.getSafeAreaInsets().top : plus.navigator.getStatusbarHeight()) : 0 +} + +function getNavigationBarHeight () { + const webview = plus.webview.currentWebview() + let style = webview.getStyle() + style = style && style.titleNView + if (style && style.type === 'default') { + return TITLEBAR_HEIGHT + getStatusbarHeight() + } + return 0 +} + +function getFixed ($el) { + let fixed + while ($el) { + const style = getComputedStyle($el) + const transform = style.transform || style.webkitTransform + fixed = transform && transform !== 'none' ? false : fixed + fixed = style.position === 'fixed' ? true : fixed + $el = $el.parentElement + } + return fixed +} + export default { name: 'Native', data () { @@ -28,10 +57,13 @@ export default { const rect = (this.$refs.container || this.$el).getBoundingClientRect() this.hidden = rect.width === 0 || rect.height === 0 if (!this.hidden) { - ['top', 'left', 'width', 'height'].forEach(key => { + const position = this.position + position.position = getFixed(this.$el) ? 'absolute' : 'static' + const keys = ['top', 'left', 'width', 'height'] + keys.forEach(key => { let val = rect[key] - val = key === 'top' ? val + (document.documentElement.scrollTop || document.body.scrollTop || 0) : val - this.position[key] = val + 'px' + val = key === 'top' ? val + (position.position === 'static' ? (document.documentElement.scrollTop || document.body.scrollTop || 0) : getNavigationBarHeight()) : val + position[key] = val + 'px' }) } },