简体   繁体   English

固定背景图像水平连接到固定宽度的内容

[英]Fixed background image horizontally connected to content with fixed width

The problem is: I have a huge background image and content with those characteristics: 问题是:我有一个巨大的背景图像和具有这些特征的内容:

  • the content is centered with margin: auto; 内容以margin: auto;为中心margin: auto; and it has a fixed width 它有一个固定的宽度
  • the position of the content is in relation to the image (like it fits in the middle of the image) 内容的位置与图像有关(就像它适合图像的中间)
  • this connection is only horizontally (vertical scrolling moves everything around as expected) 此连接仅水平连接(垂直滚动按预期移动所有内容)

This works fine, actually, on desktop devices with position fixed on the background image. 实际上,这在桌面设备上工作正常,位置固定在背景图像上。

But the problem is: When I resize the window until it's smaller than the content, the content is fixed on the left side, but the background image is still centered, as expected. 但问题是:当我调整窗口大小直到它小于内容时,内容固定在左侧,但背景图像仍然按照预期居中。 In this case the connection between both elements gets lost. 在这种情况下,两个元素之间的连接会丢失。

I have this JavaScript that does the trick, but this is of course some overhead I want to avoid as it isn't smooth anytime due to the calculation: 我有这个诀窍的JavaScript,但这当然是一些我想要避免的开销,因为它由于计算而在任何时候都不平滑:

$(window).resize(function(){
    container.css('left', (body.width() - img.width()) / 2);
});

I also tried things like that: 我也尝试过这样的事情:

<div id="test" style="
    position: absolute;
    z-index: 0;
    top: 0;
    left: 0;
    width: 100%:
    height: 100%;
    background: transparent url(path) no-repeat fixed center top;
"></div>

But this results in the same issue described above. 但是这导致了上述相同的问题。

Is there any elegant CSS solution for this problem? 这个问题有没有优雅的CSS解决方案?

Demo 演示

Try it yourself 亲自尝试一下

NOTE 注意

The image size is fixed and known and it never gets scaled by the browser. 图像大小是固定的并且是已知的,并且它永远不会被浏览器缩放。

Is this working for you? 这适合你吗? http://jsfiddle.net/wPmrm/24/ http://jsfiddle.net/wPmrm/24/

HTML HTML

<div class="background">
<div class="content">
    CONTENT
    <br><br>
    This example works fine until you the viewport size gets smaller than this content. After that the image isn't sticky anymore.
    <br><br>
    And check out vertical scrolling.

    <div style="height:1500px;"></div>
    END
</div>
</div>

CSS CSS

div.background {
    min-width: 740px;
    background: url('http://placehold.it/1600x1050') top center fixed no-repeat;
}

div.content {
    width: 700px;
    height: 2000px;
    margin: auto;
    padding: 50px 20px;
    background: none;
    opacity: 0.7;
    color: #333;
}

.background should be the wrapper for .content with a centered background and have a minimum-width of the .contents width+padding. .background应该是.content的包装器,具有居中的背景,并且具有.contents width + padding的最小宽度。

Update from comments: 来自评论的更新:

http://jsfiddle.net/wPmrm/28/ http://jsfiddle.net/wPmrm/28/

We'll have to use a media-query, so when the width is at max 740px we change the background position. 我们必须使用媒体查询,所以当宽度达到最大740px时,我们会改变背景位置。 Oh and we set background-attachment to fixed again. 哦,我们再次将背景附件设置为固定。

CSS added CSS补充说

@media screen and (max-width:740px) {
    div.background {
        background-position: -435px 0;
    }
}

I don't see why it is -435px ((1600-740)/2 would be 430) but it seems to be the most accurate value. 我不明白为什么它是-435px((1600-740)/ 2将是430)但它似乎是最准确的值。

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

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