这是html:
<div class="gallerybox">
<a href="/CustomContentRetrieve.aspx?ID=398791">
<img alt="" src="/Utilities/image.jpg" width="400" height="400" />
</a>
</div>
这是css:
.gallerybox {width:200px;height:200px;overflow:hidden;}
有谁能告诉我一种方法来对齐div.gallerybox中间(垂直)和中心(horizontaly)内的链接图像?也许使用javascript,jQuery或只是普通的CSS?
答案 0 :(得分:2)
可能类似以下内容:
<div class="gallerybox">
<a href="/CustomContentRetrieve.aspx?ID=398791">
<img id="imgUtility" class="galleryImage" alt="" src="/Utilities/ShowThumbnail.aspx?USM=1&W=400&H=400&R=1&Img={tag_image_value}" />" />
</a>
</div>
.gallerybox {
width:200px;
height:200px;
overflow:hidden;
}
<!-- Edit Again -->
<script language="javascript">
window.onload = fixImage;
function fixImage() {
var img = document.getElementById('imgUtility');
var x = img.offsetWidth;
var y = img.offsetHeight;
img.style.left = -(x / 2);
img.style.top = -(y / 2);
}
</script>
答案 1 :(得分:2)
杰克逊。我将推荐一些HTML重写,我理解这可能是不可能的,但我认为这可能是解决您问题的最有效方法。
使用编码为<img>
的数据库创建隐藏图像:
img#imgUtility {
display: none;
}
(CSS和HTML)
<img id="imgUtility" class="galleryImage" alt=""
src="/Utilities/ShowThumbnail.aspx?USM=1&W=350&H=350&
R=1&Img={tag_image_value}" />
然后在页面加载并且图片已解析为实际网址后,您可以将<img>
替换为发布的HTML中的<span>
,将隐藏标记src
设置为<span>
的背景图片,通过JavaScript使用内联style
属性:
// galleryBoxLinkTarget should be a reference to the <a> in a div.galleryBox
function imgReplacement(galleryBoxLinkTarget) {
var span = document.createElement("span");
var img = document.getElementById("imgUtility");
span.style.backgroundImage = "url('" + img.getAttribute("src") + "')";
span.className = "imgReplacement";
galleryBoxLinkTarget.appendChild(span);
}
(JavaScript和HTML)
<div class="gallerybox">
<a href="/CustomContentRetrieve.aspx?ID=398791">
<!-- <span> goes here -->
</a>
</div>
然后做一些聪明的CSS:
span.imgReplacement {
display: block;
background-position: 50% 50%;
background-repeat: no-repeat;
width: 200px;
height: 200px;
}
无论尺寸如何,都应该使图片居中,并允许您删除内联width
和height
属性。希望这对你有用!
答案 2 :(得分:2)
试试这个:
<style type="text/css">
.gallerybox {text-align:center;vertical-align:middle;height:400px;line-height:400px;}
.gallerbox img {display:inline; vertical-align:middle;}
</style>
<div class="gallerybox">
<a href="/CustomContentRetrieve.aspx?ID=398791">
<img alt="" src="/Utilities/image.jpg" />
</a>
答案 3 :(得分:1)
使用一个名为center的简单JQuery fn(用法:$(元素).center()):
jQuery.fn.center = function() {
this.css("position","absolute");
this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
this.css("bottom", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
return this;
}
$(document).ready(function(){
$(#imgUtility).center();
)};
使用HTML:
<div class="gallerybox">
<a href="/CustomContentRetrieve.aspx?ID=398791">
<img id="imgUtility" class="galleryImage" alt="" src="/Utilities/ShowThumbnail.aspx?USM=1&W=400&H=400&R=1&Img={tag_image_value}" />" />
</a>
</div>