日期:2014-05-16  浏览次数:20638 次

iframe居中
页面上有一个iframe

目前是全屏的时候水平居中

我想让它在窗口不是全屏的时候仍然在当前窗口居中
------解决方案--------------------
position:absolute;left:50%;margin-left:-iframe宽度/2
比如iframe宽100px,设置margin-left:-50
------解决方案--------------------
框架页面内水平垂直居中
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="textml; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.box{ position:absolute;}
</style>
<script>
window.onload=function()
{
var oBox=document.getElementById('box');
adjustResize();
window.onresize=window.onscroll=adjustResize;
function adjustResize()
{
var iWidth=document.documentElement.clientWidth;
var iHegiht=Math.max(document.documentElement.clientHeight,document.body.clientHeight);
oBox.style.left=(iWidth-oBox.offsetWidth)/2+'px';
oBox.style.top=(iHegiht-oBox.offsetHeight)/2+'px';
}
}
</script>
</head>

<body>
<div class="box" id="box">
<iframe width="800" height="400" frameborder="0" scrolling="no" src="http://www.baidu.com"></iframe>
</div>
</body>
<html>