【发布时间】:2013-05-06 10:25:20
【问题描述】:
谁能解释一下为什么这段代码不起作用。浏览器以值 vspace="0" 响应。这意味着算术表达式不正确,但是为什么呢?
<script type="text/javascript">
function resizeImage()
{
var window_height = document.body.clientHeight
var window_width = document.body.clientWidth
var image_width = document.images[0].width
var image_height = document.images[0].height
var height_ratio = image_height / window_height
var width_ratio = image_width / window_width
var aspect_ratio = image_height / image_width
if (height_ratio > width_ratio)
{
document.images[0].style.width = "auto";
document.images[0].style.height = "100%";
}
else
{
var new_vspace = Math.round((window_height - window_width*aspect_ratio) / 2);
document.images[0].style.width = "100%";
document.images[0].style.height = "auto";
document.images[0].vspace="new_vspace";
}
}
</script>
</head>
<BODY bgcolor=#000000 onresize="resizeImage()">
<script>
document.write('<center><img onload="resizeImage()" margin="0" border="0" src="nweshow/picture-0011.jpg"/></center>')
</script>
</body>
【问题讨论】:
-
不能只设置图片的CSS属性吗?
-
对这类事情使用 CSS 通常更快、更安全,并且在调整窗口大小时不会滞后。
-
你的代码对我有用。你用的是哪个浏览器?
-
不要使用 JS,而是这样做:css-tricks.com/perfect-full-page-background-image
-
document.images[0].vspace="new_vspace";new_vspace写在“”之间正常吗?不是 var 吗?
标签: javascript html image centering