棘轮 - 听听JS关闭的popover

时间:2014-07-01 13:12:46

标签: javascript ratchet-2

我有一个棘轮弹出器,我从here取出它:

<div id="popover" class="popover">
  <header class="bar bar-nav">
    <h1 class="title">Popover title</h1>
  </header>
  <ul class="table-view">
    <li class="table-view-cell">Item1</li>
    <li class="table-view-cell">Item2</li>
  </ul>
</div>

<header class="bar bar-nav">
  <a href="#popover">
    <h1 class="title">
      Tap title
      <span class="icon icon-caret"></span>
    </h1>
  </a>
</header>

我希望找到一种方法来收听这个关于JS的popover(也使用jQuery)

这是popovers的棘轮js代码:

!(function () {
  'use strict';

  var popover;

  var findPopovers = function (target) {
    var i;
    var popovers = document.querySelectorAll('a');

    for (; target && target !== document; target = target.parentNode) {
      for (i = popovers.length; i--;) {
        if (popovers[i] === target) {
          return target;
        }
      }
    }
  };

  var onPopoverHidden = function () {
    popover.style.display = 'none';
    popover.removeEventListener('webkitTransitionEnd', onPopoverHidden);
  };

  var backdrop = (function () {
    var element = document.createElement('div');

    element.classList.add('backdrop');

    element.addEventListener('touchend', function () {
      popover.addEventListener('webkitTransitionEnd', onPopoverHidden);
      popover.classList.remove('visible');
      popover.parentNode.removeChild(backdrop);
    });

    return element;
  }());

  var getPopover = function (e) {
    var anchor = findPopovers(e.target);

    if (!anchor || !anchor.hash || (anchor.hash.indexOf('/') > 0)) {
      return;
    }

    try {
      popover = document.querySelector(anchor.hash);
    }
    catch (error) {
      popover = null;
    }

    if (popover === null) {
      return;
    }

    if (!popover || !popover.classList.contains('popover')) {
      return;
    }

    return popover;
  };

  var showHidePopover = function (e) {
    var popover = getPopover(e);

    if (!popover) {
      return;
    }

    popover.style.display = 'block';
    popover.offsetHeight;
    popover.classList.add('visible');

    popover.parentNode.appendChild(backdrop);
  };

  window.addEventListener('touchend', showHidePopover);

}());

我该怎么做?

感谢

1 个答案:

答案 0 :(得分:0)

要触发事件documented here。如果您要将事件配置为冒泡,请查看它。基本上,在你的popover上触发一个事件,如下所示:

var event = new CustomEvent('pop', { "detail": { "id": popover.id, "type": "hide" }});
// Dispatch the event.
popover.dispatchEvent(event);

将一些事件触发器注入popovers.js。具体而言,方法onPopoverHiddenshowHidePopover

var showHidePopover = function (e) {
    var popover = getPopover(e);

    if (!popover) {
      return;
    }

    popover.style.display = 'block';
    popover.offsetHeight;
    popover.classList.add('visible');

    popover.parentNode.appendChild(backdrop);

    //create event
    var event = new CustomEvent('pop', { "detail": { "id": popover.id, "type": "show" }});
    // Dispatch the event.
    popover.dispatchEvent(event);


  };

  var onPopoverHidden = function () {
    popover.style.display = 'none';
    popover.removeEventListener('webkitTransitionEnd', onPopoverHidden);
    //create event
    var event = new CustomEvent('pop', { "detail": { "id": popover.id, "type": "hide" }});
    // Dispatch the event.
    popover.dispatchEvent(event);
  };

通过将事件侦听器附加到触发事件的弹出框

来侦听弹出窗口
// Listen for the event.
document.getElementById("menu-popover")
        .addEventListener('pop', function (e) {
            console.log(e.detail.id + " " + e.detail.type);
          }, false);