自动关闭模态窗口?一段时间后如何让模态窗口自动关闭?

时间:2014-08-21 14:02:36

标签: jquery modal-dialog

如何让模态窗口在一段时间后自动关闭?

例如:http://ruseller.com/lessons/les2000/demo/index.html。窗口弹出,现在我想在2秒后窗口关闭。

代码在这里:

<div class="remodal-bg"gt;
...Ваш контент...
</div>

<div class="remodal" data-remodal-id="modal">
    <h1>Remodal</h1>
    <p>
      Flat, responsive, lightweight, fast, easy customizable modal window plugin
      with declarative state notation and hash tracking.
    </p>
    <br>
    <a class="remodal-cancel" href="#">Cancel</a>
    <a class="remodal-confirm" href="#">OK</a>
</div>



<a href="#modal">Call the modal with data-remodal-id="modal"</a>


$(document).on('open', '.remodal', function () {
    // открытие окна
    var modal = $(this);
});

$(document).on('opened', '.remodal', function () {
    // окно открыто
    var modal = $(this);
});

$(document).on('close', '.remodal', function () {
    // закрытие окна
    var modal = $(this);
});

$(document).on('closed', '.remodal', function () {
    // окно закрыто
    var modal = $(this);
});

$(document).on('confirm', '.remodal', function () {
    // нажатие на кнопку подтверждения
    var modal = $(this);
});

$(document).on('cancel', '.remodal', function () {
    // нажатие на кнопку отмены
    var modal = $(this);
});


<script>
    var options = {...};
    $('[data-remodal-id=modal]').remodal(options).open();
</script>


var inst = $.remodal.lookup[$('[data-remodal-id=modal]').data('remodal')];

// открыть модальное окно
inst.open();

// закрыть модальное окно
inst.close();

2 个答案:

答案 0 :(得分:1)

此评论位于您链接到的页面的来源:

// You can open or close it like this:
// var inst = $.remodal.lookup[$('[data-remodal-id=modal]').data('remodal')];
// inst.open();
// inst.close();

所以你可以将关闭包装在一个setTimeout

setTimeout(inst.close, 2000);

答案 1 :(得分:1)

您可以为open事件添加一个侦听器,然后添加timeout 2000毫秒,以便在此之后关闭它。

$(document).on('open', '.remodal', function () {
    console.log('open');
    setTimeout(function(){$(this).remodal('close');},
    2000);
});
相关问题