admin管理员组

文章数量:1296451

Currently the owl-carousel that I have employed in my website is far too tall, as it takes up more than the full screen. From the demo on owl-carousel for one image per slide it appears that it is possible to adjust the height. I am using the latest version of owl carousel 2. I also tried wrapping the carousel in another div and adjusting the height of the outer div. I have noticed that I can adjust the width, which also has the effect of adjusting height. However, I would like to adjust the height without adjusting the width. I have attached my javascript and html. Thanks.

 <body>
  <div id="navigation"> 
      <ul>
        <li> <%= link_to "About", pages_about_path, id: "about-link"; %> </li>
        <li> <%= link_to "Contact Us", pages_contact_us_path; %> </li>
      </ul>   
  </div> 

  <div class="wrapper"> 


    <h1><img src=".jpg"> </h1>

      <div id="carousel" class="owl-carousel owl-theme">
           <div class="item"><img src=".jpg"></div>
           <div class="item"><img style="width:100%; height:auto;" src=".jpg"></div>
       </div>


      <div class="contact">
        <aside>
          <h2>Contact Info</h2> 
            <p> Nomadic Inc. <br>
                Tuscaloosa, AL 35404 <br>
                <b> E-mail: </b> [email protected] <br>
                <b> Phone: </b> 205-799-1668

            </p>
        </aside>
      </div>  
  </div>
</body> 

And the javascript:

$ ->
  $("#carousel").owlCarousel({
  autoplay: true,
  items: 1,
  loop: true,
  navigation : true, 
  slideSpeed : 300,
  paginationSpeed : 400,
  singleItem:true
  })

Currently the owl-carousel that I have employed in my website is far too tall, as it takes up more than the full screen. From the demo on owl-carousel for one image per slide it appears that it is possible to adjust the height. I am using the latest version of owl carousel 2. I also tried wrapping the carousel in another div and adjusting the height of the outer div. I have noticed that I can adjust the width, which also has the effect of adjusting height. However, I would like to adjust the height without adjusting the width. I have attached my javascript and html. Thanks.

 <body>
  <div id="navigation"> 
      <ul>
        <li> <%= link_to "About", pages_about_path, id: "about-link"; %> </li>
        <li> <%= link_to "Contact Us", pages_contact_us_path; %> </li>
      </ul>   
  </div> 

  <div class="wrapper"> 


    <h1><img src="http://cdn-0.famouslogos.us/images/puter-logos/ab-puter-repair-logo.jpg"> </h1>

      <div id="carousel" class="owl-carousel owl-theme">
           <div class="item"><img src="https://responsivedesign.is/__data/assets/image/0013/5017/Owl-Carousel-2.jpg"></div>
           <div class="item"><img style="width:100%; height:auto;" src="http://www.pcgeeksusa./wp-content/uploads/2015/01/puter-repair.jpg"></div>
       </div>


      <div class="contact">
        <aside>
          <h2>Contact Info</h2> 
            <p> Nomadic Inc. <br>
                Tuscaloosa, AL 35404 <br>
                <b> E-mail: </b> [email protected] <br>
                <b> Phone: </b> 205-799-1668

            </p>
        </aside>
      </div>  
  </div>
</body> 

And the javascript:

$ ->
  $("#carousel").owlCarousel({
  autoplay: true,
  items: 1,
  loop: true,
  navigation : true, 
  slideSpeed : 300,
  paginationSpeed : 400,
  singleItem:true
  })
Share Improve this question asked May 25, 2016 at 0:18 BSpasskyBSpassky 711 gold badge1 silver badge2 bronze badges 1
  • Is it possible to put this on jsfiddle with all the JS, CSS and HTML codes? It will help a lot to trouble shoot. – BigRedDog Commented May 25, 2016 at 1:13
Add a ment  | 

3 Answers 3

Reset to default 0

Add autoHeight:true it will set the height of the carousel to auto.

See below the correct code

$("#carousel").owlCarousel({
    autoplay: true,
    items: 1,
    loop: true,
    navigation : true, 
    slideSpeed : 300,
    paginationSpeed : 400,
    singleItem:true,
    autoHeight:true
})

The owl carousel normally has a different height according to the images placed in the carousel. To create an height that won't change you can use

autoHeight: true

This changes the height of the carousel according to the biggest image or item inside the carousel. So the plan is to calculate all visible items and change height according to heighest item. Your full JS code would be as follows:

$("#carousel").owlCarousel({
  autoplay: true,
  items: 1,
  loop: true,
  navigation : true, 
  slideSpeed : 300,
  paginationSpeed : 400,
  singleItem:true
  autoHeight:true
})

Can You please try this

#carousel {max-height:500px;overflow:hidden;}
#carousel .item img {width: 100%;height: 100%;object-fit:cover;}
.wrapper {max-height:500px;overflow:hidden;}

@media (max-width: 768px)
{
#carousel {max-height: 300px;}
}


    
$(() => {
$("#carousel").owlCarousel({
autoplay: true,
items: 1,
loop: true,
nav: true,
slideSpeed: 300,
paginationSpeed: 400,
autoHeight: true,
});
});

本文标签: javascriptAdjust height of owlcarouselStack Overflow