如何使用CSS创建和管理固定背景?

4

我想创建一个背景效果,看起来像这样:http://goo.gl/ZVIdvp,只使用纯CSS。

我还想知道如何使它具有响应性。

到目前为止,这是我在HTML上拥有的:

    <header class="header">
        <h1>Fixed Background Effect</h1>
    </header>

    <section class="background-fixed img-1">
        <div class="main-content">
            <h2>Title here</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
        </div>
    </section>

    <section class="background-fixed img-2">
        <div class="main-content light-background">
            <h2>Title here</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
        </div>
    </section>

    <section class="background-fixed img-3">
        <div class="main-content">
            <h2>Title here</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
        </div>
    </section>

    <section class="background-fixed img-4" data-type="slider-item">
        <div class="main-content">
            <h2>Title here</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
        </div>
    </section>


<footer>
    <p>&copy; Copyright 2015 </p>
</footer>

这是我的CSS代码:

/* -------------------------------- 

Primary style

-------------------------------- */
*, *::after, *::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

html {
  font-size: 62.5%;
}

body {
  font-size: 1.6rem;
  font-family: "Open Sans", sans-serif;
  color: #0f594d;
  background-color: #f2e6cd;
}

a {
  color: #93a748;
  text-decoration: none;
}

/* -------------------------------- 

Main Components 

-------------------------------- */
.header {
  position: relative;
  height: 100%;
  background-color: #34495e;
  z-index: 1;
}
.header h1 {
  width: 90%;
  color: #f2e6cd;
  text-align: center;
  font-size: 2.2rem;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  position: absolute;
  left: 50%;
  top: 50%;
  bottom: auto;
  right: auto;
  -webkit-transform: translateX(-50%) translateY(-50%);
  -moz-transform: translateX(-50%) translateY(-50%);
  -ms-transform: translateX(-50%) translateY(-50%);
  -o-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
}
@media only screen and (min-width: 768px) {
  .cd-header h1 {
    font-size: 3.6rem;
    font-weight: 300;
  }
}

.background-fixed {
  position: relative;
  padding: 3em 5% 0;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center center;
}
.background-fixed h2, .background-fixed p {
  color: #f2e6cd;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.background-fixed h2 {
  font-size: 2.4rem;
  margin-bottom: 1em;
}
.background-fixedp {
  line-height: 1.6;
  font-family: "Merriweather", serif;
}
.background-fixed .light-background h2, .background-fixed .light-background p {
  color: #0f594d;
}
.background-fixed .main-content::after {
  /* phone image on small devices */
  content: '';
  display: block;
  width: 100%;
  padding: 60% 0;
  margin: 2em auto 0;
}
.background-fixed.img-1 {
  background-color: #bf5138;
}


.background-fixed.img-2 {
  background-color: #f2e6cd;
}

.background-fixed.img-3 {
  background-color: #0f594d;
}


.background-fixed.img-4 {
  background-color: #db9537;
}


@media only screen and (min-width: 768px) {
  .background-fixed {
    height: 100%;
    padding: 0;
  }
  .background-fixed h2 {
    font-size: 3.6rem;
    font-weight: 300;
  }
  .background-fixed p {
    font-size: 1.8rem;
    line-height: 1.8;
  }

  .background-fixed.img-1 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-1.jpg");
  }
  .background-fixed.img-2 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-2.jpg");
  }
  .background-fixed.img-3 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-3.jpg");
  }
  .background-fixed.img-4 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-4.jpg");
  }
}


footer {
  background: #dadada;
  padding: 15px 0;

}

footer p{
  color: #000;
  text-align:center;
}

有没有想过用CSS来实现呢?这是我的JSFIDDLE链接:https://jsfiddle.net/v7a58odd/1/

我猜没有jQuery就做不到。 - stanze
@stanze:那我们该如何做呢?我该如何应用jQuery? - user4918654
请不要破坏您的帖子。 - Martijn Pieters
2个回答

6
你的代码没有问题,你只是缺少了一个样式。
 .background-fixed{
         background-attachment:fixed;
    }

jsFiddle


1
图片在哪里?我看不到它们。而且,由于我们正在跨越图像每个部分,所以它无法正常工作,请检查您的代码。 - Kimberly Wright
展开结果窗口以查看图片或使用全屏jsFiddle:https://jsfiddle.net/0f89ptc3/1/embedded/result/ 如果您的代码中屏幕宽度小于768px,则不显示图像。 - Gvidas
它不具有响应性,@Gvidas,而且还存在空格,无法正常工作。必须具有灵活性。 - Kimberly Wright
并非所有必要的CSS都被复制到您的jsFiddle中,这里是完整的复制版本:https://jsfiddle.net/v8dxee30/1/,请注意,在此fiddle中,“background-attachemnt: fixed;”仅适用于宽度为1048px及以上。 - Gvidas
你从哪里得到这个类?cd-fixed-background,还有这个呢?data-type="slider-item" - user4918654
显示剩余2条评论

2

/* -------------------------------- 

Primary style

-------------------------------- */
*, *::after, *::before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

html {
  font-size: 62.5%;
}

body {
  font-size: 1.6rem;
  font-family: "Open Sans", sans-serif;
  color: #0f594d;
  background-color: #f2e6cd;
}

a {
  color: #93a748;
  text-decoration: none;
}

/* -------------------------------- 

Main Components 

-------------------------------- */
.header {
  position: relative;
  height: 100%;
  background-color: #34495e;
  z-index: 1;
}
.header h1 {
  width: 90%;
  color: #f2e6cd;
  text-align: center;
  font-size: 2.2rem;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  position: absolute;
  left: 50%;
  top: 50%;
  bottom: auto;
  right: auto;
  -webkit-transform: translateX(-50%) translateY(-50%);
  -moz-transform: translateX(-50%) translateY(-50%);
  -ms-transform: translateX(-50%) translateY(-50%);
  -o-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
}
@media only screen and (min-width: 768px) {
  .cd-header h1 {
    font-size: 3.6rem;
    font-weight: 300;
  }
}

.background-fixed {
  position: relative;
  padding: 3em 5% 0;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center center;
}
.background-fixed h2, .background-fixed p {
  color: #f2e6cd;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.background-fixed h2 {
  font-size: 2.4rem;
  margin-bottom: 1em;
}
.background-fixedp {
  line-height: 1.6;
  font-family: "Merriweather", serif;
}
.background-fixed .light-background h2, .background-fixed .light-background p {
  color: #0f594d;
}
.background-fixed .main-content::after {
  /* phone image on small devices */
  content: '';
  display: block;
  width: 100%;
  padding: 60% 0;
  margin: 2em auto 0;
}
.background-fixed.img-1 {
  background-color: #bf5138;
}


.background-fixed.img-2 {
  background-color: #f2e6cd;
}

.background-fixed.img-3 {
  background-color: #0f594d;
}


.background-fixed.img-4 {
  background-color: #db9537;
}


@media only screen and (min-width: 768px) {
  .background-fixed {
    height: 100%;
    padding: 0;
  }
  .background-fixed h2 {
    font-size: 3.6rem;
    font-weight: 300;
        margin:0;
  }
  .background-fixed p {
    font-size: 1.8rem;
    line-height: 1.8;
  }
  
  .background-fixed.img-1 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-1.jpg");
        background-attachment:fixed;
  }
  .background-fixed.img-2 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-2.jpg");
        background-attachment:fixed;
  }
  .background-fixed.img-3 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-3.jpg");
        background-attachment:fixed;
  }
  .background-fixed.img-4 {
    background-image: url("http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/img-4.jpg");
        background-attachment:fixed;
  }
}


footer {
  background: #dadada;
  padding: 15px 0;

}

footer p{
  color: #000;
  text-align:center;
}
 <header class="header">
  <h1>Fixed Background Effect</h1>
 </header>
 
 <section class="background-fixed img-1">
  <div class="main-content">
   <h2>Title here</h2>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
  </div>
 </section>

 <section class="background-fixed img-2">
  <div class="main-content light-background">
   <h2>Title here</h2>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
  </div>
 </section>

 <section class="background-fixed img-3">
  <div class="main-content">
   <h2>Title here</h2>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
  </div>
 </section>

 <section class="background-fixed img-4" data-type="slider-item">
  <div class="main-content">
   <h2>Title here</h2>
   <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem dolor beatae, laudantium eos fugiat, deserunt delectus quibusdam quae placeat, tempora ea? Nulla ducimus, magnam sunt repellendus modi, ad ipsam est.</p>
  </div>
 </section>


<footer>
 <p>&copy; Copyright 2015 </p>
</footer>

试一试。


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