Popper.js: 如何在点击外部时关闭弹出窗口

Popper.js: How to close popup when clicking outside

我正在使用 Popper.js 在单击具有 class .js-share-cf-btn.[=17= 的元素时显示具有 class .js-share-cf-popover 的弹出元素]

但我希望仅在单击弹出窗口外部时关闭弹出窗口。这是我显示弹出窗口的实际代码:

var reference = $('.js-share-cf-btn');
var popover = $('.js-share-cf-popover');
popover.hide();

$(document).on('click', reference, function(e) {
  e.preventDefault();

  popover.show();

  var popper = new Popper(reference, popover, {
    placement: 'top',
  });
});

我找到了一些东西 here 但我无法让它发挥作用

这里是我的jsfiddle

您可以通过删除事件委托并使用 .is() 检查事件点击的目标来实现此目的(比较 e.target 是否等于引用按钮,否则隐藏弹出窗口)

fiddle

添加代码段作为您的代码:

还在 Popper 实例中进行了更改,您应该传递当前点击 js-share-cf-btn 因此 $(e.target) 元素

$(function() {
  var reference = $('.js-share-cf-btn');
  var popover = $('.js-share-cf-popover');
  popover.hide();

  $(document).on('click touchend', function(e) {
    var target = $(e.target);
    // ne need to reshow and recreate popper when click over popup so return;
    if(target.is(popover)) return;
    if (target.is(reference)) {
      e.preventDefault();

      popover.show();

      var popper = new Popper(target, popover, {
        placement: 'top',
      });
    }else {
      popover.hide();
    }
  });

});
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

.section {
  background: #fff;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
  margin-bottom: 20px;
}

.share-popover {
  background: red;
  color: white;
  padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<div class="section">
  <p>Section 1</p>
  <a href="#" class="js-share-cf-btn">This is the trigger</a>
</div>

<div class="section">
  <p>Section 2</p>
  <a href="#" class="js-share-cf-btn">This is the trigger</a>
</div>

<div class="section">
  <p>Section 3</p>
  <a href="#" class="js-share-cf-btn">This is the trigger</a>
</div>

<div class="share-popover js-share-cf-popover">
  This is the popup
</div>

像这样应该可以解决问题(通过在点击某处时检查目标):

$(function() {
  var ref = $('.js-share-cf-btn');
  var popover = $('.js-share-cf-popover');
  popover.hide();

  $(document).on('click', function(e) {
    var target = $(e.target);
    if (target.is(ref) || target.is(popover) ) {
      e.preventDefault();
      popover.show();
      var popper = new Popper(ref, popover, {
        placement: 'right',
      });
    }else {
      popover.hide();
    }
  });

});

https://jsfiddle.net/e8aL9tje/

对于那些使用 React 的人,我创建了一个 HOC 的要点,您可以附加到任何组件以在外部单击时关闭它:

https://gist.github.com/elie222/850bc4adede99650508aba2090cd5da1

我找到了一个非常简单的解决方案。

jQuery版本

<div class="Popper">
    <div class="stopPropagation">
        <p>Clicking on me won't close the Popper</p>
    </div>
</div>
$('.stopPropagation').on('click touchend', function(e) {
    e.stopPropagation()
})

由于点击事件不会传播到 Popper,因此点击时 Popper 不会关闭。

React 版本

<Popper>
    {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
    <div onClick={e => e.stopPropagation()} role="none">
        <p>Clicking on me won't close the Popper</p>
    </div>
</Popper>