【发布时间】:2013-10-02 03:52:57
【问题描述】:
我在一个较小的 320x460px div 中显示 1000x1000px 的图像,并带有溢出:自动,因此您可以在 div 中滚动图像。 当我加载页面时,图像从图像的左上角显示,我想知道如何将图像居中显示?
任何意见表示赞赏,谢谢。
这里是代码。 在我的索引文件中,我像这样加载页面:
$('#kartamap').load('imagemap.html', function() {
alert(".load has loaded");
imagescroll();
});
这是我的页面。
<div id="themap">
<div id="imagediven" style="height:460px; width:320px;">
<img src="image.png" width="1000px" height="1000px"/>
</div>
<style>
#imagediven {
overflow:auto;
height:460px;
width:320px;
position:relative;
}
</style>
<script type="text/javascript">
function imagescroll(){
var element = document.getElementById('imagediven'), /* This is your div */
scrollHeight = element.scrollHeight,
scrollWidth = element.scrollWidth;
clientHeight =$(window).height();
clientWidth = $(window).width();
alert(scrollHeight);//this is not displaying the image height?
alert(clientHeight);//this is getting a value off the screen
element.scrollTop = (scrollHeight - clientHeight) / 2;
element.scrollLeft = (scrollWidth - clientWidth) / 2;
}
</script>
</div>
【问题讨论】:
-
div是否包含除您希望居中的图像以外的任何其他内容? -
试试 text-align:center;
-
尝试改变
在样式中的位置
-
嗨,马特,不仅仅是图片。
-
您的代码在哪里?请把它贴在这里..现在这里的每个用户都在猜测,因为你没有提供相关的代码。