css - 将按钮定位在 Bootstrap 轮播中的链接上

标签 css html twitter-bootstrap

我想在每张幻灯片上添加一个位于 carousel-control 链接上方的右上角按钮。

我该怎么做?

下面你可以看到按钮在carousel-control

下面

.myButton {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 999; /*not working */
}

.carousel {
  height: 400px;
}

.carousel-inner {
   width: 100%; /*this must stay */
   height: 100%; /*this must stay */
   /* z-index: 1; //this make the carousel-control unclickable */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active text-center">
      <button type="button" class="myButton">Btn</button>
      Slide 1
    </div>
    <div class="item text-center">
      <button type="button" class="myButton">Btn</button>
      Slide 2
    </div>
    <div class="item text-center">
      <button type="button" class="myButton">Btn</button>
      Slide 3
    </div>
  </div>

  <!-- Controls -->
  <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

最佳答案

将类为“myButton”的按钮标签放在类为“item active text-center”的div 标签之前。我在下面的代码中为 Btn1 更改了它。

 <!DOCTYPE html>
    <html>
    <head>
      <title></title>
    </head>

    <style type="text/css">
      .myButton {
        position: absolute;
        top: 0;
        right: 0;
        z-index: 999; /*not working */
    }

    .carousel {
      height: 400px;
    }

.carousel-inner {
   width: 100%; /*this must stay */
   height: 100%; /*this must stay */
   /* z-index: 1; //this make the carousel-control unclickable */
}
    </style>
    <body>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
      <!-- Indicators -->
      <ol class="carousel-indicators">
        <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
        <li data-target="#carousel-example-generic" data-slide-to="1"></li>
        <li data-target="#carousel-example-generic" data-slide-to="2"></li>
      </ol>

      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <button type="button" class="myButton">Btn1</button>
        <div class="item active text-center">      
          Slide 1
        </div>
        <!-- <button type="button" class="myButton">Btn2</button> -->
        <div class="item text-center">
          <!-- <button type="button" class="myButton">Btn2</button> -->
          Slide 2
        </div>
        <!-- <button type="button" class="myButton">Btn3</button> -->
        <div class="item text-center">
          <!-- <button type="button" class="myButton">Btn3</button> -->
          Slide 3
        </div>
      </div>

      <!-- Controls -->
      <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>

    </body>
    </html>

关于css - 将按钮定位在 Bootstrap 轮播中的链接上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53657229/

相关文章:

PHP 给页面添加滚动条

javascript - 本地服务的页面无法在根的父文件夹中找到 Assets

html - 如何更好地对齐内嵌段落元素(没有表格)

html - 奇怪的字符

html - 如何使用 CSS 文件使 HTML 中的表格居中?

html - 按钮看起来不像 Bootstrap btn-default

javascript - 引用错误 : addEvent is not defined

asp.net - 在 ValidationSummary 上动态显示 bootstrap 周围的错误 div?

javascript - 有没有办法在不调用隐藏处理程序的情况下关闭 Bootstrap 模式

javascript - 我无法摆脱 HTML5 视频上方的这个差距(在移动设备上)