twitter-bootstrap 需要在引导 css 中单击时将图像加载为模态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20138307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 19:18:09  来源:igfitidea点击:

Need to load image as modal on click in bootstrap css

javascriptjqueryhtmlcsstwitter-bootstrap

提问by Govan

I have tried loading a modal image on clicking a image by fading the background.

我尝试通过淡化背景来在单击图像时加载模态图像。

It works for me. But for multiple images how can I load the image which I have clicked based on the image id without writting multiple modal div.

这个对我有用。但是对于多个图像,如何在不编写多个模态 div 的情况下根据图像 ID 加载我单击的图像。

 <!DOCTYPE html>
 <html>

   <head>
     <title>Sample</title>
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <!-- Bootstrap -->
     <link href="./dist/css/bootstrap.css" rel="stylesheet">
     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
     <script src="https://code.jquery.com/jquery.js"></script>
     <!-- Include all compiled plugins (below), or include individual files as needed -->
     <script src="./dist/js/bootstrap.js"></script>
   </head>

   <body>
     <form class="form-horizontal" role="form">
       <!-- Image trigger modal -->
       <img src="http://sheshtawy.files.wordpress.com/2010/05/extra-firefox.png" id="img1" class="img-responsive img-thumbnail" style="min-height:300px;height:300px;" data-toggle="modal" data-target="#myModal" alt="Responsive image">

      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
          <div class="modal-body">
            <img src="http://sheshtawy.files.wordpress.com/2010/05/extra-firefox.png">
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
  </form>
</body>
</html>

回答by Sridhar R

Try this

试试这个

HTML

HTML

 <!-- Image trigger modal -->
       <img src="http://sheshtawy.files.wordpress.com/2010/05/extra-firefox.png" class="img-responsive img-thumbnail" style="min-height:300px;height:300px;" alt="Responsive image">
           <img src="http://sheshtawy.files.wordpress.com/2013/03/cairo_pollution.jpg?w=250"/>
           <img src="http://sheshtawy.files.wordpress.com/2012/12/img_4749.jpg?w=250"/>
      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
          <div class="modal-body">
            <img id="mimg" src="">
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

Script

脚本

$(window).load(function(){
    $('img').on('click',function()
                {
                    var sr=$(this).attr('src'); 
                    $('#mimg').attr('src',sr);
                    $('#myModal').modal('show');
                });
});

DEMO

演示