簡體   English   中英

內聯圖像居中<ul>

[英]Centering images inside inline <ul>

對於我制作的一個簡單的輪播,有一種非常簡單的方法可將圖像居中放置在每個<li>內部。

它在初始頁面加載時效果很好,但是當我點擊刷新時,圖像開始有點混亂並且什么也不居中(請參閱下面的圖像)

這是我的jQuery方法:

$ul.children('li').each(function(){
        var $pic = $(this).children('img');
        var height = $pic.height();
        $pic.css({ paddingBottom: (200 - height)/2 + 'px' });
});

初始加載視圖:

在此處輸入圖片說明

頁面刷新視圖:

在此處輸入圖片說明

不知道為什么會這樣,我只能懷疑這是由於圖像緩存造成的。 如果您想轉到實時鏈接,請在此處查看http://www.metsales.com/metropolitansales/constantcontact/td/acrobat.aspx

我在這里先向您的幫助表示感謝。

最近,在這種情況下,我正在使用translateY。 應該是這樣

li {
    width: xxx; // set the item width
    height: xxx; // set the item height
    position: relative;
}

img {
    top: 50%;
    position: absolute;
    transform: translateY(-50%); // This should make the image go in direction to the top using 50% of its height value
}

提示:您應該避免<li>中的填充。

再見!

li ■不要正確對齊。 您可以做的一件事是使ul真正變寬並使您的li漂浮在其中。 將此添加到您的CSS:

ul {
  width: 1000%;
}

li {
  float: left;
  height: 200px;
}

並刪除設置填充和邊距的js,並將其更改為:

 $ul.children('li').each(function(){
    var $pic = $(this).children('img');
    var height = $pic.outerHeight();
    $pic.css({ marginTop: (200 - height)/2 + 'px' });
 });

解決方法

 $ul.children('li').each(function(){
    var $pic = $(this).children('img');
    var height = $pic.outerHeight();
    $pic.css({ marginTop: (200 - height)/2 + 'px' });//remove this line
 });

因為您需要刪除margin-top: -100px; 在img標簽中還添加了vertical-align: middle;

carousel lipadding: 5px; 在您的情況下,您不需要頂部和底部填充,但是它是可選的,可以使hover

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM