在自举轮播中的链接上定位按钮

时间:2018-12-06 18:01:40

标签: html css html5 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>

4 个答案:

答案 0 :(得分:1)

将类别为“ 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>

答案 1 :(得分:1)

Z索引仅适用于彼此位于同一级别的定位元素。您应该将其设置为轮播内部课程

 .carousel-inner{
z-index:999;
}

编辑:尝试这样

<html>
<link type="text/css" id="dark-mode" rel="stylesheet" href="">
<style type="text/css" id="dark-mode-custom-style"></style>
<head>
    <style>
        .carousel-inner .item .myButton {
            position: absolute;
            top: 0;
            right: 0;

        }

        .carousel {
            height: 400px;
        }

        .carousel-inner {
            width: 100%; /*this must stay */
            height: 100%; /*this must stay */
        }

        .carousel-inner .item {
            z-index: 9999; /*not working */
        }

        .carousel-inner .carousel-control {
            z-index: 990 !important;
        }
    </style>
</head>
<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=""></li>
        <li data-target="#carousel-example-generic" data-slide-to="1" class="active"></li>
        <li data-target="#carousel-example-generic" data-slide-to="2" class=""></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <div class="item text-center">
            <button type="button" class="myButton">Btn</button>
            Slide 1
        </div>
        <div class="item text-center active">
            <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>


        <!-- 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>

</div>
<script type="text/javascript">

</script>

<div class="as-console-wrapper">
    <div class="as-console"></div>
</div>
</body>
</html>

答案 2 :(得分:1)

我知道您的感觉,这也发生在我身上,直到这一刻,我还没有理想的答案。因此我提出了替代解决方案,以减小旋转木马的高度。

我知道我的替代解决方案无法完全回答您的问题,但希望它能以某种方式为您提供帮助。

  .myButton{
  position:absolute;
  right:0px
  }
  .carousel-control{
  margin: auto auto;
  height:100px;
  width:100px;
  background:#ff0000 !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.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
</head>
<body>

<div class="container">
  <h2>Carousel Example</h2>  
  <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>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">
      <div class="item active">
      <button type="button" class="myButton" onclick="alert('slide1')">Btn</button>
        <img src="https://www.w3schools.com/bootstrap/la.jpg" alt="Los Angeles" style="width:100%;">
      </div>

      <div class="item">
      <button type="button" class="myButton" onclick="alert('slide2')">Btn</button>
        <img src="https://www.w3schools.com/bootstrap/chicago.jpg" alt="Chicago" style="width:100%;">
      </div>
    </div>

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

</body>
</html>

答案 3 :(得分:1)

u可以将内部轮播的z-index设置为高于链接,以使其位于最前面 然后,要使链接可点击,请将内部轮播设置为“ pointer-events”,将其设置为none,这将使轮播将click事件转移到其背后的元素,然后要使按钮可点击,则应将按钮的指针事件设置为auto,记住应该将每个轮播子项的指针事件属性设置为auto。

jsfiddle demo

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

.carousel {
    height: 400px;
}

.carousel-inner {
    pointer-events: none;
    z-index:10;
    width: 100%; /*this must stay */
    height: 100%; /*this must stay */
}
<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>