Bootstrap 4:使用多个模式时无法关闭模态

时间:2017-12-22 17:35:48

标签: javascript html bootstrap-4

我目前正在尝试在Bootstrap 4中设置一个库。现在我想让图像可以点击并打开它的更大版本。这工作正常,但是一旦我使用多个脚本,它就不会再让我关闭模态了。

到目前为止,这是我的代码:

12-26 22:13:41.829 7420-7420/com.bachecubano.elbache E/elBacheAPP DB Found!: FileName:/storage/emulated/0/testing/elbacheapp.db
12-26 22:13:41.829 7420-7420/com.bachecubano.elbache E/elBacheAPP: Location:/storage/emulated/0/testing/elbacheapp.db
12-26 22:13:41.832 7420-7420/com.bachecubano.elbache E/elBacheAPP DATA: FileName:

JS:

<div id="Modal01" class="modal">
   <span class="close">&times;</span>
   <img class="modal-content" id="img01">
 </div>
  <div class="col-md-3">
    <div class="thumbnail">
        <img id="TheImage" src="/img/galleryimg1.png">
    </div>
  </div>

希望你们能帮帮我。

2 个答案:

答案 0 :(得分:0)

实现该库功能的最简单方法是使用Bootstrap提供的javascript库。有了它,您的标记和脚本可以像下面的示例一样简单。您还可以在Bootstrap文档的Arrays.setAll部分下阅读有关此设置的更多信息。

&#13;
&#13;
Arrays.parallelSetAll
&#13;
$('#gallery-modal').on('show.bs.modal', function(event) {
    var thumbnail = event.relatedTarget,
        title = thumbnail.alt,
        src = thumbnail.src,
        modal = $(this);

    modal.find('.modal-title').text(title);
    modal.find('.img-placeholder').attr('src', src);
});
&#13;
&#13;
&#13;

在不太可能的情况下,您希望完全忽略<div id="gallery" class="container"> <div class="row"> <div class="col-4"> <img class="img-fluid img-thumbnail" data-toggle="modal" data-target="#gallery-modal" src="http://via.placeholder.com/800x450/ff0000/ffffff?text=Image+1" alt="Image 1"/> </div> <div class="col-4"> <img class="img-fluid img-thumbnail" data-toggle="modal" data-target="#gallery-modal" src="http://via.placeholder.com/800x450/00ff00/ffffff?text=Image+2" alt="Image 2"/> </div> <div class="col-4"> <img class="img-fluid img-thumbnail" data-toggle="modal" data-target="#gallery-modal" src="http://via.placeholder.com/800x450/0000ff/ffffff?text=Image+3" alt="Image 3"/> </div> </div> </div> <div id="gallery-modal" class="modal" tabindex="-1" role="dialog"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title"></h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <img class="img-placeholder img-fluid" src=""/> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> </div> </div> </div> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>和jQuery提供的功能,并且您想要提出一个纯粹的JavaScript自定义解决方案,您绝对不应该对每个缩略图重复相同的代码。我假设您正在尝试这样做,并且当您在代码中使用全局变量时,它们会被覆盖。相反,您应该处理模态功能并以更通用的方式附加事件处理程序。我不建议采用这种方法,但仅仅是为了这个想法,这是一个纯javascript的例子。

&#13;
&#13;
bootstrap.js
&#13;
// Basic Modal object
var Modal = function() {
    var modal = document.getElementById('gallery-modal'),
        title = modal.querySelector('.modal-title'),
        img = modal.querySelector('.img-placeholder'),
        closeBtns = modal.querySelectorAll('[data-dismiss="modal"]'),
        bodyClassList = document.querySelector('body').classList;

    function show(title, src) {
        title.innerText = title;
        img.src = src;

        bodyClassList.add('modal-open');
        modal.style.display = 'block';
    }

    function hide() {
        bodyClassList.remove('modal-open');
        modal.style.display = 'none';

        title.innerText = '';
        img.src = '';
    }

    // Handling `click` on "close" buttons
    [...closeBtns].forEach(closeBtn => {
        closeBtn.addEventListener('click', hide);
    });

    return {
        show : show,
    };
}();

// Handling `click` events on thumbnails
[...document.querySelectorAll('#gallery .img-thumbnail')].forEach(thumbnail => {
    thumbnail.addEventListener('click', function() {
        // `this` is the <img> clicked
        var title = this.alt,
            src = this.src;

        // Modal is the global Modal object
        Modal.show(title, src);
    });
});
&#13;
&#13;
&#13;

答案 1 :(得分:0)

当前与

发生冲突
<div class="modal-backdrop fade show"></div>

之后您可以尝试使用CSS

body.modal-open .modal{
    z-index: 0;
}
body.modal-open .modal.fade.show{
    z-index: 1041;
}