当前位置:   article > 正文

Hbuilder (H5+) plus.webview 预加载_hbuilderx webview加载url

hbuilderx webview加载url

                                       Hbuilder (H5+)  plus.webview 预加载

plus.webview.create   经常会用到  webview 来显示 Web网页,但是有时候 会出现白屏, 而且有些低端机器 时间还挺长。这个时候就需要用到预加载。

 

应为 我们要用的 plus.webview  创建完毕, 关闭不同 close(),  改用 hide(). 以此 提升效率,

在hide() 之前 为了 解决 下次打开  不残留上次页面 还要把 当前页面 置为空白

webview.loadData('<html><style="background-color: rgb(0, 0, 0);"></body></html>')

这里有几个 核心函数 贴出来

 

  1. 预加载 进入应用的时候 就创建

  1. function webviewInit() {
  2. if (window.plus) {
  3. let view = plus.webview.getWebviewById('h5View');
  4. if (!view) {
  5. createWebView()
  6. }
  7. }
  8. }

 

我的代码里面包含  两个按钮 不用的可以去掉

实现方法

  1. function createWebView() {
  2. ///创建 webView
  3. let webview = plus.webview.create('', 'h5View', {
  4. contentAdjust: false
  5. }, null);
  6. webview.setStyle({
  7. 'zindex': 30,
  8. 'titleNView': false,
  9. })
  10. //关闭按钮
  11. let bitmap_menu = new plus.nativeObj.Bitmap("closedmenu");
  12. bitmap_menu.loadBase64Data(
  13. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAAA+CAYAAAC4Gky6AAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxMzggNzkuMTU5ODI0LCAyMDE2LzA5LzE0LTAxOjA5OjAxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD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hisbTSNO1zu0bTtNNisbTl5eV9lJqaWjU+Pu4UsqyeHD9+HBkZGQBz2/YGN0yUk5OTf+bMmQl/FoiLXC4POnDgQFpycnISe6wvICAgGADu3bs3d6yvr6+v/8CBA8t2rC85ORnXrl0DGCu8Co+rdtTdu3eX9SC+1Wqd2bVrVwcArxtjy01gYCAqKipY5y/gua9IzMzMrNxoWICTJ0+yZyKnAFzni7OiyAUwGo3Q6XQA8/qACQBvn0LcuHHDL4PYJ5ETJ07ggw8+ABjl9eIhrw6s3I71gtFoZJXoBtAH5qUBrzyNb1osioCAAFRXV7PVeRZAPxZQIrByzXgeSUlJOH/+PLdN7IEPSgRWLHIOj4vvd8C0iT5ffH/mFbl7927s3buXHd64wYwTLWDeD/KZZ1aRBoMBubm52Lp1K+s1BeBfeMzHQZ4ZRVIUBbVajffeew9btmzB+vXr2aDfwSxAjGIRz9U8dYpkH1Bau3Yt4uLioFQqkZaWhtdff90z6m0wyhuHl0H2o0AJuPf1R8MNpvr+CiGe9FrKxP4gcB+ZmwHz9Az7yBzv7f+l4H8jtgIFOqJGnQAAAABJRU5ErkJggg=="
  14. )
  15. let leftPos = (window.screen.width - 80) + 'px';
  16. let viewClose = new plus.nativeObj.View('webViewClose', {
  17. top: '5px',
  18. left: leftPos,
  19. width: "50px",
  20. height: "37px"
  21. });
  22. viewClose.drawBitmap(bitmap_menu, {
  23. top: "0",
  24. left: "0",
  25. width: "82px",
  26. height: "62px"
  27. })
  28. viewClose.interceptTouchEvent(true);
  29. viewClose.addEventListener("click", function(e) {
  30. //console.log('=====> webViewClose click')
  31. webview.onloaded = function() {}
  32. plus.webview.getWebviewById('h5View').loadData('<html><style="background-color: rgb(0, 0, 0);"></body></html>')
  33. plus.webview.getWebviewById('h5View').hide()
  34. viewClose.hide()
  35. }.bind(this));
  36. //外部打开按钮
  37. let bitmap_menuOpen = new plus.nativeObj.Bitmap("openmenu");
  38. bitmap_menuOpen.loadBase64Data(
  39. "data:image/png;base64,iVBORw0KGgoA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= new plus.nativeObj.View('webViewOpen', {
  43. top: '5px',
  44. left: pos,
  45. width: "50px",
  46. height: "37px"
  47. });
  48. viewOpen.drawBitmap(bitmap_menuOpen, {
  49. top: "0",
  50. left: "0",
  51. width: "82px",
  52. height: "62px"
  53. })
  54. viewOpen.interceptTouchEvent(true);
  55. viewOpen.addEventListener("click", function(e) {
  56. //console.log('=====> webViewOpen click')
  57. plus.runtime.openURL(webViewUrl)
  58. plus.webview.getWebviewById('h5View').hide()
  59. viewOpen.hide()
  60. }.bind(this));
  61. }

 

调用接口

  1. function openUrlPay(url, data) {
  2. webViewUrl = url
  3. plus.navigator.setFullscreen(true);
  4. plus.device.setWakelock(true)
  5. let webview = plus.webview.getWebviewById('h5View');
  6. if (!webview) {
  7. console.log("webview 预加载失败")
  8. webviewInit()
  9. return
  10. }
  11. webview.addEventListener("hide", function() {
  12. //console.log("======>hide")
  13. plus.nativeObj.View.getViewById('webViewClose').hide()
  14. plus.nativeObj.View.getViewById('webViewOpen').hide()
  15. plus.navigator.setFullscreen(false);
  16. plus.device.setWakelock(false)
  17. }, false);
  18. // webview.onloaded = function() {
  19. // plus.nativeObj.View.getViewById('webViewClose').show()
  20. // plus.nativeObj.View.getViewById('webViewOpen').show()
  21. // //plus.webview.getWebviewById('h5View').show()
  22. // //webview.show()
  23. // }
  24. // webview.onhide = function() {
  25. // plus.nativeObj.View.getViewById('webViewClose').hide()
  26. // plus.nativeObj.View.getViewById('webViewOpen').hide()
  27. // plus.navigator.setFullscreen(false);
  28. // plus.device.setWakelock(false)
  29. // }
  30. webview.loadURL(url)
  31. webview.show()
  32. plus.nativeObj.View.getViewById('webViewClose').show()
  33. plus.nativeObj.View.getViewById('webViewOpen').show()
  34. //webview.append( plus.webview.open(url))
  35. webview.evalJS(
  36. "plus.key.addEventListener('backbutton',function () {});"
  37. );
  38. window.openGift = function() {
  39. plus.webview.getWebviewById('h5View').hide();
  40. router.push("/gift");
  41. }
  42. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/97483
推荐阅读
相关标签
  

闽ICP备14008679号