Ale nan kontni prensipal la Ale nan navigasyon dokiman yo
Check
in English

Konsèy zouti

Dokimantasyon ak egzanp pou ajoute konsèy koutim Bootstrap ak CSS ak JavaScript lè l sèvi avèk CSS3 pou animasyon ak done-bs-atribi pou estoke tit lokal yo.

Apèsi sou lekòl la

Bagay ou dwe konnen lè w ap itilize plugin tooltip la:

  • Tooltips konte sou twazyèm pati bibliyotèk Popper pou pwezante. Ou dwe mete popper.min.js anvan bootstrap.js, oswa itilize youn bootstrap.bundle.min.jski gen Popper.
  • Ti konsèy yo patisipe pou rezon pèfòmans, kidonk ou dwe inisyalize yo tèt ou .
  • Konsèy zouti ak tit zewo-longè pa janm parèt.
  • Espesifye container: 'body'pou evite rann pwoblèm nan eleman ki pi konplèks (tankou gwoup opinyon nou yo, gwoup bouton, elatriye).
  • Deklanche konsèy sou eleman kache pa pral travay.
  • Konsèy zouti pou .disabledoswa disabledeleman yo dwe deklanche sou yon eleman wrapper.
  • Lè yo deklanche soti nan lyen ipèr ki kouvri plizyè liy, konsèy zouti yo pral santre. Sèvi ak white-space: nowrap;sou ou <a>pou evite konpòtman sa a.
  • Ti konsèy yo dwe kache anvan eleman korespondan yo te retire nan DOM la.
  • Konsèy zouti yo ka deklanche gras a yon eleman andedan yon DOM lonbraj.

Gen tout sa? Gwo, ann wè ki jan yo travay ak kèk egzanp.

Pa default, eleman sa a sèvi ak dezenfektan kontni entegre, ki retire nenpòt eleman HTML ki pa klèman pèmèt. Gade seksyon dezenfektan nan dokiman JavaScript nou an pou plis detay.
Efè animasyon eleman sa a depann de prefers-reduced-motionrechèch medya yo. Gade seksyon mouvman redui nan dokiman aksè nou an .

Egzanp yo

Pèmèt konsèy sou zouti

Kòm mansyone pi wo a, ou dwe inisyalize konsèy zouti anvan yo ka itilize yo. Youn nan fason yo inisyalize tout konsèy sou yon paj ta dwe chwazi yo pa data-bs-toggleatribi yo, tankou sa a:

const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))

Pase sou lyen ki anba yo pou wè konsèy sou zouti:

Tèks anplasman pou montre kèk lyen an liy ak konsèy sou zouti. Sa a se kounye a jis filler, pa gen okenn asasen. Kontni mete isit la jis imite prezans nan tèks reyèl . Ak tout sa jis pou ba ou yon lide sou ki jan konsèy zouti ta gade lè yo itilize nan sitiyasyon reyèl. Se konsa, èspere ke ou te kounye a wè ki jan konsèy sa yo sou lyen ka travay nan pratik, yon fwa ou itilize yo sou pwòp sit ou oswa pwojè.

html
<p class="muted">Placeholder text to demonstrate some <a href="#" data-bs-toggle="tooltip" data-bs-title="Default tooltip">inline links</a> with tooltips. This is now just filler, no killer. Content placed here just to mimic the presence of <a href="#" data-bs-toggle="tooltip" data-bs-title="Another tooltip">real text</a>. And all that just to give you an idea of how tooltips would look when used in real-world situations. So hopefully you've now seen how <a href="#" data-bs-toggle="tooltip" data-bs-title="Another one here too">these tooltips on links</a> can work in practice, once you use them on <a href="#" data-bs-toggle="tooltip" data-bs-title="The last tip!">your own</a> site or project.
</p>
Ou lib pou itilize swa titleoswa data-bs-titlenan HTML ou. Lè titleyo itilize, Popper pral ranplase li otomatikman ak data-bs-titlelè eleman an rann.

Konsèy zouti koutim

Te ajoute nan v5.2.0

Ou ka pèsonalize aparans konsèy zouti yo lè l sèvi avèk varyab CSS . Nou mete yon klas koutim ak data-bs-custom-class="custom-tooltip"dimansyon aparans koutim nou an epi sèvi ak li pou pase sou plas yon varyab CSS lokal yo.

.custom-tooltip {
  --bs-tooltip-bg: var(--bs-primary);
}
html
<button type="button" class="btn btn-secondary"
        data-bs-toggle="tooltip" data-bs-placement="top"
        data-bs-custom-class="custom-tooltip"
        data-bs-title="This top tooltip is themed via CSS variables.">
  Custom tooltip
</button>

Direksyon

Pase sou bouton ki anba yo pou wè kat direksyon konsèy yo: anwo, adwat, anba, ak agoch. Enstriksyon yo reflete lè w ap itilize Bootstrap nan RTL.

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

Epi ak HTML koutim te ajoute:

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

Avèk yon SVG:

CSS

Varyab

Te ajoute nan v5.2.0

Kòm yon pati nan apwòch Bootstrap a evolye varyab CSS, konsèy sou zouti kounye a itilize varyab CSS lokal yo .tooltippou amelyore personnalisation an tan reyèl. Valè pou varyab CSS yo mete atravè Sass, kidonk personnalisation Sass toujou sipòte tou.

  --#{$prefix}tooltip-zindex: #{$zindex-tooltip};
  --#{$prefix}tooltip-max-width: #{$tooltip-max-width};
  --#{$prefix}tooltip-padding-x: #{$tooltip-padding-x};
  --#{$prefix}tooltip-padding-y: #{$tooltip-padding-y};
  --#{$prefix}tooltip-margin: #{$tooltip-margin};
  @include rfs($tooltip-font-size, --#{$prefix}tooltip-font-size);
  --#{$prefix}tooltip-color: #{$tooltip-color};
  --#{$prefix}tooltip-bg: #{$tooltip-bg};
  --#{$prefix}tooltip-border-radius: #{$tooltip-border-radius};
  --#{$prefix}tooltip-opacity: #{$tooltip-opacity};
  --#{$prefix}tooltip-arrow-width: #{$tooltip-arrow-width};
  --#{$prefix}tooltip-arrow-height: #{$tooltip-arrow-height};
  

Sass varyab

$tooltip-font-size:                 $font-size-sm;
$tooltip-max-width:                 200px;
$tooltip-color:                     $white;
$tooltip-bg:                        $black;
$tooltip-border-radius:             $border-radius;
$tooltip-opacity:                   .9;
$tooltip-padding-y:                 $spacer * .25;
$tooltip-padding-x:                 $spacer * .5;
$tooltip-margin:                    null; // TODO: remove this in v6

$tooltip-arrow-width:               .8rem;
$tooltip-arrow-height:              .4rem;
// fusv-disable
$tooltip-arrow-color:               null; // Deprecated in Bootstrap 5.2.0 for CSS variables
// fusv-enable

Itilizasyon

Plugin konsèy zouti a jenere kontni ak mak sou demann, epi pa default mete konsèy zouti apre eleman deklanche yo.

Deklanche konsèy zouti a atravè JavaScript:

const exampleEl = document.getElementById('example')
const tooltip = new bootstrap.Tooltip(exampleEl, options)
Debòde autoakscroll

Pozisyon Tooltip eseye chanje otomatikman lè yon veso paran gen overflow: autooswa overflow: scrollrenmen nou an .table-responsive, men li toujou kenbe pozisyon plasman orijinal la. Pou rezoud sa a, mete boundaryopsyon a (pou modifye a baskile lè l sèvi avèk popperConfigopsyon a) nan nenpòt HTMLElement pou pase sou valè default la, 'clippingParents', tankou document.body:

const tooltip = new bootstrap.Tooltip('#example', {
  boundary: document.body // or document.querySelector('#boundary')
})

Marke

Marke ki nesesè pou yon konsèy zouti se sèlman yon dataatribi epi titlesou eleman HTML ou vle gen yon konsèy. Markup pwodwi a nan yon konsèy zouti se pito senp, menm si li mande pou yon pozisyon (pa default, mete nan toppa Plugin la).

Fè konsèy sou zouti travay pou itilizatè klavye ak teknoloji asistans

Ou ta dwe sèlman ajoute konsèy sou eleman HTML ki tradisyonèlman konsantre sou klavye ak entèaktif (tankou lyen oswa kontwòl fòm). Malgre ke eleman HTML abitrè (tankou <span>s) yo ka fè konsantre lè yo ajoute tabindex="0"atribi a, sa pral ajoute tab stops potansyèlman anmèdan ak konfizyon sou eleman ki pa entèaktif pou itilizatè klavye, ak pifò teknoloji asistans kounye a pa anonse konsèy la nan sitiyasyon sa a. Anplis de sa, pa konte sèlman sou hoverkòm deklanche pou konsèy zouti ou a, paske sa ap fè konsèy ou yo enposib deklanche pou itilizatè klavye yo.

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

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

Eleman andikape

Eleman ki gen disabledatribi a pa entèaktif, sa vle di itilizatè yo pa ka konsantre, hover, oswa klike sou yo pou deklanche yon konsèy (oswa popover). Kòm yon solisyon, ou pral vle deklanche konsèy zouti ki soti nan yon anbalaj <div>oswa <span>, depreferans te fè klavye konsantre lè l sèvi avèk tabindex="0".

html
<span class="d-inline-block" tabindex="0" data-bs-toggle="tooltip" data-bs-title="Disabled tooltip">
  <button class="btn btn-primary" type="button" disabled>Disabled button</button>
</span>

Opsyon

Kòm opsyon yo ka pase atravè atribi done oswa JavaScript, ou ka ajoute yon non opsyon nan data-bs-, tankou nan data-bs-animation="{value}". Asire w ke w chanje kalite ka non opsyon an soti nan " camelCase " a " kebab-case " lè w ap pase opsyon yo atravè atribi done yo. Pa egzanp, sèvi ak data-bs-custom-class="beautifier"olye de data-bs-customClass="beautifier".

Apati Bootstrap 5.2.0, tout konpozan sipòte yon eksperimantal atribi done rezève data-bs-configki ka loje konfigirasyon eleman senp kòm yon kòd JSON. Lè yon eleman gen data-bs-config='{"delay":0, "title":123}'ak data-bs-title="456"atribi, titlevalè final la pral 456ak atribi done separe yo pral pase sou valè yo bay sou data-bs-config. Anplis de sa, atribi done ki egziste deja yo kapab loje valè JSON tankou data-bs-delay='{"show":0,"hide":150}'.

Remake byen ke pou rezon sekirite yo pa ka bay opsyon sanitize, sanitizeFn, ak atribi done yo.allowList
Non Kalite Default Deskripsyon
allowList objè Valè default Objè ki gen atribi ak tags pèmèt.
animation booleyen true Aplike yon tranzisyon CSS fennen nan konsèy zouti a.
boundary fisèl, eleman 'clippingParents' Limit kontrent debòde nan konsèy zouti a (aplike sèlman nan modifye preventOverflow Popper a). Pa default, li la 'clippingParents'epi li ka aksepte yon referans HTMLElement (via JavaScript sèlman). Pou plis enfòmasyon, gade nan docs detectOverflow Popper a .
container fisèl, eleman, fo false Mete konsèy zouti a nan yon eleman espesifik. Egzanp: container: 'body'. Opsyon sa a se patikilyèman itil paske li pèmèt ou pozisyon enfòmasyon an nan koule nan dokiman an tou pre eleman deklanche a - ki pral anpeche konsèy la soti nan flote lwen eleman nan deklanche pandan yon redimensionnement fenèt.
customClass fisèl, fonksyon '' Ajoute klas nan konsèy zouti a lè yo montre li. Remake byen ke yo pral ajoute klas sa yo anplis de nenpòt klas ki espesifye nan modèl la. Pou ajoute plizyè klas, separe yo ak espas: 'class-1 class-2'. Ou kapab tou pase yon fonksyon ki ta dwe retounen yon sèl fisèl ki gen non klas adisyonèl.
delay nimewo, objè 0 Reta montre ak kache konsèy zouti a (ms)—pa aplike nan kalite deklanche manyèl. Si yo bay yon nimewo, reta aplike pou tou de kache/montre. Estrikti objè se: delay: { "show": 500, "hide": 100 }.
fallbackPlacements etalaj ['top', 'right', 'bottom', 'left'] Defini plasman sere lè w bay yon lis plasman nan etalaj (nan lòd preferans). Pou plis enfòmasyon, al gade nan docs konpòtman Popper .
html booleyen false Pèmèt HTML nan enfòmasyon an. Si se vre, tags HTML nan konsèy zouti yo titlepral rann nan konsèy zouti a. Si se fo, innerTextyo pral itilize pwopriyete pou mete kontni nan DOM la. Sèvi ak tèks si w enkyete w pou atak XSS.
offset etalaj, fisèl, fonksyon [0, 0] Konsantrasyon nan konsèy la anrapò ak sib li yo. Ou ka pase yon kòd nan atribi done ak valè separe vigil tankou: data-bs-offset="10,20". Lè yo itilize yon fonksyon pou detèmine konpanse a, yo rele l ak yon objè ki genyen plasman popper a, referans a, ak popper rects kòm premye agiman li. Se eleman deklanche ne DOM la pase kòm dezyèm agiman an. Fonksyon an dwe retounen yon etalaj ki gen de nimewo: skidding , distance . Pou plis enfòmasyon, al gade nan Docs Offset Popper la .
placement fisèl, fonksyon '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.
popperConfig null, object, function null To change Bootstrap’s default Popper config, see Popper’s configuration. When a function is used to create the Popper configuration, it’s called with an object that contains the Bootstrap’s default Popper configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Popper.
sanitize boolean true Aktive oswa enfim sanitizasyon an. Si yo aktive 'template', 'content'ak 'title'opsyon yo pral dezenfekte.
sanitizeFn nil, fonksyon null Isit la ou ka bay pwòp fonksyon dezenfekte ou. Sa a ka itil si ou prefere sèvi ak yon bibliyotèk devwe pou fè dezenfekte.
selector fisèl, fo false Si yo bay yon seleksyon, objè konsèy yo pral delege nan sib yo espesifye. Nan pratik, sa a yo itilize pou aplike tou konsèy sou eleman DOM ( jQuery.onsipò). Gade pwoblèm sa a ak yon egzanp enfòmatif .
template fisèl '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' Sèvi ak HTML de baz lè w ap kreye enfòmasyon an. Yo titlepral enjekte konsèy zouti a nan .tooltip-inner. .tooltip-arrowpral tounen flèch konsèy la. Eleman wrapper ki pi eksteryè a ta dwe gen .tooltipklas la ak role="tooltip".
title fisèl, eleman, fonksyon '' 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 popover 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.

Data attributes for individual tooltips

Options for individual tooltips can alternatively be specified through the use of data attributes, as explained above.

Using function with popperConfig

const tooltip = new bootstrap.Tooltip(element, {
  popperConfig(defaultBsPopperConfig) {
    // const newPopperConfig = {...}
    // use defaultBsPopperConfig if needed...
    // return newPopperConfig
  }
})

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.

Method Description
disable Removes the ability for an element’s tooltip to be shown. The tooltip will only be able to be shown if it is re-enabled.
dispose Hides and destroys an element’s tooltip (Removes stored data on the DOM element). Tooltips that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements.
enable Gives an element’s tooltip the ability to be shown. Tooltips are enabled by default.
getInstance Static method which allows you to get the tooltip instance associated with a DOM element, or create a new one in case it wasn’t initialized.
getOrCreateInstance Static method which allows you to get the tooltip instance associated with a DOM element, or create a new one in case it wasn’t initialized.
hide Hides an element’s tooltip. Returns to the caller before the tooltip has actually been hidden (i.e. before the hidden.bs.tooltip event occurs). This is considered a “manual” triggering of the tooltip.
setContent Gives a way to change the tooltip’s content after its initialization.
show Reveals an element’s tooltip. Returns to the caller before the tooltip has actually been shown (i.e. before the shown.bs.tooltip event occurs). This is considered a “manual” triggering of the tooltip. Tooltips with zero-length titles are never displayed.
toggle Toggles an element’s tooltip. Returns to the caller before the tooltip has actually been shown or hidden (i.e. before the shown.bs.tooltip or hidden.bs.tooltip event occurs). This is considered a “manual” triggering of the tooltip.
toggleEnabled Toggles the ability for an element’s tooltip to be shown or hidden.
update Updates the position of an element’s tooltip.
const tooltip = bootstrap.Tooltip.getInstance('#example') // Returns a Bootstrap tooltip instance

// setContent example
tooltip.setContent({ '.tooltip-inner': 'another title' })
The setContent method accepts an object argument, where each property-key is a valid string selector within the popover template, and each related property-value can be string | element | function | null

Events

Event Description
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 popover 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.
show.bs.tooltip This event fires immediately when the show instance method is called.
shown.bs.tooltip This event is fired when the popover has been made visible to the user (will wait for CSS transitions to complete).
const myTooltipEl = document.getElementById('myTooltip')
const tooltip = bootstrap.Tooltip.getOrCreateInstance(myTooltipEl)

myTooltipEl.addEventListener('hidden.bs.tooltip', () => {
  // do something...
})

tooltip.hide()