最近有项目需求要内嵌网页功能,layaair自带的ifram只能加载项目目录下的本地网页,无法加载远程网页,因此我考虑用原生Ifame实现加载。
<iframe src="http://www.govenlive.me" ></iframe>
当然我得把滚动条隐藏掉
<style> iframe{ overflow-x : hidden; overflow-y : hidden; } </style>
以上是原生的写法,开始在layaair代码里实现创建iframe,代码很简单,以as3为例,先创建原生iframe,以及获取laya容器:
private var iframe = Browser.window.document.createElement('iframe'); private var layaContainer =Browser.window.document.getElementById("layaContainer");
然后设置层级并将iframe添加到laya容器:
iframe.style["z-index"] = 1; iframe.style["position"] = "absolute"; layaContainer.appendChild(iframe);
设置一些其他的css属性,比如iframe的宽高,位置等:
iframe.style["width"] = width.toString() + "px"; iframe.style["height"] = height.toString() + "px"; if (this.parent){ iframe.style["left"] = this.parent.x.toString() + "px"; iframe.style["right"] = this.parent.y.toString() + "px"; }
设置要加载的网页:
iframe.src = "http://www.govenlive.me";