在bootstrap中放大图像: - 将所有图像更改为放大的图像

时间:2017-11-29 09:44:30

标签: javascript jquery css image bootstrap-modal

我想在用户点击图片时显示放大图片。然后我按照步骤 形成这个How to use bootstrap modal on multiple images on same page on image click?
但是现在当我点击图像时,这个图像也会自动改变另一个图像。 这是在放大图像之前:

enter image description here

这是我们点击图片的时候:

enter image description here

现在所有图片都已更改:

enter image description here

我正在使用此代码。

 <div class="row" id="advertisement" style="margin-bottom: 10px;">

  </div>

模态代码

<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
        <center><img class="imgr img-responsive" src="" width="80%" height="200px" /></center>

</div>
</div>

这是javascript代码: -

  $(document).ready(function()
 {
 var url="http://api.dentallabworld.com/advertisement.php";
 $.getJSON(url,function(result){
 console.log(result);
 $.each(result, function(i, field){
 var advertisement=field.Image;
 var size=field.Size;
 $("#advertisement").append('<div class="col s'+size+'" style="padding:2.5px 
 5px 5px 5px; "><img id="'+(i+1)+'" class="imgr" width="100%" height="200px" 
 data-toggle="modal" data-target="#myModal" 
  src="{image path}/'+advertisement+'"></div>');

   });
   });
   });

1 个答案:

答案 0 :(得分:1)

试试这个 -

<div class="row" id="advertisement" style="margin-bottom: 10px;">

</div>
<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <center><img class="enlarged_image imgr img-responsive" src="" width="80%" height="200px" /></center>
    </div>
</div>

$(document).ready(function() {
    var url = "http://api.dentallabworld.com/advertisement.php";
    $.getJSON(url, function(result) {
        console.log(result);
        $.each(result, function(i, field) {
            var advertisement = field.Image;
            var size = field.Size;
            $("#advertisement").append('<div class="cols' + size + '" style="padding: 2.5px 5px 5px 5px;"><a href="#" class="pop"><img id="'+(i+1)+'" class="click_to_enlarge imgr" width="100%" height="200px" src="{image_path}/'+advertisement+'" onclick="showImg({image_path}/\''+advertisement+'\')"></a></div>');

        });
    });
});

var showImg = function(src) {
    $('.enlarged_image').attr('src', src);
    $('#myModal').modal('show');
}