div图像位置固定+滚动

时间:2014-12-31 12:59:48

标签: html css

我想创建类似的内容:Page

我的意思是第一张图片。如果我在img下滚动div来覆盖图像。 我该如何创造呢?如果我给我的img这个位置:固定,一切都被毁坏了^^ 位置absoulte可能是有用的,但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>

2 个答案:

答案 0 :(得分:2)

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

你几乎就在那里,只需要给导航员一个固定的位置。

&#13;
&#13;
* {
  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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您必须按如下方式设置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'。这就是解决方案。

额外的东西:

在这里,我给了'margin-top:792px'到div'.text'。但是你可以使用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标签之前,应在标记内添加