通过JS控制外部CSS样式表,使一定大小的DIV在浏览器中动态居中
2010-11-16 23:00
387 查看
外部CSS样式表:indexRed.css
body{
text-align:center;
background-color:#7D1000;
}
#IndexImage{
position:absolute;
width:900px;
height:500px;
left:0px;
top:0px;
background-image:url(Red_Energetic.jpg);
background-repeat:no-repeat;
z-index: 1;
}
Html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" type="text/css" href="indexRed.css">
</head>
<body>
<div id="IndexImage">使这个DIV在浏览器中动态居中</div>
<script type="text/javascript">
function findDimensions()
{
var winWidth = 0;
var winHeight = 0;
var oStyleSheet = document.styleSheets[0];
var IndexImageStyle = oStyleSheet.rules[1];
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;
}
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
}
if(winHeight > parseInt(IndexImageStyle.style.height))
{
IndexImageStyle.style.top = (winHeight - parseInt(IndexImageStyle.style.height))/2 + 'px';
}
else
{
IndexImageStyle.style.top = 0 + 'px';
}
if(winWidth > parseInt(IndexImageStyle.style.width))
{
IndexImageStyle.style.left = (winWidth - parseInt(IndexImageStyle.style.width))/2 + 'px';
}
else
{
IndexImageStyle.style.left = 0 + 'px';
}
}
findDimensions();
window.onload=findDimensions;
window.onresize=findDimensions;
</script>
</body>
</html>
body{
text-align:center;
background-color:#7D1000;
}
#IndexImage{
position:absolute;
width:900px;
height:500px;
left:0px;
top:0px;
background-image:url(Red_Energetic.jpg);
background-repeat:no-repeat;
z-index: 1;
}
Html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" type="text/css" href="indexRed.css">
</head>
<body>
<div id="IndexImage">使这个DIV在浏览器中动态居中</div>
<script type="text/javascript">
function findDimensions()
{
var winWidth = 0;
var winHeight = 0;
var oStyleSheet = document.styleSheets[0];
var IndexImageStyle = oStyleSheet.rules[1];
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;
}
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
}
if(winHeight > parseInt(IndexImageStyle.style.height))
{
IndexImageStyle.style.top = (winHeight - parseInt(IndexImageStyle.style.height))/2 + 'px';
}
else
{
IndexImageStyle.style.top = 0 + 'px';
}
if(winWidth > parseInt(IndexImageStyle.style.width))
{
IndexImageStyle.style.left = (winWidth - parseInt(IndexImageStyle.style.width))/2 + 'px';
}
else
{
IndexImageStyle.style.left = 0 + 'px';
}
}
findDimensions();
window.onload=findDimensions;
window.onresize=findDimensions;
</script>
</body>
</html>
相关文章推荐
- 纯js实现Div在页面垂直居中!可根据浏览器大小的改变而改变!兼容各种浏览器----神器版!
- JS实现div动态水平垂直居中
- 通过radio和js控制div里的内容显示或者隐藏
- 通过div透明度实现隐藏div图层效果、通过js控制缩放div效果
- jQuery实现动态分割div—通过拖动分隔栏实现上下、左右动态改变左右、上下两个相邻div的大小
- js通过a链接控制多个DIV只显示其中一个其它隐藏
- 在js里通过id控制表或div的隐藏和显示,以及把id前缀相同的div全部隐藏起来
- SeimiAgent使用--通过js控制以浏览器级效果登陆爬取京东信息
- js控制使div自动适应居中
- js 控制浏览器窗口位置,大小
- JS中通过url动态获取图片大小的方法小结(两种方法)
- jquery入门 动态调整div大小,使其宽度始终为浏览器宽度
- JavaScript 的性能优化:加载和执行(以及动态引入的外部 JS 文件在各浏览器中的加载顺序不一致)
- SeimiAgent使用--通过js控制以浏览器级效果登录爬取京东信息
- js 获取div的类及js控制图片垂直居中
- jquery入门 动态调整div大小,使其宽度始终为浏览器宽度
- jquery入门 动态调整div大小,使其宽度始终为浏览器宽度
- 页面弹出新窗口,新开浏览器,控制大小,屏幕居中,隐藏浏览器工具栏
- 浏览器窗口大小改变时,动态改变div高度和宽度
- pc 电脑web浏览器js通过usb控制安卓手机打电话挂电话