动态设置轮播宽度为窗口大小

时间:2016-04-18 14:58:21

标签: html css asp.net carousel

我在asp.net中使用bootstrap轮播。我想设置轮播的宽度,以便它占据浏览器窗口的整个宽度,具有固定的高度,如果我将宽度更改为100%它不起作用。以下是轮播的代码

<div id="myCarousel" class="carousel slide" data-ride="carousel" style="width:1177px; height:239px; top: 0px; left: 0px;">


<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox" style="width:1177px; height:240px; top: 0px; left: 0px;">
<div class="item active">
  <img src='images/image1.jpg' alt="Chains" style="width:1177px"/>
</div>


<div class="item">
  <img src='images/image2.jpg' alt="Chains" style="width:1177px"/>
</div>

<div class="item">
  <img src='images/image3.jpg' alt="Chains" style="width:1177px"/>
</div>
  <div class="item">
  <img src='images/image4.jpg' alt="Chains" style="width:1177px"/>
</div>    
</div>
</div>

我手动将图像和轮播的宽度设置到浏览器窗口,但我希望这是动态的。

以下是CSS

.carousel {
position: relative;
}
.carousel-inner {
position: relative;
width: 100%;
overflow: hidden;
}
.carousel-inner > .item {
position: relative;
display: none;
-webkit-transition: .6s ease-in-out left;
      transition: .6s ease-in-out left;
}
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
line-height: 1;
width: 1177px;
}
.carousel-inner > .active,
.carousel-inner > .next,
.carousel-inner > .prev {
display: block;
}
.carousel-inner > .active {
left: 0;
}
.carousel-inner > .next,
.carousel-inner > .prev {
position: absolute;
top: 0;
width: 100%;
}
.carousel-inner > .next {
left: 100%;
}
.carousel-inner > .prev {
left: -100%;
}
.carousel-inner > .next.left,
.carousel-inner > .prev.right {
left: 0;
}
.carousel-inner > .active.left {
left: -100%;
}
.carousel-inner > .active.right {
left: 100%;
}

当我向右滚动时,旋转木马会出现在侧边栏的顶部。我该如何避免这种情况。

1 个答案:

答案 0 :(得分:0)

您必须为内联给出的所有宽度提供100%的宽度。

我可以看到一些7-8宽度:html代码中的1177px使所有宽度:100%

同样在css中,所有类的宽度:100%。

CODE:

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style>
  .carousel-inner > .item > img,
  .carousel-inner > .item > a > img {
      width: 100%;
      margin: auto;
  }
</style>
<div class="container">
  <br>
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <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>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="img_chania.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_chania2.jpg" alt="Chania" width="460" height="345">
      </div>
    
      <div class="item">
        <img src="img_flower.jpg" alt="Flower" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_flower2.jpg" alt="Flower" width="460" height="345">
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>