BootStrap Tooltip插件源码解析

yizhihongxing

BootStrap Tooltip插件源码解析

Bootstrap Tooltip插件是一个简单易用,且对用户友好的Web开发工具,其基于jQuery javascript library实现,可以加快你的Web开发速度。

Tooltip插件的基本用法

在需要使用Tooltip的HTML代码元素上添加"data-toggle"和 "data-placement"属性,其中 "data-toggle"属性的值为 "tooltip", "data-placement"属性设置Tooltip显示位置,例如 "top", "bottom", "left" 或 "right"。

<button type="button" class="btn" data-toggle="tooltip" data-placement="top" title="This is the tooltip text">Top</button>

当用户把鼠标移动到按钮上时,会弹出一个Tooltip提示框,内容为 "This is the tooltip text"。

Tooltip选项设置

除了基本的用法,Bootstrap Tooltip插件还支持一些可配置的选项,包括:animation(动画)、delay(延迟)、html(HTML内容)、selector(选择器)、template(模板)、title(标题)和 trigger(触发方式)。

以下是一个例子,演示组合样式的设置:

<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="top" title="This is the tooltip text" data-animation="true" data-delay="500" data-html="true" data-selector=".btn" data-template="<div class='tooltip' role='tooltip'><div class='tooltip-arrow'></div><div class='tooltip-inner'></div></div>" data-trigger="focus">Hover over me</button>

源码解析

Bootstrap Tooltip插件的源码基于jQuery的事件绑定实现,插件实现的主要工作包括,绑定事件监听、创建与隐藏提示框元素、采用CSS布局及计算提示框位置等。

具体实现过程如下:

  1. 绑定事件监听:插件通过jQuery的事件绑定接口,订阅目标Element的需要监听的事件,例如"mouseenter","mousemove"或"click"等。

this._$element
.on('mouseenter' + eventType + " " + 'focus' + eventType, $.proxy(this.enter, this))
.on('mouseleave' + eventType + " " + 'blur' + eventType, $.proxy(this.leave, this));

  1. 创建与隐藏提示框元素:

插件采用了下面方法创建Tooltip HTML元素及内容,并把元素添加到body中。插件隐藏Tooltip元素的方法是设置其可见性属性为"hidden",而非使用CSS类名样式控制元素的可见/隐藏。

```
Tooltip.prototype.show = function () {
// 创建Tooltip元素
var $tip = this.tip(),
// 获取内容
title = this.getTitle();

   $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title);
   $tip.removeClass('fade in');

   // 获取显示位置
   var placement = typeof this.options.placement == 'function' ?
       this.options.placement.call(this, $tip[0], this.$element[0]) :
       this.options.placement,

       // 计算元素位置
       pos = this.getPosition(),

       // 获取元素宽度和高度
       actualWidth = $tip[0].offsetWidth,
       actualHeight = $tip[0].offsetHeight,

       // 获取箭头元素
       tp = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight);

   // 设置箭头位置及可见性
   this.applyPlacement(tp, placement);
   this.$element.trigger('shown.bs.' + this.type);

};

Tooltip.prototype.hide = function() {
// 隐藏Tooltip元素
var that = this,
$tip = this.tip(),
e = $.Event('hide.bs.' + this.type);

   function complete() {
       if(that.hoverState != 'in') $tip.detach();
   }

   this.$element.removeAttr('aria-describedby');
   this.$element.trigger(e);

   if(e.isDefaultPrevented()) return;

   $tip.removeClass('in');

   $.support.transition && $tip.hasClass('fade') ?
       $tip
       .one('bsTransitionEnd', complete)
       .emulateTransitionEnd(Tooltip.TRANSITION_DURATION) :
       complete();

   this.hoverState = null;
   return this;

};
```

注意到,为了稍后准确计算Tooltip位置及大小,先显示show()该元素,然后再利用$tip.offsetWidth和$tip.offsetHeight来得到元素的实际大小。(ToolTip.TRANSITION_DURATION 为 150ms)

  1. 采用CSS布局及计算位置:

Tooltip插件基于CSS布局来横向与纵向定位Tooltip元素的坐标位置,其算法使用了CSS3的位移函数,代码如下:

```
Tooltip.prototype.applyPlacement = function(offset, placement) {
var $tip = this.tip(),
width = $tip[0].offsetWidth,
height = $tip[0].offsetHeight;

   // manually read margins because getBoundingClientRect includes difference
   var marginTop = parseInt($tip.css('margin-top'), 10),
       marginLeft = parseInt($tip.css('margin-left'), 10);

   // we must check for NaN for ie 8/9
   if (isNaN(marginTop))  marginTop  = 0;
   if (isNaN(marginLeft)) marginLeft = 0;

   offset.top  += marginTop;
   offset.left += marginLeft;

   $.offset.setOffset($tip[0], $.extend({
       using: function(props) {
           $tip.css({
               top: Math.round(props.top),
               left: Math.round(props.left)
           });
       }
   }, offset), 0);

   $tip.addClass('in');

   // check to see if placing tip in new offset caused the tip to resize itself
   var actualWidth  = $tip[0].offsetWidth,
       actualHeight = $tip[0].offsetHeight;

   if (placement == 'top' && actualHeight != height) {
       offset.top = offset.top + height - actualHeight;
   }

   var delta = this.getViewportAdjustedDelta(placement, offset, actualWidth, actualHeight);

   if (delta.left) offset.left += delta.left;
   else offset.top += delta.top;

   var isVertical        = /top|bottom/.test(placement),
       arrowDelta        = isVertical ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight,
       arrowOffsetPosition = isVertical ? 'offsetWidth' : 'offsetHeight';

   $tip.offset(offset);

   this.replaceArrow(arrowDelta, $tip[0][arrowOffsetPosition], isVertical);

};
```

以上就是Bootstrap Tooltip插件的基本用法和使用方法,以及其实现过程的源码解析,具体使用时应根据需求选用其标准配置或自定义配置,最大程度发挥该插件的好处。

示例说明

样例1:

显示一个带动画效果的提示框, 当鼠标光标放在按钮上时触发。

<button type="button" class="btn btn-lg btn-danger" data-toggle="tooltip" data-animation="true" data-placement="top" data-delay='{"show":1000, "hide":100}' title="Hello, World!">Danger button</button>

样例2:

鼠标移动到按钮上时触发一个带HTML内容的提示框。

<button type="button" class="btn btn-default" data-toggle="tooltip" data-html="true" data-placement="right" title="<em>This</em> <u>is</u> <b>a</b> <i>tooltip</i>">Hover over me</button>

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:BootStrap Tooltip插件源码解析 - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • CSS——float属性及Clear:both备忘笔记

    CSS——float属性及Clear:both备忘笔记 float属性 float属性用于控制元素的浮动方向,可以让元素向左或向右浮动,但是要注意的是浮动的元素会脱离文档流,对其他元素的布局会有一定的影响。 语法: float: none | left | right; none:默认值,元素不进行浮动。 left:使元素向左浮动。 right:使元素向右浮…

    css 2023年6月10日
    00
  • vue中使用定义好的变量设置css样式详解

    在 Vue 中,我们可以使用定义好的变量来设置 CSS 样式。下面是一个完整的攻略,包含了如何在 Vue 中使用定义好的变量设置 CSS 样式的过程和两个示例说明。 在 Vue 中使用定义好的变量设置 CSS 样式 1. 定义变量 首先,我们需要在 Vue 中定义变量。我们可以在 data 中定义变量,也可以在 computed 中定义变量。下面是一个示例:…

    css 2023年5月18日
    00
  • 一款利用纯css3实现的超炫3D表单的实例教程

    一款利用纯CSS3实现的超炫3D表单的实例教程 简介 利用CSS3可以实现很炫酷的效果,本篇教程将介绍如何利用CSS3实现一个超炫3D表单。 教程步骤 1. HTML代码 先看看我们要实现的表单的大体结构: <form> <fieldset> <label>用户名:</label> <input type…

    css 2023年6月10日
    00
  • 纯CSS打造的导航菜单(附jquery版)

    好的。在这里,我将为您详细讲解“纯CSS打造的导航菜单(附jquery版)”的完整攻略。该攻略包含了该导航菜单的制作过程以及如何将其转化成jquery版的导航菜单。 制作纯CSS导航菜单 第一步:HTML结构 首先,我们需要创建一个含有ul和li元素的HTML结构。 代码如下: <nav> <ul> <li><a h…

    css 2023年6月9日
    00
  • CSS伪元素before、after设置特殊效果:制作时尚焦点图相框

    CSS伪元素是指通过CSS选择器在一个元素的前面或者后面添加一个虚拟的元素,用于对该元素的部分内容进行样式的设置或者添加一些特殊效果。 在本篇攻略中,我们将详细讲解如何使用CSS伪元素before、after制作时尚焦点图相框。操作步骤如下: 1. 创建HTML结构 首先我们需要按照以下的HTML结构创建一个基本框架: <div class=&quot…

    css 2023年6月9日
    00
  • css实现动态阴影、蚀刻文本、渐变文本效果

    CSS实现动态阴影、蚀刻文本、渐变文本效果的攻略如下: 动态阴影 动态阴影效果可以通过CSS3中的box-shadow属性实现,结合:hover伪类使其出现动态变化。具体实现步骤如下: 定义一个带有box-shadow属性的元素,可以设置阴影的颜色、位置、大小和扩散程度。例如: .box { width: 200px; height: 200px; box-…

    css 2023年6月9日
    00
  • jQuery修改class属性和CSS样式整理

    下面我来详细讲解一下“jQuery修改class属性和CSS样式整理”的完整攻略: 修改class属性 addClass()方法 我们可以通过addClass()方法给一个元素添加一个或多个class属性,示例代码如下: $(‘h1’).addClass(‘title big’); 上述代码就给所有<h1>标签添加了title和big两个clas…

    css 2023年6月10日
    00
  • CSS 网页表单实现鼠标悬停交互效果

    下面我将为您详细讲解“CSS 网页表单实现鼠标悬停交互效果”的完整攻略。 什么是鼠标悬停交互效果 在网页设计中,鼠标悬停交互效果是一种用户界面设计技术,它使用户在鼠标悬停在页面元素上时产生视觉反馈,从而增强了用户体验和导航性。鼠标悬停交互效果可以应用在各种网页元素上,其中最常见的是应用在网页表单中。 如何实现鼠标悬停交互效果 在实现鼠标悬停交互效果的过程中,…

    css 2023年6月10日
    00
合作推广
合作推广
分享本页
返回顶部