向下滚动到新背景图像时CSS间距问题和滞后

时间:2016-07-01 16:42:08

标签: html css background-image parallax

我目前正在使用CSS background-attachment属性构建一个主页来创建滚动背景效果。

我有多个背景,每个背景之间有一个内容部分。在第一个内容部分之后,在下一个背景图像之前有一个很小的小空格,我不知道如何摆脱那个空间。

SS

我遇到的另一个问题是页面的实际性能。

当我滚动到新图像时,实际显示图像之前会有大约一秒或两秒的延迟。换句话说,当尝试滚动到新的background时,屏幕实际上会冻结几秒钟。

我认为这根本不重要,但我将其构建为ASP.NET MVC Web应用程序。

这是我的HTML:

<body>   
        <main class="cd-main-content">
            <div class="cd-fixed-bg cd-bg-1">
                <div class="cd-intro">

                </div>
            </div> <!-- cd-fixed-bg -->

            <div class="cd-scrolling-bg cd-color-2">            
                <div class="cd-container">
                    <p>
                        Lorem ipsum dolor sit amet, consectetur adipisicing  elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur 
                    </p>
                </div> <!-- cd-container -->
            </div> <!-- cd-scrolling-bg -->

            <div class="cd-fixed-bg cd-bg-2">
                <h2>Lorem ipsum dolor sit amet.</h2>
            </div> <!-- cd-fixed-bg -->

            <div class="cd-scrolling-bg cd-color-3">
                <div class="cd-container">
                    <p>
                        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur autem laborum fuga quas ipsam doloribus sequi, mollitia, repellendus sapiente 

                    </p>
                </div> <!-- cd-container -->
            </div> <!-- cd-scrolling-bg -->

            <div class="cd-fixed-bg cd-bg-3">
                <div class="cd-container">
                    <p>


                    </p>
                </div>
            </div> <!-- cd-fixed-bg -->


        </main> <!-- cd-main-content -->

    </body>

这是我的CSS:

    body, html, main {
    /* important */
    height: 100%;
}

.cd-fixed-bg {
    min-height: 100%;
    background-size: cover;
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-position: center center;
}

.cd-fixed-bg.cd-bg-1 {
  background-image: url('../Images/portfolio1.jpg');
}
.cd-fixed-bg.cd-bg-2 {
  background-image: url('../Images/portfolio2.jpg');
}
.cd-fixed-bg.cd-bg-3 {
  background-image: url('../Images/portfolio3.jpg');
}
.cd-scrolling-bg {
    position:relative;
    min-height: 70%;
    padding: 4em 0;
    line-height: 2.7;
    z-index: 2;
    top: 0%;
    bottom: 0%;
    left: 0%;
}
.cd-intro {
    position: absolute;
    left: 50%;
    top: 50%;
    background-color: inherit;
    z-index: 100;
    text-align:center;
    height: 400px;
    margin-top: -200px;
    color: white;
    width: 600px;
    margin-left: -300px;
    font-size: 45px;
    font-family:'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
.cd-container
{
    width: 90%;
    max-width: 768px;
    margin: 0px auto;
}
.cd-scrolling-bg.cd-color-3 {
    background-color: #b4d7a8;
    color: #3d3536;
}
.cd-scrolling-bg.cd-color-2 {
    background-color: #b4d7a8;
    color: #3d3536;
}
.cd-scrolling-bg.cd-color-3 {
    background-color: #b4d7a8;
    color: #3d3536;
}

1 个答案:

答案 0 :(得分:0)

空白区域由ph2创建。

在HTML中,段落和标题默认为margin,因此请重置全部或仅重置您需要的那个。

由于创建了水平滚动条,我从您的第一个left:50%中删除了cd-intro

*,
*::before,
*::after {
  box-sizing: border-box
}
body,
html,
main {
  /* important */
  height: 100%;
  margin: 0
}
.cd-fixed-bg {
  min-height: 100%;
  background-size: cover;
  background-attachment: fixed;
  background-repeat: no-repeat;
  background-position: center center;
}
.cd-fixed-bg.cd-bg-1 {
  background-image: url('//lorempixel.com/900/900/sports');
}
.cd-fixed-bg.cd-bg-2 {
  background-image: url('//lorempixel.com/900/900/animals');
}
.cd-fixed-bg.cd-bg-3 {
  background-image: url('//lorempixel.com/900/900/city');
}
.cd-scrolling-bg {
  position: relative;
  min-height: 70%;
  padding: 4em 0;
  line-height: 2.7;
  z-index: 2;
  top: 0%;
  bottom: 0%;
  left: 0%;
}
.cd-intro {
  position: absolute;
  top: 50%;
  background-color: inherit;
  z-index: 100;
  text-align: center;
  height: 400px;
  color: white;
  width: 600px;
  font-size: 45px;
  font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
.cd-container {
  width: 90%;
  max-width: 768px;
  margin: 0px auto;
}
.cd-scrolling-bg.cd-color-3 {
  background-color: #b4d7a8;
  color: #3d3536;
}
.cd-scrolling-bg.cd-color-2 {
  background-color: #b4d7a8;
  color: #3d3536;
}
.cd-scrolling-bg.cd-color-3 {
  background-color: #b4d7a8;
  color: #3d3536;
}
h2, p {
  margin: 0
}
<main class="cd-main-content">
  <div class="cd-fixed-bg cd-bg-1">
    <div class="cd-intro">
      some text
    </div>
  </div>
  <!-- cd-fixed-bg -->

  <div class="cd-scrolling-bg cd-color-2">
    <div class="cd-container">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur
      </p>
    </div>
    <!-- cd-container -->
  </div>
  <!-- cd-scrolling-bg -->

  <div class="cd-fixed-bg cd-bg-2">
    <h2>Lorem ipsum dolor sit amet.</h2>
  </div>
  <!-- cd-fixed-bg -->

  <div class="cd-scrolling-bg cd-color-3">
    <div class="cd-container">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore incidunt suscipit similique, dolor corrupti cumque qui consectetur autem laborum fuga quas ipsam doloribus sequi, mollitia, repellendus sapiente

      </p>
    </div>
    <!-- cd-container -->
  </div>
  <!-- cd-scrolling-bg -->

  <div class="cd-fixed-bg cd-bg-3">
    <div class="cd-container">
      <p>


      </p>
    </div>
  </div>
  <!-- cd-fixed-bg -->


</main>
<!-- cd-main-content -->