固定位置div图片 + 滚动

3
我想要创建类似这样的东西:Page 我指的是第一张图片。如果我滚动,div会覆盖在图片上方。 如何创建这个效果?如果我给我的图片 position: fixed,一切都被破坏了^^ 而且position: absolute也许有用,但是div就在导航栏旁边。 这是我的尝试:enter link description here 以下是代码:

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
    font-size: 18px;
}
html {
    height: 100%;
}
body{
    /*background: url("images/bg.png") repeat-x scroll left top #9EB5D6;
    background: url("images/bg2.png"); */
    height: 100%;
}

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

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;
}

.pic1 {
    background: url(images/banner.jpg);
    height: 100%;
    width: 100%;
}

.text{
    height: 800px;
    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="pic1">
            </div>
            <div class="text">
            </div>

    </body>
</html>


1
信不信由你,这就是 position:fixedz-index:999 的魔力。 - Vedant Terkar
2个回答

2
你需要按照以下方式设置CSS规则:
.pic1 {
    background-attachment: fixed;
    background-position: center center;
    background-repeat: no-repeat;
    background-size: cover;
    position: fixed;
    top: 0;
    width: 100%;
    z-index: -1;
}

.text{
    margin-top: 792px;
}

这将使div '.pic1'固定,div '.text'可以滚动到div '.pic1'上方。这就是解决方案。
一些额外的内容:
我在div '.text'中使用了'margin-top:792px'。但是,您可以使用jQuery检测视口高度,并将该值分配为div '.text'的'margin-top',这将使其始终在所有屏幕上正确显示在折叠下方。
以下是如何通过jQuery实现:
$(document).ready(function(){

var wH = $(window).height();
$('.text').css('margin-top',wH);

})

在这种情况下,您需要删除以下CSS规则:
 .text{
        margin-top: 792px;
    }

请注意,在您的网页中最初必须链接jQuery库。整个jQuery代码应该像下面展示的那样:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
 $(document).ready(function(){

    var wH = $(window).height();
    $('.text').css('margin-top',wH);

    })

</script>

应该在标记内添加,就在关闭BODY标记之前。

谢谢你的回答,非常完美,还感谢你额外提供的帮助! :) - Skeptar
嗨Skeptar,谢谢你,很高兴它对你有用 :) - Jean P. Johnson

2

这是关于选择正确的方法。这种滚动方式可以通过固定菜单(在顶部),下推的div和背景图像轻松完成。我添加了一个漂亮的可重复使用的猫图像来展示效果。

你已经接近成功,只需要给导航条一个固定的位置。

* {
  padding: 0;
  margin: 0;
}
body {
  background: url(https://c1.staticflickr.com/9/8226/8557105873_a82c51f03f_z.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
nav {
  background-color: #555555;
  text-align: center;
  height: 20px;
  position: fixed;
  top: 0px;
  width: 100%;
}
nav li {
  display: inline;
  list-style-type: none;
  padding-right: 20px;
}
nav li a {
  color: white;
  text-decoration: none;
}
.text {
  background-color: orange;
  margin-top: 500px;
  min-height: 1000px;
}
<html>

<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="text">
    text
  </div>

</body>

</html>


谢谢你的回答,非常完美。我给你点了个赞,因为你更快 :) 谢谢! - Skeptar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接