简体   繁体   English

如何由于背景尺寸(封面)而停止放大背景图像?

[英]How do I stop background-image from zooming in because of background-size: cover?

I'm creating a website, using chrome to debug. 我正在创建一个网站,使用chrome进行调试。 I am creating a scrolling page with full height images as background images for my divisions, using "background-size: cover;" 我正在使用“ background-size:cover;”创建一个滚动页面,其中包含全高图像作为我的部门的背景图像。 and "background-attachment: fixed;". 和“背景附件:固定;”。

On chrome's (mobile) debug viewer on my pc, even though there is more scrolling text then could fit over the image, the image stays the same size, and the content just scrolls over an image that is not zoomed in or distorted. 在我PC上的chrome(移动)调试查看器上,即使有更多的滚动文本可以容纳在图像上,图像仍保持相同大小,并且内容只是在没有放大或变形的图像上滚动。

After uploading to s3 and viewing on my mobile device, the background-image for the div with a lot of content zooms the photo in (assuming it does this to continue coverage), to a point which it does not look good. 上传到s3并在我的移动设备上查看后,具有很多内容的div的背景图像将照片放大了(假设这样做是为了继续覆盖),使其看起来不太好。

So far I have tried changing the background-attachment of the content wrapper to fixed, and then scroll just to see if it would work; 到目前为止,我尝试将内容包装程序的背景附件更改为固定,然后滚动查看是否可行。 it did not. 它没。 The only thing that has allowed the background image to NOT zoom in, was to change the background-size from cover, to 100% 80%;. 唯一不允许背景图像放大的是将背景大小从封面更改为100%80%;。 This kept the original zoom, but obviously did not cover all of the text, causing there to be a gap between one full size image div, and the next. 这样可以保持原始缩放,但显然不能覆盖所有文本,从而导致一个全尺寸图像div和下一个全尺寸图像div之间存在间隙。

`    .second-page{
    background-image: linear-gradient(rgba(77, 77, 77, 0.5), rgba(77, 77, 77, 0.5)), url(images/background6_port.jpg);
    background-repeat: no-repeat;
    min-height: 100vh;
    min-width: 100vw;
    background-size: cover;
    position: relative;
    background-attachment: fixed;


}
.content{
    margin-right: 15%;
    margin-left: 15%;
}`

Expected Results: background-image covers 100% of viewport on mobile, allowing text and other content to scroll over it without zooming in 预期结果:背景图像覆盖了移动设备上的视口的100%,允许文本和其他内容在其上滚动而无需放大

Actual Results: background-image zooms in to cover all of content area, causing pixelation and bad visual experience. 实际结果:背景图像放大以覆盖所有内容区域,从而导致像素化和不良的视觉体验。

As most times I post to this site, as soon as I post my question I come up with the answer. 在大多数情况下,我会在此站点上发布问题,而在我发布问题后,我就会给出答案。 For anyone in a similar situation I fixed this by adding 对于处于类似情况的任何人,我通过添加

max-height: 100vh;
overflow-y: scroll;

Hope this helps someone else! 希望这对别人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM