Jquery-使用父高度进行垂直居中。某种程度上
因此,我有X个图像要垂直居中于aJquery-使用父高度进行垂直居中。某种程度上,jquery,css,image,center,Jquery,Css,Image,Center,因此,我有X个图像要垂直居中于a(下面jquery中的类名是实际的)。 这很有效,但是。。。它仅获取第一个值并将其应用于其余图像,导致第一个图像居中,而其他图像不居中 我可以补充一点,所有的图像都有不同的高度和宽度。从现在开始,只需使用text align:center $(window).load(function(){ var parent_height = $('.tv-list-channel-logo').parent().height(); var image_hei
(下面jquery中的类名是实际的)。
这很有效,但是。。。它仅获取第一个值并将其应用于其余图像,导致第一个图像居中,而其他图像不居中
我可以补充一点,所有的图像都有不同的高度和宽度。从现在开始,只需使用text align:center
$(window).load(function(){
var parent_height = $('.tv-list-channel-logo').parent().height();
var image_height = $('.tv-list-channel-logo').height();
var top_margin = (parent_height - image_height)/2;
$('.tv-list-channel-logo').css( 'margin-top' , top_margin);
});
我已经盯着它看了很长一段时间了,我肯定错过了一些明显的东西。如果图像需要不同的值,你需要在它们之间循环。尝试使用$('.tv list channel logo')。每个(函数…
)。在内部。每个,此引用当前元素
编辑:这里有一个的示例。每个:
// `.each` takes each selected element and calls the callback on each one
$('.tv-list-channel-logo').each( function( ) {
// `this` refers to the current .tv-list-channel-logo element
var parent_height = $(this).parent().height();
var image_height = $(this).height();
var top_margin = (parent_height - image_height)/2;
$(this).css( 'margin-top' , top_margin);
// I just replaced all instances of `'.tv-list-channel-logo'` with `this`
} );
你试过把它们完全放在一个相对的容器里吗?是的,我试过了,但我应该在我的第一篇文章中提到,这些图像的高度和宽度都不同。请使用我的建议以及下面的答案Ryan P
。循环浏览每个图像并使用每个执行此计算我不太确定这是否是您的意思,但它不起作用<代码>var image_height=$('.tv list channel logo')。each().height()代码>我的脑子里肯定有一些有价值的东西。我搞不懂这个…我今天早上在电话里接的。。。现在在工作,所以我给你打了个例子。