通过esc键或外部单击检测jquery显示插件模式

时间:2013-07-07 19:53:30

标签: javascript jquery reveal.js

我正在使用jquery reveal插件来显示弹出窗口。我在jquery或javascript中寻找一种方法,当按下esc键或点击弹出窗口外的弹出窗口时,我可以触发相应的事件。有什么方法可以捕捉到这个事件吗?

在揭示插件网站上,只给出了几个选项,例如:

$('#myModal').reveal({
 animation: 'fadeAndPop',                   //fade, fadeAndPop, none
 animationspeed: 300,                       //how fast animtions are
 closeonbackgroundclick: true,              //if you click background will modal close?
 dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
});

此插件还有其他选项吗?如果是这样,请提供链接。

2 个答案:

答案 0 :(得分:9)

根据source一个名为'揭示的事件:关闭'在这两种情况下都会触发,你应该能够通过

为该事件添加自己的处理程序
$yourModal.bind('reveal:close', function () {
  console.log('Modal closed');
});

当您需要知道它关闭的方式时,您可以使用'揭示:打开' event,在文档对象上添加keyup事件处理程序,或在.reveal-modal-bg元素上添加click事件处理程序。

$yourModal.bind('reval:open', function () {
  var $document = $(document);
  $document.bind('keyup', function onEscHandler( event ) {
    if (event.which === 27) {
      console.log('closed by ESC');

      // Modal is closed, let's remove the handler again
      $document.unbind('keyup', onEscHandler);
    }
  });


  var $modal_bg = $('.reveal-modal-bg');
  $modal_bg.one('click', function onBgClidkHandler() {
    console.log('closed by click on BG');
    // We don't need to remove this handler since 'one' does it automatically.
  });
});

答案 1 :(得分:1)

  1. Open jquery.reveal.js.

  2. Add new option:

    var defaults = {
    animation: 'fadeAndPop', animationspeed: 300, closeonbackgroundclick: true, dismissmodalclass: 'close-reveal-modal' escape: true // true: modal close with Esc. false: modal no close with Esc };

  3. In the file jquery.validate, find the line that content e.which===27.

    Complete line is:

    if(e.which===27){ modal.trigger('reveal:close'); }

  4. Modify and validate new option escape in this line:

    if(e.which===27 && options.escape === true){ modal.trigger('reveal:close'); }

  5. That's it. Now the escape option works.