固定位置时,模态显示按钮不起作用

时间:2019-08-02 10:13:38

标签: twitter-bootstrap-3 modal-dialog

我有一个已设置为position:的按钮,固定在页面右侧。 单击时,应打开模态,但单击时不执行任何操作。

如果我移除position:fixed,它可以正常工作,并且模态显示,但这不是我想要的,因为它需要固定到侧面

  <html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css"
    rel="stylesheet" type="text/css">
    <link href="https://pingendo.github.io/pingendo-bootstrap/themes/default/bootstrap.css"
    rel="stylesheet" type="text/css">
  </head>

  <body>
    <div class="navbar navbar-default navbar-static-top" style="height:80px;background-color:white;border-bottom:0px;">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-ex-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" style="padding-top:0px;"><img height="75" alt="Brand" src="logo260x80-2.png"></a>
        </div>
        <div class="collapse navbar-collapse" id="navbar-ex-collapse" style="border-top:0px;border-bottom:0px;">
          <ul class="nav navbar-nav navbar-right" style="padding-top:20px;border-top:0px;border-bottom:0px;">
            <li class="active">
              <a href="#">Home</a>
            </li>
            <li>
              <a href="#">Contacts</a>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="section">
      <div class="background-image" style="background-image : url('headerbg3.jpg');"></div>
      <div class="container">
        <div class="row">
          <div class="col-md-12" style="height:200px;"></div>
        </div>
      </div>
    </div>
    <button data-toggle="modal" data-target="#demo" type="button" style="position:fixed;right:0px;"
    tab-index="1" id="showmodal" class="btn btn-lg btn-success">
      <i class="fa fa-envelope fa-2x"></i>
    </button>
    <div class="section">
      <div class="container">
        <div class="row">
          <div class="col-md-12">
            <h1 class="text-center">Gallery</h1>
          </div>
        </div>
        <div class="row">
          <div class="col-md-4">
            <a><img src="http://pingendo.github.io/pingendo-bootstrap/assets/placeholder.png" class="img-responsive"></a>
            <h3>A title</h3>
            <p>Lorem ipsum dolor sit amet, consectetur adipisici elit,&nbsp;</p>
          </div>
          <div class="col-md-4">
            <a><img src="http://pingendo.github.io/pingendo-bootstrap/assets/placeholder.png" class="img-responsive"></a>
            <h2>A title</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipisici elit,&nbsp;</p>
          </div>
          <div class="col-md-4">
            <a><img src="http://pingendo.github.io/pingendo-bootstrap/assets/placeholder.png" class="img-responsive"></a>
            <h2>A ttile</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipisici elit,&nbsp;</p>
          </div>
        </div>
        <div class="row">
          <div class="col-md-4">
            <a><img src="http://pingendo.github.io/pingendo-bootstrap/assets/placeholder.png" class="img-responsive"></a>
            <h3>A title</h3>
            <p>Lorem ipsum dolor sit amet, consectetur adipisici elit,&nbsp;</p>
          </div>
          <div class="col-md-4">
            <a><img src="http://pingendo.github.io/pingendo-bootstrap/assets/placeholder.png" class="img-responsive"></a>
            <h3>A title</h3>
            <p>Lorem ipsum dolor sit amet, consectetur adipisici elit,&nbsp;</p>
          </div>
          <div class="col-md-4">
            <a><img src="http://pingendo.github.io/pingendo-bootstrap/assets/placeholder.png" class="img-responsive"></a>
            <h2>A title</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipisici elit,&nbsp;</p>
          </div>
        </div>
      </div>
    </div>
    <div class="modal fade" id="demo" tabindex="-1" role="dialog">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title">Modal title</h4>
          </div>
          <div class="modal-body">
            <p>One fine body...</p>
          </div>
          <div class="modal-footer">
            <a class="btn btn-default" data-dismiss="modal">Close</a>
            <a class="btn btn-primary">Save changes</a>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>

上面是页面的完整HTMl代码

1 个答案:

答案 0 :(得分:0)

问题在于该按钮隐藏在div部分后面,无法单击。因此,为了使按钮显示在该部分的前面,我们对按钮应用z-index:1;样式,代码将如下所示:

<button data-toggle="modal" data-target="#demo" type="button" style="z-index:1;position:fixed;right:0px;" tab-index="1" id="showmodal" class="btn btn-lg btn-success">

JSfiddle demo

  

注意:Here您可以阅读有关z-index属性的更多信息。