当前位置:首页 > 代码·功能 > 正文

iframe自适应高度,加载完成判断,加载中效果

有的时候想弄个提交表单,但是在主题内的样式总是不合适,只好使用iframe来插入网页。

但是主题分为两种模式 手机 和电脑。在电脑上显示很合适,但是在手机上……不说了都懂得。

那么怎么才能在手机上美观的插入呢。

使用自适应吧。一切替你完成,附送加载状态 一枚。

看一送一,是不是很划算那!

一、iframe自适应

var winWidth = 0;
var winHeight = 0;
var ratioWidth = 0;
var ratioHeight = 0;
function currentWindSize() //函数:获取当前窗口尺寸
{
//获取窗口宽度
if (window.innerWidth)
winWidth = window.innerWidth;
else if ((document.body) && (document.body.clientWidth))
winWidth = document.body.clientWidth;
//获取窗口高度
if (window.innerHeight)
winHeight = window.innerHeight;
else if ((document.body) && (document.body.clientHeight))
winHeight = document.body.clientHeight;
//通过深入Document内部对body进行检测,获取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
}
}
//设置iframe高度
function setIframeHeight(winHeight) {
$("#iframepage").height(winHeight - 100)
}
function resize() {
currentWindSize();
setIframeHeight(winHeight);
}
//获取当前窗口大小
currentWindSize();

window.onresize = resize;
setIframeHeight(winHeight);

 二、iframe加载完成状态判断

var iframe = document.createElement("iframe");
iframe.src = "http://www.baidu.com";
iframe.style.overflowX = "hidden";
iframe.setAttribute("width", 1018);
iframe.setAttribute("frameborder", "no");
iframe.setAttribute("border", "0");
iframe.setAttribute("marginwidth", "0");
iframe.setAttribute("marginheight", "0");
iframe.setAttribute("allowtransparency", "yes");
iframe.setAttribute("scrolling", "yes");
iframe.setAttribute("id", "iframepage");
iframe.setAttribute("name", "iframepage");
if (iframe.attachEvent) {
iframe.attachEvent("onload", function () {
document.getElementById("loading").style.display = "none";
});
} else {
iframe.onload = function () {
document.getElementById("loading").style.display = "none";
};
}
document.getElementById("iframeDiv").appendChild(iframe);

 

标签:
上一篇: 下一篇:

8 条评论

评论加载中...
  1. 438490 646458But wanna admit that this really is extremely helpful , Thanks for taking your time to write this. 978320

    2018年8月14日 03:04 评论
  2. 5楼
    未知系统 谷歌浏览器 Windows 10
    calonye  

    这两段都是放到主题的js里面吗?

    2016年8月2日 12:00 评论
  3. 4楼
    来自天朝的朋友 未知浏览器 Unknow Os
    云落  

    -_-|| 其实不需要这么来的,宽度100%,高度用mobile判断,使用两种高度,我主题插入视频自适应就是这么处理的

    2015年3月1日 15:46 评论
  4. 那个小人好可爱!

    2015年1月23日 18:37 评论
  5. 板凳
    Hong Kong 未知浏览器 Unknow Os
    李明  

    自适应效果不错。

    2015年1月9日 17:22 评论
  6. 沙发
    来自天朝的朋友 未知浏览器 Unknow Os
    我要钱来赚  

    海到天边云做岸,山登绝顶我为峰! !这话好熟悉。。。。。 仙路尽头谁为峰,一见无始道成空

    2015年1月9日 13:05 评论

发表评论

不理你。不要啊!吃饭。吃惊。吃西瓜。飞吻!恭喜!Hi纠结!膜拜!OK抛媚眼。泡泡糖。抛钱。忍!生闷气!调皮。偷看。委屈。献花。疑问?抓狂!

小提示:Ctrl+Enter快速提交助您一臂之力~
加载中……