我想弄清楚如何动态地将边距插入到“mainImage”中。我想要做的是垂直居中的图像,它具有动态的高度和宽度(和动态客户端视口)。我无法弄清楚如何将利润添加到图像作为他们正在从JavaScript加载中...在图像上添加边距?
我有以下HTML:
<div id="content">
<img id="left" onclick="prevImage('images/52/image_','mainImage',52)" src="icons/arrow_left.png" />
<img id="right" onclick="nextImage('images/52/image_','mainImage',52)" src="icons/arrow_right.png"/>
<img id="mainImage" src="images/52/image_0.jpg" onload="addMargins()" />
</div>
与下面的CSS:
div#content{
position: absolute;
margin-left: 20%;
margin-right: 5%;
width: 75%;
height: 95%;
text-align: center;
}
div#content img#left{
position: absolute;
left: 0;
top: 50%;
}
div#content img#right{
position: absolute;
right: 0;
top: 50%;
}
div#content img#mainImage{
max-height: 100%;
max-width: 95%;
}
和JavaScript:
var key = 1;
function nextImage(thePath, id, max)
{
if(key == max)
{
key = 1;
}else{
key++;
}
var image = thePath + key + ".jpg";
document.getElementById(id).src= image;
};
function prevImage(thePath, id, max)
{
if(key == 1)
{
key = max;
}else{
key--;
}
var image = thePath + key + ".jpg";
document.getElementById(id).src= image;
};
所以我的想法是做一些在javascript,基本上SA ys:
"var viewport= viewport.height;
var mainImage= mainImage.height;
set marginTop to (viewport-mainImage)/2;
set marginBottom to (viewport-mainImage/2;"
并且通过“next/prev”按钮加载每个图像。
可能的重复http://stackoverflow.com/questions/11284637/center-an-image-inside-a-div –
不,图像尺寸是未知的(我希望最大可能),并且因为它是动态的(最大宽度:100%等),其基于浏览器/视口/设备等而变化 –
同样在上述问题中。 –