单击时显示和隐藏工具提示 (Tipso.js)

Posted

技术标签:

【中文标题】单击时显示和隐藏工具提示 (Tipso.js)【英文标题】:Show and hide tooltip on click (Tipso.js) 【发布时间】:2020-07-28 22:02:30 【问题描述】:

我正在使用名为 Tipso 的简单工具提示插件。 如何仅通过单击显示和隐藏工具提示?

$('.top').tipso();
/* Tipso Bubble Styles */

.tipso_bubble,
.tipso_bubble>.tipso_arrow 
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;


.tipso_bubble 
  position: absolute;
  text-align: center;
  border-radius: 6px;
  z-index: 9999;


.tipso_style 
  cursor: help;
  border-bottom: 1px dotted;


.tipso_title 
  border-radius: 6px 6px 0 0;


.tipso_content 
  word-wrap: break-word;
  padding: 0.5em;



/* Tipso Bubble size classes - Similar to Foundation's syntax*/

.tipso_bubble.tiny 
  font-size: 0.6rem;


.tipso_bubble.small 
  font-size: 0.8rem;


.tipso_bubble.default 
  font-size: 1rem;


.tipso_bubble.large 
  font-size: 1.2rem;
  width: 100%;



/* Tipso Bubble Div */

.tipso_bubble>.tipso_arrow 
  position: absolute;
  width: 0;
  height: 0;
  border: 8px solid;
  pointer-events: none;


.tipso_bubble.top>.tipso_arrow 
  border-top-color: #000;
  border-right-color: transparent;
  border-left-color: transparent;
  border-bottom-color: transparent;
  top: 100%;
  left: 50%;
  margin-left: -8px;


.tipso_bubble.bottom>.tipso_arrow 
  border-bottom-color: #000;
  border-right-color: transparent;
  border-left-color: transparent;
  border-top-color: transparent;
  bottom: 100%;
  left: 50%;
  margin-left: -8px;


.tipso_bubble.left>.tipso_arrow 
  border-left-color: #000;
  border-top-color: transparent;
  border-bottom-color: transparent;
  border-right-color: transparent;
  top: 50%;
  left: 100%;
  margin-top: -8px;


.tipso_bubble.right>.tipso_arrow 
  border-right-color: #000;
  border-top-color: transparent;
  border-bottom-color: transparent;
  border-left-color: transparent;
  top: 50%;
  right: 100%;
  margin-top: -8px;


.tipso_bubble .top_right_corner,
.tipso_bubble.top_right_corner 
  border-bottom-left-radius: 0;


.tipso_bubble .bottom_right_corner,
.tipso_bubble.bottom_right_corner 
  border-top-left-radius: 0;


.tipso_bubble .top_left_corner,
.tipso_bubble.top_left_corner 
  border-bottom-right-radius: 0;


.tipso_bubble .bottom_left_corner,
.tipso_bubble.bottom_left_corner 
  border-top-right-radius: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://tipso.object505.com/tipso.js"></script>


<div id="banner-message">
  <p>Hello World</p>
  <span class="top tipso_style" data-tipso="This is a tooltip text">I want to show the tooltip on click and hide it on click as well.</span>
</div>

View on jsFiddle

【问题讨论】:

另外,Tooltipster 看起来非常健壮并支持click/touch triggering。 我确实先尝试过,但我没有注意到它有 minWidth 和其他样式选项。现在我正在考虑切换回去 :) 再次感谢您的帮助! 【参考方案1】:

考虑添加一个click 处理程序,根据工具提示是否已经显示来显示或隐藏它。 Tipso documentation 建议使用类来指示工具提示何时显示;请参阅标题为“单击以显示/隐藏提示”的演示。

这是一个切换工具提示的点击事件的演示。它还包括 mouseentermouseleave 处理程序以更新“显示”类。

$('.top')
  .tipso()
  .on(
    mouseenter: function(e) 
      jQuery(this).addClass('typso-showing');
    ,
    mouseleave: function(e) 
      jQuery(this).removeClass('typso-showing');
    ,
    click: function(e) 
      let $this = jQuery(this);
      if ($this.hasClass('typso-showing')) 
        $this.removeClass('typso-showing');
        $this.tipso('hide');
       else 
        $this.addClass('typso-showing');
        $this.tipso('show');
      
    
  );
/* Tipso Bubble Styles */

.tipso_bubble,
.tipso_bubble>.tipso_arrow 
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;


.tipso_bubble 
  position: absolute;
  text-align: center;
  border-radius: 6px;
  z-index: 9999;


.tipso_style 
  cursor: help;
  border-bottom: 1px dotted;


.tipso_title 
  border-radius: 6px 6px 0 0;


.tipso_content 
  word-wrap: break-word;
  padding: 0.5em;



/* Tipso Bubble size classes - Similar to Foundation's syntax*/

.tipso_bubble.tiny 
  font-size: 0.6rem;


.tipso_bubble.small 
  font-size: 0.8rem;


.tipso_bubble.default 
  font-size: 1rem;


.tipso_bubble.large 
  font-size: 1.2rem;
  width: 100%;



/* Tipso Bubble Div */

.tipso_bubble>.tipso_arrow 
  position: absolute;
  width: 0;
  height: 0;
  border: 8px solid;
  pointer-events: none;


.tipso_bubble.top>.tipso_arrow 
  border-top-color: #000;
  border-right-color: transparent;
  border-left-color: transparent;
  border-bottom-color: transparent;
  top: 100%;
  left: 50%;
  margin-left: -8px;


.tipso_bubble.bottom>.tipso_arrow 
  border-bottom-color: #000;
  border-right-color: transparent;
  border-left-color: transparent;
  border-top-color: transparent;
  bottom: 100%;
  left: 50%;
  margin-left: -8px;


.tipso_bubble.left>.tipso_arrow 
  border-left-color: #000;
  border-top-color: transparent;
  border-bottom-color: transparent;
  border-right-color: transparent;
  top: 50%;
  left: 100%;
  margin-top: -8px;


.tipso_bubble.right>.tipso_arrow 
  border-right-color: #000;
  border-top-color: transparent;
  border-bottom-color: transparent;
  border-left-color: transparent;
  top: 50%;
  right: 100%;
  margin-top: -8px;


.tipso_bubble .top_right_corner,
.tipso_bubble.top_right_corner 
  border-bottom-left-radius: 0;


.tipso_bubble .bottom_right_corner,
.tipso_bubble.bottom_right_corner 
  border-top-left-radius: 0;


.tipso_bubble .top_left_corner,
.tipso_bubble.top_left_corner 
  border-bottom-right-radius: 0;


.tipso_bubble .bottom_left_corner,
.tipso_bubble.bottom_left_corner 
  border-top-right-radius: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://tipso.object505.com/tipso.js"></script>

<div id="banner-message">
  <p>Hello World</p>
  <span class="top tipso_style" data-tipso="This is a tooltip text">I want to show the tooltip on click and hide it on click as well.</span>
</div>

您似乎想删除 Tipso 的默认悬停行为,以便工具提示在点击时显示。一种方法是使用off() 删除 Typso 的处理程序,这些处理程序似乎用于“mouseover”和“mouseout”事件。然后添加您自己的点击处理程序。

// define all tips
let $tips = $('.top');

// initialize tipso and configure handlers
$tips
  .tipso()
  .off('mouseover mouseout')
  .on('click', function() 

    let $this = $(this);
    let showing = $this.hasClass('typso-showing');

    $this
      .tipso(showing ? 'hide' : 'show')
      .toggleClass('typso-showing', !showing);

  );

// close on click outside
$(document).on('click', function(e) 
  if (!$tips.is(e.target)) 
    $tips.tipso('hide').removeClass('typso-showing');
  
);
/* Tipso Bubble Styles */

.tipso_bubble,
.tipso_bubble>.tipso_arrow 
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;


.tipso_bubble 
  position: absolute;
  text-align: center;
  border-radius: 6px;
  z-index: 9999;


.tipso_style 
  cursor: help;
  border-bottom: 1px dotted;


.tipso_title 
  border-radius: 6px 6px 0 0;


.tipso_content 
  word-wrap: break-word;
  padding: 0.5em;



/* Tipso Bubble size classes - Similar to Foundation's syntax*/

.tipso_bubble.tiny 
  font-size: 0.6rem;


.tipso_bubble.small 
  font-size: 0.8rem;


.tipso_bubble.default 
  font-size: 1rem;


.tipso_bubble.large 
  font-size: 1.2rem;
  width: 100%;



/* Tipso Bubble Div */

.tipso_bubble>.tipso_arrow 
  position: absolute;
  width: 0;
  height: 0;
  border: 8px solid;
  pointer-events: none;


.tipso_bubble.top>.tipso_arrow 
  border-top-color: #000;
  border-right-color: transparent;
  border-left-color: transparent;
  border-bottom-color: transparent;
  top: 100%;
  left: 50%;
  margin-left: -8px;


.tipso_bubble.bottom>.tipso_arrow 
  border-bottom-color: #000;
  border-right-color: transparent;
  border-left-color: transparent;
  border-top-color: transparent;
  bottom: 100%;
  left: 50%;
  margin-left: -8px;


.tipso_bubble.left>.tipso_arrow 
  border-left-color: #000;
  border-top-color: transparent;
  border-bottom-color: transparent;
  border-right-color: transparent;
  top: 50%;
  left: 100%;
  margin-top: -8px;


.tipso_bubble.right>.tipso_arrow 
  border-right-color: #000;
  border-top-color: transparent;
  border-bottom-color: transparent;
  border-left-color: transparent;
  top: 50%;
  right: 100%;
  margin-top: -8px;


.tipso_bubble .top_right_corner,
.tipso_bubble.top_right_corner 
  border-bottom-left-radius: 0;


.tipso_bubble .bottom_right_corner,
.tipso_bubble.bottom_right_corner 
  border-top-left-radius: 0;


.tipso_bubble .top_left_corner,
.tipso_bubble.top_left_corner 
  border-bottom-right-radius: 0;


.tipso_bubble .bottom_left_corner,
.tipso_bubble.bottom_left_corner 
  border-top-right-radius: 0;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://tipso.object505.com/tipso.js"></script>

<div id="banner-message">
  <p>Hello World</p>
  <span class="top tipso_style" data-tipso="This is a tooltip text">I want to show the tooltip on click and hide it on click as well.</span>
</div>

【讨论】:

是的,两者都有。如果没有悬停,我想知道你为什么要使用 Tipso。 由于样式功能,我在工具提示中使用了图像。我应该使用其他东西吗? 不,没关系;我只是好奇而已。我已经编辑了我的答案以删除默认的悬停行为。 非常感谢!最后一件事 - 是否有可能通过点击任何地方来隐藏工具提示?所以当我点击链接时它会打开,但当我点击页面上的任何地方时它会隐藏。 已更新。另见Use jQuery to hide a DIV when the user clicks outside of it,【参考方案2】:

此外,如果在单击第一个工具提示之外单击另一个工具提示,如何清除一个工具提示?

Example on jsfiddle 表示要解决的问题。

所有代码都与上面的showdev相同,除了html ...

<span class="top tipso_style" data-tipso="Tooltip one">One for tooltip</span>
<br><br>
<br><br>
<span class="top tipso_style" data-tipso="Tooltip two">Two for tooltip></span>

编辑:

https://jsfiddle.net/j4r3zybg/3/$tips.tipso('hide')let showing 后面加上$tips.tipso('hide') 怎么样?

let $this = $(this);
let showing = $this.hasClass('typso-showing');
$tips.tipso('hide')  // close all, prevent two showing

不,几乎。用户现在无法轻松复制文本,除非该工具提示在他们可以执行 ctrl-c 之前消失。

【讨论】:

以上是关于单击时显示和隐藏工具提示 (Tipso.js)的主要内容,如果未能解决你的问题,请参考以下文章

引导工具提示在点击时显示,但在鼠标移出时隐藏

滚动时显示和隐藏 Div

jQuery:悬停时显示和隐藏子div

隐藏 TabBar 并在按钮单击时显示 NavigationController 工具栏

extjs4 在按钮单击而不是鼠标悬停时显示工具提示

在PHP文件中实现javascript时显示/隐藏功能不起作用[关闭]