Bootstrap中的模态图标

时间:2017-01-29 12:22:08

标签: jquery html twitter-bootstrap-3

我有bootstrap模式的问题。 我想用模态显示图片(用HTML格式显示1,2,3 ......)。 在每张图片上,我都覆盖了背景和图标(glyphicon)。 问题是,如果我的模态是打开的,下面的图片也是一个图标。我不知道问题出在哪里。我尝试了很多事情,但我仍然不知道出了什么问题。

HTML:

<div id="content">
        <div class="container">
            <div class="row margin-row">
                <div class="col-xs-6 col-sm-3 image_container"><a title="1" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_1.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div>
                <div class="col-xs-6 col-sm-3 image_container"><a title="2" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_2.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div>
                <div class="col-xs-6 col-sm-3 image_container"><a title="3" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_3.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div> 
                <div class="col-xs-6 col-sm-3 image_container"><a title="4" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_4.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div>
                <div class="col-xs-6 col-sm-3 image_container"><a title="5" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_1.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div>
                <div class="col-xs-6 col-sm-3 image_container"><a title="6" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_2.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div>
                <div class="col-xs-6 col-sm-3 image_container"><a title="7" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_3.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div>
                <div class="col-xs-6 col-sm-3 image_container"><a title="8" href="#content"><img class="img_thumbnail img-responsive" src="images/pic_4.jpg"><div class="after"><i class="glyphicon glyphicon-plus"></i></div></a></div>
            </div>
        </div>
    <div tabindex="-1" class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" data-dismiss="modal">×</button>
                    <h3 class="modal-title">Heading</h3>
                </div>
                <div class="modal-body">

                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</div>      

jQuery的:

    $('.img_thumbnail, .after').click(function(){
     $('.modal-body').empty();
     var title = $(this).parent('a').attr("title");
     $('.modal-title').html(title);
     $($(this).parents('div').html()).appendTo('.modal-body');
     $('#myModal').modal({show:true});
});

以下是两张图片:

enter image description here

第二张图片(这里我们可以看到冗余图标): enter image description here

请帮忙。

1 个答案:

答案 0 :(得分:0)

Bootstrap模式会发出一些可用于隐藏的事件以及打开时的图标(参见Modal Events):

$('#myModal').on('show.bs.modal', function() {
  $('.after', this).hide();
});

&#13;
&#13;
    $('.img_thumbnail, .after').click(function() {
      $('.modal-body').empty();
      var title = $(this).parent('a').attr("title");
      $('.modal-title').html(title);
      $($(this).parents('div').html()).appendTo('.modal-body');
      $('#myModal').modal({
        show: true
      });
    });
 $('#myModal').on('show.bs.modal', function() {
   $('.after', this).hide();
 });
&#13;
.after {
  position: relative;
  left: 30px;
  top: -80px;
  z-index: 99;
  font-size: 40px;
  color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.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="content">
  <div class="container">
    <div class="row margin-row">
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="1" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="2" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="3" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="4" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="5" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="6" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="7" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
      <div class="col-xs-6 col-sm-3 image_container">
        <a title="8" href="#content">
          <img class="img_thumbnail img-responsive" src="http://lorempixel.com/100/100">
          <div class="after"><i class="glyphicon glyphicon-plus"></i>
          </div>
        </a>
      </div>
    </div>
  </div>
  <div tabindex="-1" class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button class="close" type="button" data-dismiss="modal">×</button>
          <h3 class="modal-title">Heading</h3>
        </div>
        <div class="modal-body">

        </div>
        <div class="modal-footer">
          <button class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;