div 背景图像位置固定的容器

div container with background-image position fixed

我想创建这样的页面:Page

我不想将图像作为背景。我想创建一个 div 容器,这个 div 容器有一个背景图像。在这个 div 下应该是另一个 div。如果我将 div 滚动到带有背景图像的 div 下方,应该会覆盖带有背景图像的 div。我有一些东西,但它是废话。 这是我的主页:1

这是代码:

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
    font-size: 18px;
}
html {
    height: 100%;
}
body{
    height: 100%;
    background-image: url('http://cdn1.editmysite.com/uploads/3/8/9/4/38945355/background-images/748028443.png') !important;
    background-repeat: no-repeat !important;
    background-position: 50% 0 !important;
    background-size: auto !important;
    background-color: transparent !important;
    background: inherit;
}

nav{
   background: url("images/line-header.png") repeat-x scroll center bottom #4A525A;
   padding: 15px;
   position: fixed;
   top: 0px;
   width: 100%;
}

nav > ul{
    margin: 0px;
    padding: 0px;
    text-align: center;
}

nav ul > li{
    margin-left: 25px;
    display: inline-block;
    list-style-type: none;
}

nav ul li > a{
    display: block;
    text-decoration: none;
    color: black;
    color: #697683;
    transition: color 0.5s;
}

nav ul li > a:hover{
    color: #FFF;
}

.content{
    height: 1000px;
    margin-top: 500px;
    background-color: orange;
}
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="index.css" >
        <!-- Open Sans -->
        <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    </head>
    <body>

            <nav>
                <ul>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                </ul>
            </nav>

            <div class="content">
                <h1></h1>
            </div>

    </body>
</html>

只需添加另一个 div 并将 background-attachment 设置为 fixed

.background{
  background: url('http://cdn1.editmysite.com/uploads/3/8/9/4/38945355/background-images/748028443.png') no-repeat;
  background-attachment: fixed;
  background-size: cover;
  height: 500px; <------can make 500px instead of using margin-top: 500px on .content
}


.content{
  height: 1000px;
  background-color: orange;
  /*margin-top: 500px;*/ <-------------take out
}

FIDDLE

您可以试试这个,添加 700 像素的图像高度和背景附件:固定;

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
    font-size: 18px;
}
html {
    height: 100%;
}
body{
    height: 700px;
    background-image:url('http://cdn1.editmysite.com/uploads/3/8/9/4/38945355/background-        images/748028443.png') !important;
    background-repeat: no-repeat !important;
    background-position: 50% 0 !important;
    background-size: auto !important;
    background-color: transparent !important;
    background-attachment: fixed;
}

nav{
   background: url("images/line-header.png") repeat-x scroll center bottom #4A525A;
   padding: 15px;
   position: fixed;
   top: 0px;
   width: 100%;
}

nav > ul{
    margin: 0px;
    padding: 0px;
    text-align: center;
}

nav ul > li{
    margin-left: 25px;
    display: inline-block;
    list-style-type: none;
}

nav ul li > a{
    display: block;
    text-decoration: none;
    color: black;
    color: #697683;
    transition: color 0.5s;
}

nav ul li > a:hover{
    color: #FFF;
}

.content{
    height: 1000px;
    margin-top: 500px;
    background-color: orange;
}


<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="index.css" >
        <!-- Open Sans -->
        <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    </head>
    <body>

            <nav>
                <ul>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                </ul>
            </nav>

            <div class="content">
                <h1></h1>
            </div>

    </body>
</html>