4***@qq.com
4***@qq.com
  • 发布:2022-08-05 22:16
  • 更新:2022-08-06 10:25
  • 阅读:347

vue 下 plus.webview.create() 创建的weview 无法监听keyboardchange

分类:uni-app

detail.vue

onLoad() {  
            var wv = plus.webview.create("","custom-webview",{  
                'uni-app': 'none', //不加载uni-app渲染层框架,避免样式冲突  
            })  
            wv.loadURL("/hybrid/html/index.html")  
            var currentWebview = this.$scope.$getAppWebview();  
            currentWebview.append(wv);  
        }

index.html

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8">  
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
    <title></title>  
</head>  
<body>  
    <p><br></p<p><br></p><p><br></p>  
    <input type="text" />  
    <script>  
        document.addEventListener("plusready", onPlusReady, false);  
        function onPlusReady(){  
            document.addEventListener("keyboardchange", onKeyboardChange, false);  
        }  
        function onKeyboardChange(e){  
            var height = e.height;   // 键盘高度  
            console.log('键盘高度:'+height)  
            if(height > 0){  
                console.log('键盘显示');  
            }else{  
                console.log('键盘隐藏');   
            }  
        }     
    </script>  
</body>  
</html>

onPlusReady是走进去的,onKeyboardChange没有走进去,不知道是哪里配置没写对,还是其他原因,求解决方法!

2022-08-05 22:16 负责人:无 分享
已邀请:
4***@qq.com

4***@qq.com (作者)

我知道原因了,自问自答吧。
plus.webview.create() 创建的weview是子weview,软键盘其实是在主weview的(detail.vue),所以在子weview监听keyboardchange肯定不行, 只能在主weview监听,然后根据软键盘高度改变子weview的高度:

uni.onKeyboardHeightChange(res => {  
                var height = res.height;   // 键盘高度  
                if(height > 0){  
                    wv.setStyle({height:app.globalData.windowHeight - app.globalData.statusBarHeight - height})  
                }else{  
                    wv.setStyle({height:app.globalData.windowHeight - app.globalData.statusBarHeight})  
                }  
            })

子weview注册window.onresize,来接收软键盘的高度变化, 只是无法直接获得软键盘的高度,而是用了变通的办法,不知道还有其他办法没,等回复。。。

要回复问题请先登录注册