2017-03-07 434 views
2

我在github頁面上有website,它可以在任何桌面瀏覽器上正常工作。但是,我的兩個背景圖像不會顯示在移動設備上(我只測試過iPad和iPhone,它可能只是IOS)。我試圖添加媒體查詢,以確保手持設備上的background-attachment屬性設置爲scroll(我讀過這有時是問題)。我也有媒體查詢,以確保圖像不會太大,無法加載。這裏是我的html:背景圖像不顯示/延伸在iPad或iPhone上

<div id="image-1" class="background-image"></div> 
<div id="image-2" class="background-image"></div> 

這裏的CSS:

#image-1 { 
    background-image: url('imgs/coding.jpg'); 
} 
#image-2 { 
    background-image: url("imgs/game.JPG"); 
} 
@media only screen and (min-width: 500px) { 
    /* For mobile phones: */ 
    #image-1 { 
     background-image: url("imgs/coding-large.jpg"); 
    } 
    #image-2 { 
     background-image: url("imgs/game-large.jpg"); 
    } 
} 
@media not handheld { 
    .background-image { 
     background-attachment: fixed; 
    } 
} 
.background-image { 
    opacity: 0.8; 
    background-repeat: no-repeat; 
    background-attachment: scroll; 
    background-size: 100% 100vh; 
    height: 85vh; 
} 

如果我改變100vh至100%,然後將圖像加載,但他們可怕的垂直拉伸。有什麼建議麼?

回答

2

您可以:

0

使用此

/* Image is centered vertically and horizontally at all times */ 
background-position: center center; 

/* Image doesn't repeat */ 
background-repeat: no-repeat; 

/* Makes the image fixed in the viewport so that it doesn't move when 
the content height is greater than the image height */ 
background-attachment: fixed; 

/* This is what makes the background image rescale based on its  container's size */ 
background-size: cover; 
2

的iPhone似乎無視handheld設備(見這裏Do iPhone/Android browsers support CSS @media handheld? @media規則。如果圖像不是正方形,則100% 100%background-size屬性會使圖像拉伸。

因此,您可以使用max-width媒體查詢來檢測移動設備,並將background-attachment設置爲scroll。並使用background-size: cover或背景大小:100% auto