Tooltips

Tooltips(ツールチップ)を追加するためのドキュメントと例です。

Overview

ツールチップを使用するときに知っておくべきことは下記です。:

  • ツールチップの位置は Popper.js に依存します。 popper.min.js を bootstrap.jsの前におくか, Popper.jsを含む bootstrap.bundle.min.js / bootstrap.bundle.js を使用する必要があります。
  • JavaScriptをソースから構築するには requires util.js を使用します。
  • ツールチップはパフォーマンスの理由でオプトイン(任意)で取得されるため, 初期化する必要があります。
  • 長さが0のタイトルのツールチップは表示されません。
  • container: 'body' を指定すると, 複雑なコンポーネント(インプットグループやボタングループなど)のレンダリングの問題が回避可能です。
  • 隠された要素のツールチップをトリガーすることはできません。
  • .disabled または disabled 要素のツールチップはそれを囲む要素で起動する必要があります。
  • 複数の行にまたがるハイパーリンクからトリガされると, ツールチップが中央に配置されます。この動作を避けるために <a>white-space: nowrap; をラップしてください。
  • ツールチップは, 対応する要素がDOMから削除される前に非表示にする必要があります。

下記の例をみてください。

Example: Enable tooltips everywhere

ツールチップを初期化する方法の1つは data-toggle 属性で選択することです。

$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})

Examples

ツールチップを表示するには, 以下のリンクをホバーしてください。

Tight pants next level keffiyeh you probably haven't heard of them. Photo booth beard raw denim letterpress vegan messenger bag stumptown. Farm-to-table seitan, mcsweeney's fixie sustainable quinoa 8-bit american apparel have a terry richardson vinyl chambray. Beard stumptown, cardigans banh mi lomo thundercats. Tofu biodiesel williamsburg marfa, four loko mcsweeney's cleanse vegan chambray. A really ironic artisan whatever keytar, scenester farm-to-table banksy Austin twitter handle freegan cred raw denim single-origin coffee viral.

下記のボタンをホバーすると上, 右, 下, 左の4つの例がみれます。

<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="top" title="Tooltip on top">
  Tooltip on top
</button>
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="right" title="Tooltip on right">
  Tooltip on right
</button>
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">
  Tooltip on bottom
</button>
<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-placement="left" title="Tooltip on left">
  Tooltip on left
</button>

And with custom HTML added:

<button type="button" class="btn btn-secondary" data-toggle="tooltip" data-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">
  Tooltip with HTML
</button>

Usage

ツールチップは, コンテンツとマークアップを生成します。デフォルトでは, ツールチップはトリガー要素の後に配置されます。

JavaScript経由でツールチップをトリガします。

$('#example').tooltip(options)
Overflow auto and scroll

Tooltip position attempts to automatically change when a parent container has overflow: auto or overflow: scroll like our .table-responsive, but still keeps the original placement’s positioning. To resolve, set the boundary option to anything other than default value, 'scrollParent', such as 'window':

$('#example').tooltip({ boundary: 'window' })

Markup

ツールチップに必要なマークアップは data 属性と HTML要素の title だけです。 生成されたツールチップのマークアップは位置が必要です(デフォルトでは, プラグインによって top に設定されています)。

Making tooltips work for keyboard and assistive technology users

You should only add tooltips to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form controls). Although arbitrary HTML elements (such as <span>s) can be made focusable by adding the tabindex="0" attribute, this will add potentially annoying and confusing tab stops on non-interactive elements for keyboard users. In addition, most assistive technologies currently do not announce the tooltip in this situation.

Additionally, do not rely solely on hover as the trigger for your tooltip, as this will make your tooltips impossible to trigger for keyboard users.

<!-- HTML to write -->
<a href="#" data-toggle="tooltip" title="Some tooltip text!">Hover over me</a>

<!-- Generated markup by the plugin -->
<div class="tooltip bs-tooltip-top" role="tooltip">
  <div class="arrow"></div>
  <div class="tooltip-inner">
    Some tooltip text!
  </div>
</div>

Disabled elements

disabled 属性をもつ要素はインタラクティブではないので ユーザーがフォーカス , ホバー, クリックしてツールチップ(またはポップオーバー)を起動することはできません。

As a workaround, you’ll want to trigger the tooltip from a wrapper <div> or <span>, ideally made keyboard-focusable using tabindex="0", and override the pointer-events on the disabled element.

回避策として tabindex="0" を適用して <div> または <span> をラッパーしてツールチップを起動し, 無効化された要素の pointer-events を上書きする必要があります。

<span class="d-inline-block" tabindex="0" data-toggle="tooltip" title="Disabled tooltip">
  <button class="btn btn-primary" style="pointer-events: none;" type="button" disabled>Disabled button</button>
</span>

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-animation=""`. -->

オプションは, データ属性またはJavaScriptを使用して渡すことができます。 データ属性の場合 data- にオプション名を適用します( data-animation =" " のように)

Name Type Default Description
animation boolean true Apply a CSS fade transition to the tooltip
container string | element | false false

Appends the tooltip to a specific element. Example: container: 'body'. This option is particularly useful in that it allows you to position the tooltip in the flow of the document near the triggering element - which will prevent the tooltip from floating away from the triggering element during a window resize.

delay number | object 0

Delay showing and hiding the tooltip (ms) - does not apply to manual trigger type

If a number is supplied, delay is applied to both hide/show

Object structure is: delay: { "show": 500, "hide": 100 }

html boolean false

Allow HTML in the tooltip.

If true, HTML tags in the tooltip's title will be rendered in the tooltip. If false, jQuery's text method will be used to insert content into the DOM.

Use text if you're worried about XSS attacks.

placement string | function 'top'

How to position the tooltip - auto | top | bottom | left | right.
When auto is specified, it will dynamically reorient the tooltip.

When a function is used to determine the placement, it is called with the tooltip DOM node as its first argument and the triggering element DOM node as its second. The this context is set to the tooltip instance.

selector string | false false If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to enable dynamic HTML content to have popovers added. See this and an informative example.
template string '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>'

Base HTML to use when creating the tooltip.

The tooltip's title will be injected into the .tooltip-inner.

.arrow will become the tooltip's arrow.

The outermost wrapper element should have the .tooltip class and role="tooltip".

title string | element | function ''

Default title value if title attribute isn't present.

If a function is given, it will be called with its this reference set to the element that the tooltip is attached to.

trigger string 'hover focus'

How tooltip is triggered - click | hover | focus | manual. You may pass multiple triggers; separate them with a space.

'manual' indicates that the tooltip will be triggered programmatically via the .tooltip('show'), .tooltip('hide') and .tooltip('toggle') methods; this value cannot be combined with any other trigger.

'hover' on its own will result in tooltips that cannot be triggered via the keyboard, and should only be used if alternative methods for conveying the same information for keyboard users is present.

offset number | string 0 Offset of the tooltip relative to its target. For more information refer to Popper.js's offset docs.
fallbackPlacement string | array 'flip' Allow to specify which position Popper will use on fallback. For more information refer to Popper.js's behavior docs
boundary string | element 'scrollParent' Overflow constraint boundary of the tooltip. Accepts the values of 'viewport', 'window', 'scrollParent', or an HTMLElement reference (JavaScript only). For more information refer to Popper.js's preventOverflow docs.

Data attributes for individual tooltips

個々のツールチップのオプションはデータ属性を使用して指定するできます。

方法(Methods)

Asynchronous methods and transitions

All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.

See our JavaScript documentation for more information.

$().tooltip(options)

要素にツールチップハンドラーをつけます。

.tooltip('show')

ツールチップを表示します。ツールチップが表示される前( shown.bs.tooltip の発動前)に呼び出し元に戻ります。 これは, ツールチップの “manual” トリガーとみなされる。タイトルの長さが0のツールチップは表示されません。

$('#element').tooltip('show')

.tooltip('hide')

ツールチップを非表示にする。ツールチップが非表示になる前( hidden.bs.tooltip の前)に呼び出し元に戻ります。 ツールチップの “manual” トリガーとみなされます。

$('#element').tooltip('hide')

.tooltip('toggle')

ツールチップを切り替えます。ツールチップが表示/非表示になる前( shown.bs.tooltip , hidden.bs.tooltip の発動前)に呼び出し元に戻ります。 ツールチップの “manual” トリガーとみなされる。

$('#element').tooltip('toggle')

.tooltip('dispose')

ツールチップを非表示にしたり破棄したりします。 the selector option を使用して作成されます。 デリゲートを使用するツールチップは, 子孫トリガー要素で個別に破棄できません。

$('#element').tooltip('dispose')

.tooltip('enable')

ツールチップに表示する機能を与えます。デフォルトは有効です。

$('#element').tooltip('enable')

.tooltip('disable')

ツールチップを表示する機能を削除します。ヒントが再度有効になっている場合にのみ, ツールチップを表示することができます。

$('#element').tooltip('disable')

.tooltip('toggleEnabled')

ツールチップの表示/非表示にするかを切り替えます。

$('#element').tooltip('toggleEnabled')

.tooltip('update')

ツールチップの位置を更新します。

$('#element').tooltip('update')

Events

Event Type Description
show.bs.tooltip This event fires immediately when the show instance method is called.
shown.bs.tooltip This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete).
hide.bs.tooltip This event is fired immediately when the hide instance method has been called.
hidden.bs.tooltip This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.bs.tooltip This event is fired after the show.bs.tooltip event when the tooltip template has been added to the DOM.
$('#myTooltip').on('hidden.bs.tooltip', function () {
  // do something…
})

v4.2