简体   繁体   中英

How do i add a Bootstrap carousel to Ruby on rails app?

I am new developer and trying to add a Bootstrap carousel to my rails app.

This is the carousel that I am trying to add in particular: http://arturssmirnovs.com/blog/bootstrap-carousel-100-height-and-width/

So far i've only gotten the white left and right pointers to show up from the carousel (Ex: the navigation pointers that look like this < >)

Here's my html code, that i snabbed from the link above, I inserted this portion in the about.html.erb page, which is the static page that I want the carousel to appear in like this

views/layouts/staticpages/about.html.erb:

<div class="aboutslide">

<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item bg bg1 active">
<div class="container">
<div class="carousel-caption">
<h1>Example headline.</h1>
<p>Note: If you're viewing this page via a <code>file://</code> URL, the "next" and "previous" Glyphicon buttons on the left and right might not load/display properly due to web browser security rules.</p>
<p><a class="btn btn-lg btn-primary" href="#" role="button">Sign up today</a></p>
</div>
</div>
</div>
<div class="item bg bg2">
<div class="container">
<div class="carousel-caption">
<h1>Another example headline.</h1>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a class="btn btn-lg btn-primary" href="#" role="button">Learn more</a></p>
</div>
</div>
</div>
<div class="item bg bg1">
<div class="container">
<div class="carousel-caption">
<h1>One more for good measure.</h1>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
<p><a class="btn btn-lg btn-primary" href="#" role="button">Browse gallery</a></p>
</div>
</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
<a class="right carousel-control" href="#myCarousel" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
</div>

</div>

Here is my app/assets/stylesheets/carousel.css file:

html, body {
    height:100%;
    margin:0;
    padding:0;
}
    .carousel, .item, .active {
    height:100%;
}
.carousel-inner {
    height:100%;
}
.carousel {
    margin-bottom: 60px;
}
.carousel-caption {
    z-index: 10;
}
.carousel .item {
    background-color: #777;
}
.carousel .carousel-inner .bg {
    background-repeat:no-repeat;
    background-size:cover;
}
.carousel .carousel-inner .bg1 {
    background-image:url(bg1.jpg);
    background-position: center top;
}
.carousel .carousel-inner .bg2 {
    background-image:url(bg2.jpg);
    background-position: center center;
}
.carousel .carousel-inner .bg3 {
    background-image:url(bg3.jpg);
    background-position: center bottom;
}

@media (min-width: 768px) {


  /* Bump up size of carousel content */
  .carousel-caption p {
    margin-bottom: 20px;
    font-size: 21px;
    line-height: 1.4;
  }
}

and there is the docs.js file that i have inserted in apps/assets/javascripts/docs.js . I can't post it because the file is too big.

The issue with the button images is probably just that you need to add the images to your app/assets/images directory and change the css use the Rails asset_path helper:

.carousel .carousel-inner .bg {
    background-repeat:no-repeat;
    background-size:cover;
}
.carousel .carousel-inner .bg1 {
    background-image:url(<%= assets_path 'bg1.jpg' %>);
    background-position: center top;
}
.carousel .carousel-inner .bg2 {
    background-image:url(<%= assets_path 'bg2.jpg' %>);
    background-position: center center;
}
.carousel .carousel-inner .bg3 {
    background-image:url(<%= assets_path 'bg3.jpg' %>);
    background-position: center bottom;
}

This uses ERB (embedded ruby) to add the correct url in the stylesheet. You can also just change the extension of the file to '.css.scss' and use the sass-rails helpers.

.carousel.carousel-inner {
  .bg {
    background-repeat:no-repeat;
    background-size:cover;
  }
  .bg1 {
    background-image:url( image-url('bg1.jpg') );
    background-position: center top;
  }  
  .bg2 {
    background-image:url( image-url('bg2.jpg') );
    background-position: center center;
  }
  .bg3 {
    background-image:url( image-url('bg3.jpg') );
    background-position: center bottom;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM