Hbuilder (H5+) plus.webview 预加载

                                       Hbuilder (H5+)  plus.webview 预加载

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

 

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

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

webview.loadData('')

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

 

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

function webviewInit() {

  if (window.plus) {
    let view = plus.webview.getWebviewById('h5View');
    if (!view) {
      createWebView()
    }
  }
}

 

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

实现方法

Hbuilder (H5+) plus.webview 预加载_第1张图片

function createWebView() {

  ///创建 webView
  let webview = plus.webview.create('', 'h5View', {
    contentAdjust: false
  }, null);
  webview.setStyle({
    'zindex': 30,
    'titleNView': false,
  })





  //关闭按钮
  let bitmap_menu = new plus.nativeObj.Bitmap("closedmenu");
  bitmap_menu.loadBase64Data(
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAAA+CAYAAAC4Gky6AAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxMzggNzkuMTU5ODI0LCAyMDE2LzA5LzE0LTAxOjA5OjAxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIi8+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgo8P3hwYWNrZXQgZW5kPSJyIj8+IEmuOgAACxxJREFUeJztnH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gQFpycnISe6wvICAgGADu3bs3d6yvr6+v/8CBA8t2rC85ORnXrl0DGCu8Co+rdtTdu3eX9SC+1Wqd2bVrVwcArxtjy01gYCAqKipY5y/gua9IzMzMrNxoWICTJ0+yZyKnAFzni7OiyAUwGo3Q6XQA8/qACQBvn0LcuHHDL4PYJ5ETJ07ggw8+ABjl9eIhrw6s3I71gtFoZJXoBtAH5qUBrzyNb1osioCAAFRXV7PVeRZAPxZQIrByzXgeSUlJOH/+PLdN7IEPSgRWLHIOj4vvd8C0iT5ffH/mFbl7927s3buXHd64wYwTLWDeD/KZZ1aRBoMBubm52Lp1K+s1BeBfeMzHQZ4ZRVIUBbVajffeew9btmzB+vXr2aDfwSxAjGIRz9U8dYpkH1Bau3Yt4uLioFQqkZaWhtdff90z6m0wyhuHl0H2o0AJuPf1R8MNpvr+CiGe9FrKxP4gcB+ZmwHz9Az7yBzv7f+l4H8jtgIFOqJGnQAAAABJRU5ErkJggg=="
  )
  let leftPos = (window.screen.width - 80) + 'px';
  let viewClose = new plus.nativeObj.View('webViewClose', {
    top: '5px',
    left: leftPos,
    width: "50px",
    height: "37px"
  });
  viewClose.drawBitmap(bitmap_menu, {
    top: "0",
    left: "0",
    width: "82px",
    height: "62px"
  })
  viewClose.interceptTouchEvent(true);
  viewClose.addEventListener("click", function(e) {
    //console.log('=====> webViewClose click')
    webview.onloaded = function() {}
    plus.webview.getWebviewById('h5View').loadData('')
    plus.webview.getWebviewById('h5View').hide()

    viewClose.hide()

  }.bind(this));


  //外部打开按钮
  let bitmap_menuOpen = new plus.nativeObj.Bitmap("openmenu");
  bitmap_menuOpen.loadBase64Data(
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFIAAAA+CAYAAAC4Gky6AAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4P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NHqFnuNwOBzODvn3ABkZGWrkBSAsLGzq0M9evXrVGx0dXXDlyhWPjzxotVof1/dPnz69s3Tp0ty9e/fekUOnJzhbbYAuAL1ej+aNPQPKIIqiz9DP8vLy/tDd3W0ZSRyLxfJ6T01nZ+fFqVOnFpw6dapXDo2eYrPZXhtpRnpWsmnTJlWS2+12m+v7mpqaotLS0s6Rxtm5c2f99evXaxsbG49ERkZ+Y7PZFD3KNhw2m83uWvzuAaxerU7/2HUZ1WQy/WfFihV/H02c8+fPm+Lj4w/qdLpxW5B3rdoADwFzcnIyer1e8eQ9PT3/db7evXv3nxRPqCCujQ2AA7gD6lTvmzdv3gepcdizZ8/7Mz0/CgYGBuxDW5ZOwLRw4ULFd1ycO3euC6CiouKQoolUYGjVBqlUXgcc2dnZLFiwQLHkXV1d/ffu3fsuPz//imJJVMJisdgEN+e1Y4HZDQ0NpKSkYLGMqEfywZGWlhbmrtP4b6BPp9N51Ra/8aKvr8/mzkgb8C/AunbtWkpLS1WU5X28ePHC+rZhzAskM+0bN26kuLhYJVnex6NHjwbcPSNdcR6hE8rKyibO3LjBEyNhyKFOvV4/0QANwdMZiofAZQafmXV1dYp2jbwRT0ukE+f5xECYOPjuykiNhCFXMVy4cIGDBw9SUVEhuzhvYjRGOgkB4hjcX3n69GmOHj1KeXn5B3k5yFiMBGm3WCQwj8FdbSBdV3PkyBEaGxu9YhewHIzVSCcapJMRkQy5QKmmpobm5mZaW1u5e/cujx8//lFeoCSXka44r/SahlTtP4grvZQw0hUf/n/JXBDgx4/0krn/AR6XtWUO9nCVAAAAAElFTkSuQmCC"
  )
  let pos = (window.screen.width - 80 - 50) + 'px';
  let viewOpen = new plus.nativeObj.View('webViewOpen', {
    top: '5px',
    left: pos,
    width: "50px",
    height: "37px"
  });
  viewOpen.drawBitmap(bitmap_menuOpen, {
    top: "0",
    left: "0",
    width: "82px",
    height: "62px"
  })
  viewOpen.interceptTouchEvent(true);
  viewOpen.addEventListener("click", function(e) {
    //console.log('=====> webViewOpen click')
    plus.runtime.openURL(webViewUrl)
    plus.webview.getWebviewById('h5View').hide()
    viewOpen.hide()
  }.bind(this));


}

 

调用接口

function openUrlPay(url, data) {
  webViewUrl = url
  plus.navigator.setFullscreen(true);
  plus.device.setWakelock(true)

  let webview = plus.webview.getWebviewById('h5View');
  if (!webview) {
    console.log("webview 预加载失败")
    webviewInit()
    return
  }


  webview.addEventListener("hide", function() {
    //console.log("======>hide")
    plus.nativeObj.View.getViewById('webViewClose').hide()
    plus.nativeObj.View.getViewById('webViewOpen').hide()
    plus.navigator.setFullscreen(false);
    plus.device.setWakelock(false)
  }, false);


  // webview.onloaded = function() {
  //   plus.nativeObj.View.getViewById('webViewClose').show()
  //   plus.nativeObj.View.getViewById('webViewOpen').show()
  //   //plus.webview.getWebviewById('h5View').show()
  //   //webview.show()
  // }
  // webview.onhide = function() {
  //   plus.nativeObj.View.getViewById('webViewClose').hide()
  //   plus.nativeObj.View.getViewById('webViewOpen').hide()
  //   plus.navigator.setFullscreen(false);
  //   plus.device.setWakelock(false)
  // }

  webview.loadURL(url)
  webview.show()
  plus.nativeObj.View.getViewById('webViewClose').show()
  plus.nativeObj.View.getViewById('webViewOpen').show()
  //webview.append(  plus.webview.open(url))


  webview.evalJS(
    "plus.key.addEventListener('backbutton',function () {});"
  );
  window.openGift = function() {
    plus.webview.getWebviewById('h5View').hide();
    router.push("/gift");
  }


}

 

你可能感兴趣的:(Vue)