【问题标题】:Fixed background with top navbar and scrolling固定背景与顶部导航栏和滚动
【发布时间】:2016-02-29 10:58:06
【问题描述】:

我正在为我姐姐的生日创建一个假公司的网站,但我在固定背景上遇到了一些问题。我知道它应该很简单,只是一个附件问题:已修复,但由于某种原因它似乎不起作用。好吧,原因显然是我,但我认为你可以帮我解决这个问题:)

这是网站:http://camilleperrin.fr/BBE/,当您必须滚动时会出现问题(如果您的分辨率为 1920x1080,则在此页面上:http://camilleperrin.fr/BBE/index.php?page=careers)。如果您有一个巨大的屏幕并且看不到问题,则背景图像不会停留在应有的位置,并随着滚动而下降。

这是我的代码(我得到了 Internet 的帮助,这一切都不是我自己想出来的):

CSS:

    body{
        background:url('background.jpg') no-repeat center 160px fixed;
    }

    #overallcontainer{
        padding: 70px 90px 120px 90px;
    }

    #blurcontainer {
        position: relative;
    }

    #blurbg{
        background:url('background.jpg') no-repeat center 160px fixed;
        text-align:center;
        position: absolute;
        top: 0px;
        right: 0px;
        bottom: 0px;
        left: 0px;
        z-index: 99;  
        width:800px;
        margin:0 auto;
        padding:60px;

      -webkit-filter: blur(5px);
    }

    #textContainer  {
        position: relative;     
        z-index: 100;
        background: rgba(0,0,0,0.65);
        margin:0 auto;
        color:#dde;
        width:800px;
        padding:60px;
        text-align:justify;
    }

HTML:

<div id="overallcontainer">
    <div id="blurcontainer">
        <div id="blurbg"></div>
        <div id="textContainer">
            blah blah blah
        </div>
    </div>
</div>

如果您知道如何在保留模糊文本容器的同时解决此问题,那将非常有帮助。

谢谢!

卡米尔

【问题讨论】:

  • 能否指定您的环境,因为在我安装的浏览器(装有 firefox、ie 和 opera 的 PC)上一切正常?
  • @silviagreen,两个背景都固定在我共享的代码版本上。我刚刚用 Aziz 的答案更新了页面,所以现在两张图片都不匹配了。
  • @scraaappy,我在 PC、Windows 7、Chrome 上,但我尝试使用 Firefox 并遇到同样的问题:滚动时背景顶部出现一条白带(再次,我已经根据 Aziz 的建议进行了更新,所以现在看起来不像 :))

标签: html css scroll fixed background-attachment


【解决方案1】:

问题是由 background-position 的 160px 顶部偏移引起的。我假设您这样做是为了防止背景干扰您的标题。但是,fixed 位置会保留偏移量,因为它有点“卡在”视口中。我建议从 body 中删除背景并将其直接应用到您的主要内容容器 #overallcontainer 以解决此问题:

#overallcontainer {
    padding: 70px 90px 120px 90px;
    background: url('../design/careers.jpg') no-repeat top center fixed;
    background-size: cover; /* makes sure image covers entire viewport */
}

编辑 - 另一种方法

正如 cmets 中所讨论的,前面的方法可能不会跨越整个视口,因为#overallcontainer 具有基于内容的动态高度,并且可能小于实际视口高度,从而创建空白空白。

这可以通过将背景恢复为body 来缓解,然后通过向标题添加纯白色背景来创建隐藏正文背景的特殊标题元素。

改变

<img src="design/header.jpg">

<header><img src="design/header.jpg"></header>

CSS

body { 
   ...
    background: url('../design/company.jpg') no-repeat top center fixed;
    background-size: cover;
}

header {background:#FFF;}

#overallcontainer { /* no need for any styles to this div any more */ }

【讨论】:

  • 嗨,阿齐兹,感谢您的回答。实际上我一开始就是这样做的,最后把背景放在body 中,因为否则它会在块停止的地方停止,块的长度取决于我的文本长度(camilleperrin.fr/BBE/index.php?page=home)。编辑:height:100%; 我认为占据了整个屏幕的大小,所以即使文本不需要,我也会得到一个 1080 像素的高度和一个滚动条。
  • 我明白了。然后,您可以添加 min-height: calc(100vh - 353px); 更改 350px 以匹配标题的高度。这有点hacky,可能没有最好的支持......嗯,也许只是把它带回正文并将你的标题图像包装在一个具有白色背景的div中以隐藏顶部
  • 这似乎有效,太棒了,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 2017-12-20
  • 2023-03-21
  • 2017-07-17
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
相关资源
最近更新 更多