Bootstrap下拉菜单无法在轮播幻灯片中使用

时间:2017-01-12 13:44:35

标签: html css twitter-bootstrap drop-down-menu carousel

我需要在网页中有旋转木马幻灯片,旋转木马幻灯片内需要有下拉菜单。

旋转木马幻灯片工作正常,但当我点击dorpdown菜单按钮时 它似乎没有出现。

请帮我弄清楚解决方案。

以下是我的代码。



<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  
  .carousel-control.left {
  left: -30px;
  /* left: auto; */
}
.carousel-control .icon-prev,
.carousel-control .glyphicon-chevron-left {
  left: 50%;
  margin-left: -10px;
}
.carousel-control .icon-next,
.carousel-control .glyphicon-chevron-right {
  right: 50%;
  margin-right: -10px;
}
.carousel-indicators {
  position: absolute;
  bottom: -40px;
  left: 50%;
  z-index: 15;
  width: 60%;
  padding-left: 0;
  margin-left: -30%;
  text-align: center;
  list-style: none;
}
.carousel-control {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  font-size: 20px;
  color: #FFFFFF;
  /* width: 15%; 
  filter: alpha(opacity=50);
  opacity: .5;
  */
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .6);
  
}
  </style>
</head>
<body>

<div class="container">
  <br>
  <div id="myCarousel" class="carousel slide">
    <!-- 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">
        <div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a></li>
      <li role="presentation" class="divider"></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a></li>    
    </ul>
  </div>
      </div>

      <div class="item">
       <div class="dropdown">
    <button class="btn btn-danger dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a></li>
      <li role="presentation" class="divider"></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a></li>    
    </ul>
  </div>
      </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>

</body>
</html>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:2)

只需将轮播内部从overflow: hidden更改为overflow: visible

.carousel-inner {
    overflow: visible;
}

工作示例:

&#13;
&#13;
.carousel .carousel-control.left {
  left: -30px;
  /* left: auto; */
}
.carousel .carousel-control .icon-prev,
.carousel .carousel-control .glyphicon-chevron-left {
  left: 50%;
  margin-left: -10px;
}
.carousel .carousel-control .icon-next,
.carousel .carousel-control .glyphicon-chevron-right {
  right: 50%;
  margin-right: -10px;
}
.carousel .carousel-indicators {
  position: absolute;
  bottom: -40px;
  left: 50%;
  z-index: 15;
  width: 60%;
  padding-left: 0;
  margin-left: -30%;
  text-align: center;
  list-style: none;
}
.carousel-control {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  font-size: 20px;
  color: #FFFFFF;
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .6);
}
.carousel .carousel-inner {
  overflow: visible;
  text-align: center
}
&#13;
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container">
  <br>
  <div id="myCarousel" class="carousel slide">
    <!-- 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>
    </ol>

    <div class="carousel-inner" role="listbox">

      <div class="item active">
        <div class="btn-group">
          <button class="btn btn-primary dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">Dropdown Example
            <span class="caret"></span>
          </button>
          <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a>
            </li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a>
            </li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a>
            </li>
            <li role="presentation" class="divider"></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a>
            </li>
          </ul>
        </div>
      </div>

      <div class="item">
        <div class="btn-group">
          <button class="btn btn-danger dropdown-toggle" id="menu2" type="button" data-toggle="dropdown">Dropdown Example
            <span class="caret"></span>
          </button>
          <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a>
            </li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a>
            </li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a>
            </li>
            <li role="presentation" class="divider"></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a>
            </li>
          </ul>
        </div>
      </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>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

它有效,只需添加一些高度:

.carousel-inner { height:... }

答案 2 :(得分:0)

用户无法点击下拉列表,因为箭头元素carousel-control 下拉列表

减小项目容器.carousel-inner的宽度,使其位于中心,并设置高度:

.carousel-inner {
   position: relative;
   width: 80%;
   overflow: hidden;
   height: 500px;
   margin: 0 auto;
}

答案 3 :(得分:0)

因为.carousel-inner div具有以下类

.carousel-inner {
    position: relative;
    width: 100%;
    overflow: hidden;
}

将隐藏溢出,这就是下拉选项不可见的原因。

你可以覆盖这个类,或者给.carousel-inner一些高度。

.carousel-inner
{
  height:400px;
}

.carousel-inner
{
  overflow:visible !important;
}

.carousel-inner
{
  overflow:visible !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  
  .carousel-control.left {
  left: -30px;
  /* left: auto; */
}
.carousel-control .icon-prev,
.carousel-control .glyphicon-chevron-left {
  left: 50%;
  margin-left: -10px;
}
.carousel-control .icon-next,
.carousel-control .glyphicon-chevron-right {
  right: 50%;
  margin-right: -10px;
}
.carousel-indicators {
  position: absolute;
  bottom: -40px;
  left: 50%;
  z-index: 15;
  width: 60%;
  padding-left: 0;
  margin-left: -30%;
  text-align: center;
  list-style: none;
}
.carousel-control {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  font-size: 20px;
  color: #FFFFFF;
  /* width: 15%; 
  filter: alpha(opacity=50);
  opacity: .5;
  */
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .6);
  
}
  </style>
</head>
<body>

<div class="container">
  <br>
  <div id="myCarousel" class="carousel slide">
    <!-- 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">
        <div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a></li>
      <li role="presentation" class="divider"></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a></li>    
    </ul>
  </div>
      </div>

      <div class="item">
       <div class="dropdown">
    <button class="btn btn-danger dropdown-toggle" id="menu1" type="button" data-toggle="dropdown">Dropdown Example
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">HTML</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">CSS</a></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">JavaScript</a></li>
      <li role="presentation" class="divider"></li>
      <li role="presentation"><a role="menuitem" tabindex="-1" href="#">About Us</a></li>    
    </ul>
  </div>
      </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>

</body>
</html>

答案 4 :(得分:-2)

这里的位置:绝对给予ul导致问题..改变它相对..并调整左边

.dropdown-menu{
position:relative;
left:14%; /* or accroding to the dimension */

}