有人可以解释为什么这个代码不起作用。浏览器以值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属性? – 2013-05-12 04:34:08
对这类事情使用CSS通常更快,更安全,并且在窗口调整大小时不会延迟。 – Greg 2013-05-12 04:40:15
而你的代码适合我。你正在使用哪种浏览器? – Greg 2013-05-12 04:42:09