Javascript 在页面中间将图像定中心
有人能解释一下为什么这个代码不起作用吗。浏览器以值vspace=“0”响应。这意味着算术表达式不正确,但为什么Javascript 在页面中间将图像定中心,javascript,html,image,centering,Javascript,Html,Image,Centering,有人能解释一下为什么这个代码不起作用吗。浏览器以值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
<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>
函数resizeImage()
{
var window_height=document.body.clientHeight
var window_width=document.body.clientWidth
var image_width=document.images[0]。宽度
var image\u height=document.images[0]。高度
可变高度比率=图像高度/窗口高度
变量宽度比=图像宽度/窗口宽度
变量纵横比=图像高度/图像宽度
if(高/宽比>宽/宽比)
{
document.images[0].style.width=“auto”;
document.images[0].style.height=“100%”;
}
其他的
{
var new_vspace=Math.round((窗高-窗宽*纵横比)/2);
document.images[0].style.width=“100%”;
document.images[0].style.height=“auto”;
document.images[0].vspace=“new_vspace”;
}
}
文件。写入(“”)
我复制粘贴了您的代码,并做了一个小改动
来自此
<center><img onload="resizeImage()" margin="0" border="0" src="nweshow/picture-0011.jpg"/></center>
<div style="text-align: center"><img onload="resizeImage()" margin="0" border="0" src="nweshow/picture-0011.jpg"/></div>
到此
<center><img onload="resizeImage()" margin="0" border="0" src="nweshow/picture-0011.jpg"/></center>
<div style="text-align: center"><img onload="resizeImage()" margin="0" border="0" src="nweshow/picture-0011.jpg"/></div>
除此之外,您的代码工作得非常好,图像的大小和中心没有任何问题
编辑:忘了提到我使用Flickr图像进行测试,下面是链接:通过将这一行添加到“resizeImage()”的条件[if part]中解决了问题: document.images[0].hspace=Math.floor((窗口宽度-图像宽度/高度比)/2) 并将此行添加到“else”部分: document.images[0].vspace=Math.floor((窗口高度-图像高度/宽度比)/2)
非常感谢 感谢您的建议,不要将vspace=的值放在引号内[与W3的引用相反],我直接将数学表达式作为其值编写[不带引号],并且不将其绑定到变量:将此行添加到“resizeImage()”的if部分: document.images[0].hspace=Math.floor((窗口宽度-图像宽度/高度比)/2) 在高度=100%的情况下,这将使图像水平居中 并将此行添加到“else”部分: document.images[0].vspace=Math.floor((窗口高度-图像高度/宽度比)/2) 当宽度调整为100%时,这会使图像垂直居中 这看起来很简单,CSS无法完成
再次感谢你的帮助 你不能只设置图像的CSS属性吗?使用CSS做这类事情通常更快、更安全,并且在调整窗口大小时不会滞后。你的代码对我来说很有用。您正在使用哪个浏览器?不要使用JS,而是这样做:document.images[0].vspace=“new_vspace”是否正常代码>新的空间写在“”之间?这不是var吗?回答adamj的最后一个答案。“文本对齐”选项仅使图像水平居中。顺便说一句,“堆栈溢出”添加注释的问题是,您无法通过按for在注释中开始新行,然后此键会立即添加[unfinished]注释,因此我必须小心。这就留下了图像垂直居中的问题,如果图像的宽度远大于我的代码=.54中定义的图片的高宽比。因此,屏幕底部有大量空白,但顶部没有空白。谢谢