【问题标题】:Bootstrap 3 - jumbotron background image effectBootstrap 3 - jumbotron 背景图片效果
【发布时间】:2014-01-21 18:37:14
【问题描述】:

您好,我正在尝试使用带有背景图片的超大屏幕构建一个网站,这本身并不难:

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

现在这会创建一个带有背景图像的超大屏幕,但我想让它产生一种我很难描述但在此网页上可以看到的效果:http://www.andrewmunsell.com 当您滚动 jumbotron 内容文本等时,您可以看到比背景图像更快地向上滚动。这个效果叫什么,用bootstrap/html/css容易实现吗?

我已经查看了付款的 HTML,但现在它有点太复杂了,我无法理解。

谢谢, 本。

编辑:我试图通过第一个答案提供的示例来获得效果,该示例位于引导层。

但是对我来说,背景图像出现了,然后只要滚动一点点,整个图像就会消失。如果我使用 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 和类 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;
}

【问题讨论】:

  • 称为视差滚动。 Google 博士知道许多示例和教程。
  • @StephenThomas 是对的,这就是所谓的视差。网络上有大量资源可以帮助您入门。

标签: html css twitter-bootstrap-3 webpage


【解决方案1】:

示例: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();
});

演示

http://bootply.com/103783

【讨论】:

  • 这对我不起作用,我将用我如何尝试复制示例来更新问题。
  • 这是另一个例子:bootply.com/103820(使用简单的视差插件)
  • 有没有办法动态添加背景图片链接?
  • 这是一个很好的答案——它很简单而且效果很好。就我而言,出于某种原因,我不得不在视差函数中添加 70px - 可能是因为没有考虑边距/填充/导航栏,但这是一个简单的修复。
【解决方案2】:

查看了你提供的样例网站,我发现作者可能会使用一个名为Stellar.js的库来实现这个效果,看看库网站,干杯!

【讨论】:

    【解决方案3】:

    我认为您正在寻找的是保持背景图像固定并仅在滚动时移动内容。为此,您必须简单地使用以下 css 属性:

    背景附件:固定;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 2016-10-16
      相关资源
      最近更新 更多