Bootstrap 3 - jumbotron 背景图像效果

54

嗨,我正在尝试使用带有背景图像的 jumbotron 构建网站,这本身并不难:

HTML:

<div class="jumbotron">
...
</div>

CSS: (它位于我的自定义CSS文件中,加载在所有其他CSS之后)。

.jumbotron {
    margin-bottom: 0px;
    background-image: url(../img/jumbotronbackground.jpg);
    background-position: 0% 25%;
    background-size: cover;
    background-repeat: no-repeat;
    color: white;
    text-shadow: black 0.3em 0.3em 0.3em;
}
现在这会创建一个带有背景图片的jumbotron,但我想要它做一个效果,我很难描述,但在这个网页上看到: http://www.andrewmunsell.com。您可以看到当您滚动时,jumbotron内容文本等会比背景图像快速向上滚动。这种效果叫什么名字,使用bootstrap/html/css容易实现吗?
我已经查看了页面的HTML,但目前对我来说有点太复杂了。
谢谢, Ben.
编辑:我尝试通过第一个答案提供的示例获得效果,该示例位于boot ply。
然而,对我来说,背景图像出现了,但只要我稍微滚动一下,整个图像就会消失。如果我使用safari的惯性滚动尝试向页面顶部滚动,背景会再次尝试向下移动进入视图,所以我认为图像被正确加载,但只要我稍微滚动一下,高度就被操纵,以使图像完全移动到屏幕外。以下是我的HTML(在制表符放置位置方面有些丑陋,但Jekyll通过包括我正在尝试为其制作视差效果的Jumbotron标题、页面内容和页面页脚将其组合在一起。)。
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Hybridisation Recombination and Introgression Detection and Dating</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Site for the HybRIDS R package for analysing recombination signal in DNA sequences">
    <link href="/css/bootstrap.css" rel="stylesheet">
    <link href="/css/bootstrap-responsive.css" rel="stylesheet">
    <link rel="stylesheet" href="css/custom.css" type="text/css"/>
    <style>
    </style>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type='text/javascript'>
        var jumboHeight = $('.jumbotron').outerHeight();
        function parallax(){
            var scrolled = $(window).scrollTop();
            $('.bg').css('height', (jumboHeight-scrolled) + 'px');
        }
        $(window).scroll(function(e){
            parallax();
        });
    </script>
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></sc\
ript>
    <![endif]-->
  </head>

  <body>
    <div class="bg"></div>
    <div class="jumbotron">
        <div class="row">
            <div class="col-lg-4">
                <img src="./img/HybRIDSlogo.png" class="img-rounded">
            </div>
            <div class="col-lg-8">
                <div class="page-header">
                    <h2> Hybridisation Recombination and Introgression Detection and Dating </h2>
                    <p> <h2> <small> Easy detection, dating, and visualisation for recombination, introgression and hybridisation events in genomes. </small> </h2> </p>
                </div>
            </div>
        </div>
    </div>
    <!-- End of JumboTron -->

    <div class="container">

        PAGE CONTENT HERE

    <!-- Close the container that is opened up in header.html -->
    </div>
    </body>
</html>

你看到我在页面顶部包含了Javascript代码以及class为bg的div和jumbotron。

我的CSS如下:

body {
    background-color: #333333;
    padding-bottom: 100px;
}

.bg {
  background: url('../img/carouelbackground.jpg') no-repeat center center;
  position: fixed;
  width: 100%;
  height: 350px; 
  top:0;
  left:0;
  z-index: -1;
}

.jumbotron {
    margin-bottom: 0px;
    height: 350px;
    color: white;
    text-shadow: black 0.3em 0.3em 0.3em;
    background: transparent;
}

2
它被称为视差滚动。谷歌博士知道许多例子和教程。 - Stephen Thomas
@StephenThomas 是正确的,这被称为视差效果。网络上有大量资源可帮助您入门。 - robabby
3个回答

63

示例: http://bootply.com/103783

一种实现方法是使用一个position:fixed的容器作为背景图片并将其放置在.jumbotron之外。使bg容器与.jumbotron相同高度,并将背景图居中:

background: url('/assets/example/...jpg') no-repeat center center;

CSS

:层叠样式表。

.bg {
  background: url('/assets/example/bg_blueplane.jpg') no-repeat center center;
  position: fixed;
  width: 100%;
  height: 350px; /*same height as jumbotron */
  top:0;
  left:0;
  z-index: -1;
}

.jumbotron {
  margin-bottom: 0px;
  height: 350px;
  color: white;
  text-shadow: black 0.3em 0.3em 0.3em;
  background:transparent;
}

然后使用jQuery技术,在窗口滚动时减少.jumbtron的高度。由于背景图像居中在DIV中,它会相应地调整大小--创建一种视差效果。

JavaScript

var jumboHeight = $('.jumbotron').outerHeight();
function parallax(){
    var scrolled = $(window).scrollTop();
    $('.bg').css('height', (jumboHeight-scrolled) + 'px');
}

$(window).scroll(function(e){
    parallax();
});

Demo

http://bootply.com/103783


1
这对我不起作用,我会更新问题并说明我如何尝试复制示例。 - SJWard
2
这是另一个例子:http://bootply.com/103820(使用简单的视差插件) - Carol Skelly
有没有办法动态添加背景图片链接? - CanCeylan
1
这是一个很好的答案 - 它简单而且运行得非常好。在我的情况下,我不得不将70px添加到视差函数中,可能是因为边距/填充/导航栏没有被考虑在内,但这是一个简单的修复。 - Stephen Smith

6

在检查您提供的示例网站后,我发现作者可能是通过使用一个名为Stellar.js的库来实现效果的,请查看该库的网站,谢谢!


1
我觉得你想要的是保持背景图片固定,只在滚动时移动内容。为此,您只需使用以下CSS属性:

background-attachment: fixed;


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