需求

在egret中嵌入网页,类似

网上大概有两种思路吧,一种是直接在body里面加入iframe,如:【Egret】里使用iframe标签达到内嵌多个web界面;另一种就是通过模仿htmlinput来做,如egret的WebView实现。具体要什么样子看需求吧。


Egret由两个层组成,简单看一个页面:

id为“canvasDiv的DIV”层是一个canvas,主要用于文字、位图、矢量图等的渲染,id为“StageDelegateDiv”的层是使用HTML原生的输入文本组件,如下图:

输入文本这块有不少坑,比如焦点啥的

最终代码如下:

  1. /**
  2. * WebView
  3. * 适配FIXED_WIDTH、FIXED_HEIGHT、NO_BORDER、SHOW_ALL四种缩放模式
  4. * 暂未考虑屏幕大小改变、屏幕旋转以及单页面多Webplay实例的情形
  5. * Created by yxiao on 2015/9/30.
  6. */
  7. class WebView extends egret.DisplayObjectContainer {
  8. private _x:number=0;
  9. private _y:number=0;
  10. private _width:number=0;
  11. private _height:number=0;
  12. private _src:string="";
  13. private _scaleMode:string=egret.MainContext.instance.stage.scaleMode;
  14. private _stageW:number;
  15. private _stageH:number;
  16. private _windowW:number;
  17. private _windowH:number;
  18. private _displayH:number;
  19. private _displayW:number;
  20. private _designH:number;
  21. private _designW:number;
  22. private _iframeWrapper:HTMLDivElement=null;
  23. private _iframe:HTMLIFrameElement=null;
  24. /**
  25. * @param src
  26. */
  27. public constructor(src:string){
  28. super();
  29. var stageDelegateDom:HTMLElement=document.getElementById("StageDelegateDiv"),playerContainer:HTMLElement=stageDelegateDom.parentElement;
  30. var iframeWrapperDom=document.getElementById("iframe-wrapper");
  31. if(!iframeWrapperDom){
  32. iframeWrapperDom=document.createElement("div");
  33. iframeWrapperDom.style.display="none";
  34. iframeWrapperDom.attributes['style'].value+='position:absolute;-webkit-overflow-scrolling: touch;overflow-y: scroll;';//解决iframe在ios下的显示问题
  35. iframeWrapperDom.id="iframe-wrapper";
  36. stageDelegateDom.appendChild(iframeWrapperDom);
  37. }
  38. this._iframeWrapper=<HTMLDivElement>iframeWrapperDom;
  39. this._iframeWrapper.style.display="none";
  40. this._iframeWrapper.style.opacity="0";
  41. var iframe = document.createElement("iframe"),t=new Date().getTime();
  42. iframe.src=src;
  43. iframe.id="webview-iframe-"+t;
  44. iframe.name="webview-iframe-"+t;
  45. iframe.style.position="absolute";
  46. iframe.style.top="0";
  47. iframe.style.left="0";
  48. iframe.style.opacity="0";
  49. iframe.style.display='none';
  50. iframe.frameBorder='0';
  51. iframe.border="0";
  52. this._iframeWrapper.appendChild(iframe);
  53. this._iframe=<HTMLIFrameElement>document.getElementById("webview-iframe-"+t);
  54. var self=this;
  55. this._iframe.onload=function(){
  56. self._iframeWrapper.style.opacity="1";
  57. self._iframe.style.opacity="1";
  58. }
  59. this._stageW=egret.MainContext.instance.stage.stageWidth;
  60. this._stageH=egret.MainContext.instance.stage.stageHeight;
  61. this._windowW=window.innerWidth;
  62. this._windowH=window.innerHeight;
  63. this._designH=parseInt(playerContainer.attributes['data-content-height'].value);
  64. this._designW=parseInt(playerContainer.attributes['data-content-width'].value);
  65. var stageSize = egret.sys.screenAdapter.calculateStageSize(egret.MainContext.instance.stage.scaleMode, this._windowW, this._windowH, this._designW, this._designH);
  66. this._displayH=stageSize.displayHeight;
  67. this._displayW=stageSize.displayWidth;
  68. console.log("windowW:"+this._windowW);
  69. console.log("stageW:"+this._stageW);
  70. console.log("disPlayW:"+this._displayW);
  71. console.log("windowH:"+this._windowH);
  72. console.log("stageH:"+this._stageH);
  73. console.log("displayH:"+this._displayH);
  74. }
  75. public show():void {
  76. this._iframe.style.display='block';
  77. this._iframeWrapper.style.display='block';
  78. }
  79. public destroy():void {
  80. if(this._iframe){
  81. this._iframeWrapper.style.display="none";
  82. this._iframeWrapper.removeChild(this._iframe);
  83. }
  84. }
  85. public get width():number {
  86. return this._width;
  87. }
  88. public set width(value:number) {
  89. this._width = value;
  90. if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT ){
  91. this._iframe.width=this._width/this._stageW*this._windowW+"px";
  92. this._iframeWrapper.style.width=this._width/this._stageW*this._windowW+"px";
  93. }
  94. if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER ) {
  95. if(this._windowW==this._displayW){
  96. this._iframe.style.width = this._width / this._stageW * this._windowW + "px";
  97. this._iframeWrapper.style.width = this._width / this._stageW * this._windowW + "px";
  98. }else{
  99. this._iframe.style.width = this._width / this._stageW * this._displayW + "px";
  100. this._iframeWrapper.style.width = this._width / this._stageW * this._displayW + "px";
  101. }
  102. }
  103. }
  104. public get height():number {
  105. return this._height;
  106. }
  107. public set height(value:number) {
  108. this._height = value;
  109. if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT ) {
  110. this._iframe.height=this._height/this._stageH*this._windowH+"px";
  111. this._iframeWrapper.style.height=this._height/this._stageH*this._windowH+"px";
  112. }
  113. if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER ) {
  114. if(this._windowH==this._displayH){
  115. this._iframe.style.height = this._height / this._stageH * this._windowH + "px";
  116. this._iframeWrapper.style.height = this._height / this._stageH * this._windowH + "px";
  117. }else{
  118. this._iframe.style.height = this._height / this._stageH * this._displayH + "px";
  119. this._iframeWrapper.style.height = this._height / this._stageH * this._displayH + "px";
  120. }
  121. }
  122. }
  123. public set x(value:number) {
  124. this._x = value;
  125. if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT) {
  126. this._iframeWrapper.style.left = this._x / this._stageW * this._windowW + "px";
  127. }
  128. if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER ) {
  129. if(this._windowW==this._displayW){
  130. this._iframeWrapper.style.left = this._x / this._stageW * this._windowW + "px";
  131. }else{
  132. this._iframeWrapper.style.left = this._x / this._stageW * this._displayW + "px";
  133. }
  134. }
  135. }
  136. public set y(value:number) {
  137. this._y = value;
  138. if(this._scaleMode==egret.StageScaleMode.FIXED_WIDTH || this._scaleMode==egret.StageScaleMode.FIXED_HEIGHT ) {
  139. this._iframeWrapper.style.top = this._y / this._stageH * this._windowH + "px";
  140. }
  141. if(this._scaleMode==egret.StageScaleMode.SHOW_ALL || this._scaleMode==egret.StageScaleMode.NO_BORDER){
  142. if(this._windowH==this._displayH){
  143. this._iframeWrapper.style.top = this._y / this._stageH * this._windowH + "px";
  144. }else{
  145. this._iframeWrapper.style.top =this._y / this._stageH * this._displayH + "px";
  146. }
  147. }
  148. }
  149. public get x():number {
  150. return this._x;
  151. }
  152. public get y():number {
  153. return this._y;
  154. }
  155. public get src():string {
  156. return this._src;
  157. }
  158. public set src(value:string) {
  159. this._src = value;
  160. }
  161. }

参考资料:

http://bbs.egret.com/thread-11245-1-1.html

input输入框的type技巧

http://blog.csdn.net/arvin0/article/details/51437863

【Egret】里使用iframe标签达到内嵌多个web界面

http://blog.csdn.net/xiaoyang0611/article/details/49128077

egret的WebView实现

http://www.html5party.com/2503.html

【HTML5】Egret笔记(一):罗列细碎几个点

egret的WebView实现的更多相关文章

  1. egret随笔-egret浅入浅出

    •不知道有多人跟笔者一样,喜欢学各种技术,但是都不精,但也有一两项算是精的. 自从踏上了egret游戏开发的道路,就不得不学习各种技术了,因为,要精通egret,首先必须要会TypeScript,其次 ...

  2. Android混合开发之WebView与Javascript交互

    前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...

  3. android通过webview调起支付宝app支付

    webview在加载网页的时候会默认调起手机自带的浏览器加载网页,用户体验不好.但当用户设置浏览器客户端(setWebViewClient)设置这样的监听事件之后,当请求url的时候就不会打开手机自带 ...

  4. [Egret]优雅的写http

    首先,自从使用链式调用的写法后,就一发不可收拾的喜爱上了这种优雅的方式.不管是写架构还是写模块,我都会不自觉的使用这种最优雅的方式.链式写法既减少了代码量,又非常优雅的. 在使用 egret 的htt ...

  5. egret调用页面js的方法。

    参考文献: http://bbs.egret-labs.org/thread-267-3-1.html http://docs.egret-labs.org/post/manual/threelibs ...

  6. egret GUI 和 egret Wing 是我看到h5 最渣的设计

    一个抄袭FlexLite抄的连自己思想都没有,别人精髓都不懂的垃圾框架.也不学学MornUI,好歹有点自己想法. 先来个最小可用集合吧: 1. egret create legogame --type ...

  7. Android WebView 优化页面加载效果

    目前带有Web功能的APP越来越多,为了能够更好的使用WebView展示页面,可以考虑做相关的优化:WebView 缓存,资源文件本地存储,客户端UI优化. 可能有些人会说,为什么不做Native的, ...

  8. Android 浏览器 —— 使用 WebView 实现文件下载

    对当前的WebView设置下载监听 mCurrentWebView.setDownloadListener(new DownloadListener() { @Override public void ...

  9. Android混合开发之WebView使用总结

    前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...

随机推荐

  1. PowerPC-Link Command File解析

    https://mp.weixin.qq.com/s/CATWma2mv5IPYGtKZLuGDA   以Code Warrior 11生成的flash版本(FLASH.lcf)为例   一. 参考资 ...

  2. 工业级CC1125模块有哪些优势?主要应用领域?

    CC1125无线模块是基于 TI 的 CC1125无线收发芯片设计,是一款完整的.体积小巧的.低功耗的无线收发模块.是 TI Chipcon 推出的 ISM 频段高性能无线收发芯片之一,最大输出功率可 ...

  3. Java实现 蓝桥杯VIP 算法提高 连接乘积

    算法提高 连接乘积 时间限制:1.0s 内存限制:256.0MB 问题描述 192这个数很厉害,用它分别乘以1.2.3,会得到: 192 x 1 = 192 192 x 2 = 384 192 x 3 ...

  4. Java实现 LeetCode 41 缺失的第一个正数

    41. 缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: ...

  5. NodeJS及路由

    1.基本介绍- http://nodejs.cn/api/ Node.js 是一个基于Chrome V8 引擎的JavaScript运行环境 Node.js使用了一个事件驱动.非阻塞式I/O的模型,使 ...

  6. 阿里云杨敬宇:边缘计算行业通识与阿里云ENS的技术演进之路

    近日,阿里云杨敬宇在CSDN阿里云核心技术竞争力在线峰会上进行了<5G基础设施-阿里云边缘计算的技术演进之路>主题演讲,针对5G时代下,行业和技术的趋势.边缘计算产业通识以及阿里云边缘计算 ...

  7. 4.vue class 绑定- model基础应用

        //代码可以复制自行体验   <template>     <div id="app" @click.stop="test('你点击了我big- ...

  8. Springboot 的单元测试

    1 测试基础类 @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment. ...

  9. wdcp如何添加反向代理功能

    1.winscp进入目录 /www/wdlinux/httpd-x.x.x/conf/右键编辑 httpd.conf 这个文件 依次把下面文件名字前面的 # 号去掉 LoadModule proxy_ ...

  10. RabbitMQ系列之【启动过程中遇到问题及解决方案】

    1.如果显示找不到主机,请在hosts文件中添加: vi /etc/hosts 127.0.0.1 localhost 2.从3.3.1版本开始,RabbitMQ默认不允许远程ip登录,即只能使用lo ...