Bootstrap Popover:悬停/单击并在外部单击时关闭,但恢复到悬停模式

时间:2018-08-20 17:09:18

标签: jquery twitter-bootstrap twitter-bootstrap-3

在下面的代码片段中,我有一个Bootstrap Popover,它同时支持Hover和Click模式。单击时,窗口保持打开状态,并且应在以下两种情况下关闭:(1)自单击(再次链接),或(2)任何外部单击。

问题:在外部单击后,弹出窗口进入“ 当前打开”模式。这意味着,如果您再次将鼠标悬停在其上,则窗口将被卡住并且不会消失。应该发生的是,在外部单击之后,当您再次将鼠标悬停在其上时,将还原为原始的“悬停和消失”模式。预期与在内部单击并在原始状态下单击后悬停时的行为相同。我忘了什么吗?

$('#linkPopover').popover({
  trigger: 'hover click', 
  content: 'This is my content', 
  title: 'TITLE'
});

// -----------------
// Just with the above (and no other code), the Hover and Click-Toggle 
// works within the SAME Popover window,
// but now I need to also remove the visible Popover on ANY CLICK OUTSIDE.

// However, although the below works, *after* clicking outside the Popover is in the CLICKED 
// mode i.e. it doesn't hover anymore

$('body').on('click', function(e) {
	    $('[data-toggle="popover"]').each(function () {
	        if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
              // From outside click with the popover open, need to hide
	            $(this).popover('hide');
	        }	    	
	    });  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<a id="linkPopover" data-toggle="popover">POPOVER</a>

<br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/>
End of body

3 个答案:

答案 0 :(得分:1)

此代码可能会对您有所帮助。我更改了条件语句,以检查用户是否在弹出式触发器之外单击&&如果弹出式窗口可见,然后触发弹出式触发器的click事件。

我想到了这是因为click事件是单击触发器本身时发生的事件。

$(document).ready(function(){

$('#linkPopover').popover({
  trigger: 'hover click',
  content: 'This is my content', 
  title: 'TITLE'
});

$('body').on('click', function(e) {
    $('[data-toggle="popover"]').each(function () {
        if(!$(this).is(e.target) && ($("#linkPopover").next('div.popover:visible').length)){
        $("#linkPopover").trigger('click');
    }
    });
});

});
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<a id="linkPopover" data-toggle="popover">POPOVER</a>

<br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/>
End of body
</body>

答案 1 :(得分:1)

当弹出框超过1个时,由于无限循环,我的第一个答案产生了"dd/MMM/yyyy hh:mm tt"错误(请参阅我的第一个答案和评论)。

解决此问题的方法:每当系统即将执行Maximum call stack size exceeded时,我都会在主体单击处理程序上使用.off。之后,我重新初始化主体单击处理程序。请参见下面的示例。

trigger('click')
$(document).ready(function(){

function initializeClick(){
$('body').on('click', function(e) {
    $('[data-toggle="popover"]').each(function () {
        if(!$(this).is(e.target) && ($(this).next('div.popover:visible').length)){
        $('body').off('click');
        $(this).trigger('click');
        initializeClick();
        }
    });
});
}

$('#linkPopover').popover({
  trigger: 'hover click',
  content: 'This is my content', 
  title: 'TITLE'
});

$('#linkPopover2').popover({
  trigger: 'hover click',
  content: 'This is my content', 
  title: 'TITLE'
});

$('body').on('click', function(e) {
    $('[data-toggle="popover"]').each(function () {
        if(!$(this).is(e.target) && ($(this).next('div.popover:visible').length)){
        $('body').off('click');
        $(this).trigger('click');
        initializeClick();
        }
    });
});

});

答案 2 :(得分:1)

我实际上是通过在trigger('click')中引入自定义标志参数来解决eminememinem原始答案中的无限循环(由于body.click)。您可以按照

     $('body').on('click', function (e, manualClickTriggered) {

在我们自己重新触发的特殊情况下,我们设置标志。如果是这样,我们将忽略常规逻辑。

完整摘要:

$('[id*="linkPopover"]').popover({
  trigger: 'hover click', 
  content: 'This is my content', 
  title: 'TITLE'
});

// Close any visible popover if clicked outside 
// The "manualClickTriggered" flag indicates that we initiated the special Click event ourselves in order to turn off the popover, in which case skip the regular logic.
$('body').on('click', function (e, manualClickTriggered) {
  var linkId = null;
  if (e.target.id.indexOf("linkPopover") != -1) {
    linkId = e.target.id;
  }
		
  if (manualClickTriggered != true) {
    if (e.target.id.indexOf("linkPopover") == -1) {  // Not coming from any Popover Link (outside click) 
      $('a[id^="linkPopover"][data-toggle="popover"]').each(function () {	    	
        if( !$(this).is(e.target) && ($(this).next('div.popover:visible').length )) {
          $(this).trigger('click', true);
        }
      });
    }
    else // Coming from some Popover link
    {
        $('a[id^="linkPopover"][data-toggle="popover"]').each(function () {
          if ($(this).attr('id') != linkId && $(this).next('div.popover:visible').length) {
            $(this).trigger('click', true);
          }
        });
      }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<a id="linkPopover1" data-toggle="popover">POPOVER 1</a><br/>
<a id="linkPopover2" data-toggle="popover">POPOVER 2</a><br/>
<a id="linkPopover3" data-toggle="popover">POPOVER 3</a><br/>
<a id="linkPopover4" data-toggle="popover">POPOVER 4</a><br/>

<br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/>
End of body