2017-03-07 255 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