我正在尝试找出使背景图像完全响应的最佳方法 - 到目前为止,我能想到的最佳方法是使用 background-size: cover,正如大多数人倾向于建议的那样,但是 with background-attachment: fixed 以便图像在屏幕调整大小时按比例缩小,否则它只是保留其原始比例而根本不缩放。仅使用 background-size: cover 拉伸(stretch)图像以填充容器 div,但不会自动缩放比例。
但是我不希望固定背景的效果在您向下滚动时隐藏部分图像,我更希望它是背景附件:滚动,但我无法让它工作 和 也使其具有可扩展性。
所以我的问题是:有什么我不知道的方法可以让背景图像随屏幕尺寸自动缩放,而无需使用 background-attachment: fixed 来实现吗?
请查看我的 JSFiddle,了解我目前拥有的内容:https://jsfiddle.net/uhoL5d5w/2/
(是的,我也知道在某些时候我需要使用媒体查询来为各种屏幕尺寸提供优化的图像)
我当前的代码如下:
<header>
<div class="launch-bg">
<nav class="menu">
</nav>
</div>
</header>
<div class="page-wrapper">
</div>
<div class="push"></div>
<!-- Footer -->
<div class="footer"></div>
html,
body {
@include box-sizing(border-box);
height: 100%;
}
div,
body {
margin: 0;
padding: 0;
}
body {
display: block;
background-color: #000000;
}
.page-wrapper {
margin: 0 auto -900;
min-height: 100%;
height: auto;
}
* {
margin: 0;
padding: 0;
}
header {
display: block;
width: 100%;
height: 1200px;
}
.launch-bg {
height: 1200px;
background-image: url('http://s8.postimg.org/56xlj2rc5/launch_bg.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center center;
background-size: cover;
}
.footer {
height: 900px;
padding: 6% 0;
color: $white;
background-image: url('http://s8.postimg.org/onib5lmg5/footer_bg.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center center;
background-size: cover;
}
最佳答案
我认为这是一个简单的示例,说明您的要求,为了清楚起见,将其全部删减:
header {
display: block;
width: 100%;
height: 1200px;
background-image: url('http://s8.postimg.org/56xlj2rc5/launch_bg.jpg');
background-repeat: no-repeat;
background-attachment: scroll;
background-position: center center;
background-size: cover;
}
关于html - 在没有背景附件的情况下缩放背景图像 : fixed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30879457/