我在一個較小的div 320x460px中使用overflow:auto顯示圖像1000x1000px,因此您可以在div內滾動圖像。 當我加載頁面時,圖像從圖像的左上角顯示出來,我想知道如何以中心顯示圖像?如何在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'含有別的其他然後你希望中心的形象呢? – Matt
嘗試text-align:center; –
嘗試更改風格的位置 –