简体   繁体   English

引导模态imgAreaSelect模态关闭后隐藏所选区域

[英]bootstrap modal imgAreaSelect hide selected area after modal closes

Im trying to use imgAreaSelect plugin for cutting images, but I have problem. 我试图使用imgAreaSelect插件来剪切图像,但是我有问题。

If I open modal open image there then I see preview image and then I select area and I have in modal button what closes bootstrap modal, but it dont hide selected area. 如果我在那里打开模态打开图像,那么我会看到预览图像,然后选择区域,并且在模态按钮中有关闭引导模态的东西,但它不会隐藏所选区域。

It must hide selected area and then I have there form where is Image name and if I click upload then it must upload image what I selected. 它必须隐藏选择的区域,然后我在那儿有表单,其中的图像名称是,如果单击“上传”,则它必须上传我选择的图像。

At the moment it uploads image what I Cutted, but there is selected area if I close modal and its not nice in site. 目前,它会上传我剪切过的图像,但是如果我关闭模态,则会出现选定区域,并且在现场效果不佳。

My modal code: 我的模态代码:

<div class="col-sm-3"><button type="button" class="btn btn-info btn-lg" id="press">Cut image</button></div>
<div id="popup" class="modal fade" role="dialog">
  <div class="modal-dialog modal-lg">

    <!-- Modal content-->
    <div class="modal-content" style="display:inline-block;">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Cut image</h4>
      </div>
      <div class="modal-body">
        <input name="bgimg" id="fileInput" size="30" type="file" />
        <input type="hidden" id="x" name="x" />
        <input type="hidden" id="y" name="y" />
        <input type="hidden" id="w" name="w" />
        <input type="hidden" id="h" name="h" />
        <p><img id="filePreview" style="display:none;"/></p>
        <div style="clear: both;"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" id="closemodal" data-dismiss="modal">Go add information</button>
      </div>
    </div>

  </div>
</div>

My jquery code: 我的jQuery代码:

//set image coordinates
function updateCoords(im,obj){
  $('#x').val(obj.x1);
  $('#y').val(obj.y1);
  $('#w').val(obj.width);
  $('#h').val(obj.height);
}

//check coordinates
function checkCoords(){
  if(parseInt($('#w').val())) return true;
  alert('Please select a crop region then press submit.');
  return false;
}

$(document).ready(function(){
  //prepare instant image preview
  var p = $("#filePreview");
  $("#fileInput").change(function(){
    //fadeOut or hide preview
    p.fadeOut();

    //prepare HTML5 FileReader
    var oFReader = new FileReader();
    oFReader.readAsDataURL(document.getElementById("fileInput").files[0]);

    oFReader.onload = function (oFREvent) {
      p.attr('src', oFREvent.target.result).fadeIn();
    };
  });

  //implement imgAreaSelect plugin
  $('img#filePreview').imgAreaSelect({
    onSelectEnd: updateCoords
  });
});
$(document).ready(function() {
    $("#popup").modal({
        show: false,
        backdrop: 'static'
    });

    $("#press").click(function() {
       $("#popup").modal("show");             
    });
});
$(document).ready(function(){
    $("#closemodal").click(function(){
        $("img#filePreview").hide();
    });
});

Jsfiddle example: https://jsfiddle.net/efsdbyxb/5/ jsfiddle示例: https ://jsfiddle.net/efsdbyxb/5/

Also wanted ask how I can set fixed size? 还想问我如何设置固定尺寸?

You are trying to remove this by $("img#filePreview").hide(); 您正在尝试通过$("img#filePreview").hide();删除它$("img#filePreview").hide();

The problem is that the plugin created a different DOM nodes so when you hide the img you not hide those nodes. 问题在于该插件创建了一个不同的DOM节点,因此当您隐藏img您不会隐藏这些节点。

The solution is to "say" to the plugin that you want to reset the selection. 解决方案是“说出”您要重置选择的插件。

When you want to "talk" with the plugin, you need to pass a prop/value instance: true to the plugin, in this way, the plugin will returns an instance of the plugin instead of the jQuery collection Scroll to API method section . 当您想与插件“对话”时,您需要传递一个prop / value instance: true到插件,通过这种方式,插件将返回插件的实例,而不是jQuery集合Scroll to API method section

Once you have the instance of the plugin, you can call the api method cancelSelection . 获得插件实例后,可以调用api方法cancelSelection

Also, I wrapped the inputs with a form so you can reset it when you close the modal, so the user could upload another file. 另外,我用form包装了输入内容,以便您在关闭模式时可以将其重置,以便用户可以上传另一个文件。

That's how you can request the instance: 这样便可以请求实例:

imgPreview = $('img#filePreview').imgAreaSelect({
  onSelectEnd: updateCoords,
  instance: true
});

And cancel the selection: 并取消选择:

$("#closemodal").click(function() {
  $("img#filePreview").hide();
  imgPreview.cancelSelection();
  // call it after you upload the image
  $('form')[0].reset();
});

And All the code: 和所有代码:

https://jsfiddle.net/efsdbyxb/6/ https://jsfiddle.net/efsdbyxb/6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM