Bootstrap 3 - jumbotron background image effect

前端 未结 3 1149
梦谈多话
梦谈多话 2020-12-07 07:30

Hi I\'m trying to build a site with a jumbotron with a background image, which in itself is not hard:

HTML:

...
相关标签:
3条回答
  • 2020-12-07 08:07

    I think what you are looking for is to keep the background image fixed and just move the content on scroll. For that you have to simply use the following css property :

    background-attachment: fixed;

    0 讨论(0)
  • 2020-12-07 08:12

    After inspecting the sample website you provided, I found that the author might achieve the effect by using a library called Stellar.js, take a look at the library site, cheers!

    0 讨论(0)
  • 2020-12-07 08:14

    Example: http://bootply.com/103783

    One way to achieve this is using a position:fixed container for the background image and place it outside of the .jumbotron. Make the bg container the same height as the .jumbotron and center the background image:

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

    Then use jQuery to decrease the height of the .jumbtron as the window scrolls. Since the background image is centered in the DIV it will adjust accordingly -- creating a parallax affect.

    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

    0 讨论(0)
提交回复
热议问题