Tsallake zuwa babban abun ciki Tsallake zuwa kewayawa na takardu
Check
in English

Popovers

Takaddun bayanai da misalai don ƙara Bootstrap popovers, kamar waɗanda aka samo a cikin iOS, zuwa kowane nau'in rukunin yanar gizon ku.

Dubawa

Abubuwan da ya kamata ku sani lokacin amfani da plugin ɗin popover:

  • Popovers sun dogara da ɗakin karatu na ɓangare na uku Popper don sakawa. Dole ne ku haɗa popper.min.js kafin bootstrap.js, ko amfani da bootstrap.bundle.min.jswanda ya ƙunshi Popper.
  • Popovers suna buƙatar plugin ɗin popover azaman abin dogaro.
  • Popovers suna shiga don dalilai na aiki, don haka dole ne ka fara su da kanka .
  • Tsawon sifili titleda contentƙima ba za su taɓa nuna buguwa ba.
  • Ƙayyade container: 'body'don guje wa yin matsaloli a cikin ƙarin hadaddun abubuwa (kamar ƙungiyoyin shigar da mu, ƙungiyoyin maɓalli, da sauransu).
  • Haɓaka popovers akan abubuwan ɓoye ba zai yi aiki ba.
  • Popovers don .disabledko disabledabubuwan dole ne a kunna su akan abin nadi.
  • Lokacin da aka kunna shi daga anka waɗanda ke nannade cikin layi ɗaya, popovers za su kasance a tsakiya tsakanin faɗin anka gaba ɗaya. Yi amfani .text-nowrapda <a>s ɗin ku don guje wa wannan halin.
  • Dole ne a ɓoye maɓuɓɓuka kafin a cire abubuwan da suka dace daga DOM.
  • Ana iya haifar da popovers godiya ga wani abu a cikin inuwa DOM.
Ta hanyar tsoho, wannan bangaren yana amfani da ginanniyar sanitizer na abun ciki, wanda ke fitar da duk abubuwan HTML waɗanda ba a ba su izini ba. Duba sashin sanitizer a cikin takaddun JavaScript ɗin mu don ƙarin cikakkun bayanai.
Tasirin raye-rayen wannan bangaren ya dogara ne da prefers-reduced-motiontambayar kafofin watsa labarai. Dubi raguwar sashin motsi na takaddun damar mu .

Ci gaba da karantawa don ganin yadda popovers ke aiki tare da wasu misalai.

Misalai

Kunna popovers

Kamar yadda aka ambata a sama, dole ne ku fara fara popovers kafin a iya amfani da su. Hanya ɗaya don fara duk popovers akan shafi shine zaɓi su ta yanayin su data-bs-toggle, kamar haka:

const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]')
const popoverList = [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl))

Live demo

Muna amfani da JavaScript mai kama da snippet na sama don yin popover mai zuwa. An saita taken ta data-bs-titlekuma an saita abun cikin jiki ta hanyar data-bs-content.

Jin kyauta don amfani da ko dai titlea data-bs-titlecikin HTML ɗinku. Lokacin da titleaka yi amfani da shi, Popper zai maye gurbinsa ta atomatik tare da data-bs-titlelokacin da aka samar da kashi.
html
<button type="button" class="btn btn-lg btn-danger" data-bs-toggle="popover" data-bs-title="Popover title" data-bs-content="And here's some amazing content. It's very engaging. Right?">Click to toggle popover</button>

Hanyoyi hudu

Akwai zaɓuɓɓuka huɗu: sama, dama, ƙasa, da hagu. Ana nuna kwatance yayin amfani da Bootstrap a cikin RTL. Saita data-bs-placementdon canza alkibla.

html
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="top" data-bs-content="Top popover">
  Popover on top
</button>
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="right" data-bs-content="Right popover">
  Popover on right
</button>
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-content="Bottom popover">
  Popover on bottom
</button>
<button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="left" data-bs-content="Left popover">
  Popover on left
</button>

Customcontainer

Lokacin da kake da wasu nau'ikan nau'ikan nau'ikan iyaye waɗanda ke tsoma baki tare da popover, za ku so ku saka al'ada containerta yadda HTML ɗin popover ta bayyana a cikin wannan kashi maimakon. Wannan ya zama ruwan dare a cikin tebur masu amsawa, ƙungiyoyin shigarwa, da makamantansu.

const popover = new bootstrap.Popover('.example-popover', {
  container: 'body'
})

Wani yanayi inda zaku so saita al'ada bayyananne containershine popovers a cikin maganganun modal , don tabbatar da cewa popover kanta an haɗa shi da modal. Wannan yana da mahimmanci musamman ga popovers waɗanda ke ƙunshe da abubuwa masu mu'amala - maganganu na modal za su kama hankali, don haka sai dai idan popover ɗin ɗan ƙaramin ɗan ƙaramin abu ne, masu amfani ba za su iya mayar da hankali ko kunna waɗannan abubuwan mu'amala ba.

const popover = new bootstrap.Popover('.example-popover', {
  container: '.modal-body'
})

Popovers na al'ada

An ƙara a cikin v5.2.0

Kuna iya siffanta bayyanar popovers ta amfani da masu canji na CSS . Mun saita aji na al'ada tare data-bs-custom-class="custom-popover"da iyakacin kamannin mu na al'ada kuma muyi amfani da shi don ƙetare wasu masu canjin CSS na gida.

.custom-popover {
  --bs-popover-max-width: 200px;
  --bs-popover-border-color: var(--bs-primary);
  --bs-popover-header-bg: var(--bs-primary);
  --bs-popover-header-color: var(--bs-white);
  --bs-popover-body-padding-x: 1rem;
  --bs-popover-body-padding-y: .5rem;
}
html
<button type="button" class="btn btn-secondary"
        data-bs-toggle="popover" data-bs-placement="right"
        data-bs-custom-class="custom-popover"
        data-bs-title="Custom popover"
        data-bs-content="This popover is themed via CSS variables.">
  Custom popover
</button>

Kore a danna gaba

Yi amfani da focusfaɗakarwa don korar popovers akan danna gaba na mai amfani na wani nau'i na daban fiye da abin juyawa.

Takamaiman alamar da ake buƙata don korar-kan-na gaba-danna

Don dacewa da mai binciken giciye da kuma halayyar dandamali, dole ne ku yi amfani da <a>alamar, ba alamar ba <button>, kuma dole ne ku haɗa da tabindexsifa.

html
<a tabindex="0" class="btn btn-lg btn-danger" role="button" data-bs-toggle="popover" data-bs-trigger="focus" data-bs-title="Dismissible popover" data-bs-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a>
const popover = new bootstrap.Popover('.popover-dismiss', {
  trigger: 'focus'
})

Abubuwan da aka kashe

Abubuwan da ke da disabledsifa ba su da ma'amala, ma'ana masu amfani ba za su iya yin shawagi ko danna su don jawo popover (ko kayan aiki ba). A matsayin madaidaicin aiki, zaku so kunna popover daga nannade <div>ko <span>, wanda aka fi dacewa da maballin madannai ta amfani da tabindex="0".

Don naƙasassun abubuwan faɗowa, kuna iya gwammace data-bs-trigger="hover focus"ta yadda popover ɗin ya bayyana azaman ra'ayin gani nan da nan ga masu amfani da ku saboda ƙila ba za su yi tsammanin danna kan wani guragu ba.

html
<span class="d-inline-block" tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-content="Disabled popover">
  <button class="btn btn-primary" type="button" disabled>Disabled button</button>
</span>

CSS

Masu canji

An ƙara a cikin v5.2.0

A matsayin wani ɓangare na tsarin canza canjin CSS na Bootstrap, popovers yanzu suna amfani da masu canjin CSS na gida .popoverdon haɓaka gyare-gyare na ainihin lokaci. An saita dabi'u don masu canjin CSS ta hanyar Sass, don haka har yanzu ana tallafawa keɓancewar Sass, ma.

  --#{$prefix}popover-zindex: #{$zindex-popover};
  --#{$prefix}popover-max-width: #{$popover-max-width};
  @include rfs($popover-font-size, --#{$prefix}popover-font-size);
  --#{$prefix}popover-bg: #{$popover-bg};
  --#{$prefix}popover-border-width: #{$popover-border-width};
  --#{$prefix}popover-border-color: #{$popover-border-color};
  --#{$prefix}popover-border-radius: #{$popover-border-radius};
  --#{$prefix}popover-inner-border-radius: #{$popover-inner-border-radius};
  --#{$prefix}popover-box-shadow: #{$popover-box-shadow};
  --#{$prefix}popover-header-padding-x: #{$popover-header-padding-x};
  --#{$prefix}popover-header-padding-y: #{$popover-header-padding-y};
  @include rfs($popover-header-font-size, --#{$prefix}popover-header-font-size);
  --#{$prefix}popover-header-color: #{$popover-header-color};
  --#{$prefix}popover-header-bg: #{$popover-header-bg};
  --#{$prefix}popover-body-padding-x: #{$popover-body-padding-x};
  --#{$prefix}popover-body-padding-y: #{$popover-body-padding-y};
  --#{$prefix}popover-body-color: #{$popover-body-color};
  --#{$prefix}popover-arrow-width: #{$popover-arrow-width};
  --#{$prefix}popover-arrow-height: #{$popover-arrow-height};
  --#{$prefix}popover-arrow-border: var(--#{$prefix}popover-border-color);
  

Sass masu canji

$popover-font-size:                 $font-size-sm;
$popover-bg:                        $white;
$popover-max-width:                 276px;
$popover-border-width:              $border-width;
$popover-border-color:              var(--#{$prefix}border-color-translucent);
$popover-border-radius:             $border-radius-lg;
$popover-inner-border-radius:       subtract($popover-border-radius, $popover-border-width);
$popover-box-shadow:                $box-shadow;

$popover-header-font-size:          $font-size-base;
$popover-header-bg:                 shade-color($popover-bg, 6%);
$popover-header-color:              $headings-color;
$popover-header-padding-y:          .5rem;
$popover-header-padding-x:          $spacer;

$popover-body-color:                $body-color;
$popover-body-padding-y:            $spacer;
$popover-body-padding-x:            $spacer;

$popover-arrow-width:               1rem;
$popover-arrow-height:              .5rem;

Amfani

Kunna popovers ta JavaScript:

const exampleEl = document.getElementById('example')
const popover = new bootstrap.Popover(exampleEl, options)

Yin popovers yayi aiki ga maballin madannai da masu amfani da fasaha masu taimako

Don ƙyale masu amfani da madannai don kunna popovers ɗinku, yakamata ku ƙara su kawai zuwa abubuwan HTML waɗanda ke al'adar maɓalli-mayar da hankali da mu'amala (kamar hanyoyin haɗin gwiwa ko sarrafa tsari). Kodayake abubuwan HTML na sabani (kamar <span>s) ana iya mai da hankali ta hanyar ƙara tabindex="0"sifa, wannan zai ƙara yiwuwar dakatarwa mai ban haushi da ruɗani akan abubuwan da ba su da alaƙa ga masu amfani da keyboard, kuma galibin fasahar taimako a halin yanzu ba sa sanar da abun cikin popover a cikin wannan yanayin. . Bugu da ƙari, kar a dogara kawai a hovermatsayin abin da ke haifar da popover ɗinku, saboda wannan zai sa su gagara jawo masu amfani da madannai.

Yayin da za ku iya saka arziki, tsararren HTML a cikin popovers tare da htmlzaɓi, muna ba da shawarar sosai cewa ku guji ƙara yawan adadin abun ciki. Yadda popovers ke aiki a halin yanzu shine, da zarar an nuna, abun cikin su yana ɗaure da sifa mai jawo tare da aria-describedbysifa. Sakamakon haka, za a ba da sanarwar gabaɗayan abubuwan da ke cikin popover ga masu amfani da fasaha na taimaka wa masu amfani da fasaha a matsayin dogon rafi marar yankewa.

Bugu da ƙari, yayin da zai yiwu a haɗa da iko mai hulɗa (kamar abubuwa) a cikin poution da aka allowListyarda da shi a halin yanzu Pover ba ta gudanar da tsarin kula da keyboard ba. Lokacin da mai amfani da madannai ya buɗe popover, mayar da hankali kan abin da ke jawowa, kuma kamar yadda popover yawanci ba ya bin abin faɗakarwa a cikin tsarin takaddar, babu tabbacin ci gaba/latsawa.TABzai motsa mai amfani da madannai zuwa cikin popover kanta. A taƙaice, ƙara ikon mu'amala kawai zuwa popover yana yiwuwa ya sa waɗannan abubuwan sarrafawa ba za su iya isa ga masu amfani da madannai da masu amfani da fasahar taimako ba, ko kuma aƙalla yin tsarin mayar da hankali gabaɗaya mara ma'ana. A cikin waɗannan lokuta, yi la'akari da yin amfani da maganganun modal maimakon.

Zabuka

Kamar yadda za a iya wuce zaɓuɓɓuka ta hanyar sifofin bayanai ko JavaScript, zaku iya ƙara sunan zaɓi zuwa data-bs-, kamar a cikin data-bs-animation="{value}". Tabbatar canza nau'in shari'ar sunan zaɓi daga " CamelCase " zuwa " kebab-case " lokacin ƙaddamar da zaɓuɓɓuka ta hanyar halayen bayanai. Misali, amfani data-bs-custom-class="beautifier"maimakon data-bs-customClass="beautifier".

Kamar na Bootstrap 5.2.0, duk abubuwan da aka gyara suna goyan bayan sifa da aka tanada na gwajidata-bs-config wanda zai iya saita tsarin sassauƙan sassa azaman kirtani JSON. Lokacin da kashi yana data-bs-config='{"delay":0, "title":123}'da data-bs-title="456"sifofi, titleƙimar ƙarshe za ta kasance 456kuma keɓantattun halayen bayanan za su soke ƙimar da aka bayar akan data-bs-config. Bugu da ƙari, halayen bayanan da ke akwai suna iya ɗaukar ƙimar JSON kamar data-bs-delay='{"show":0,"hide":150}'.

Lura cewa saboda dalilai na tsaro ba za a iya samar da sanitize, sanitizeFn, da zaɓuɓɓuka ta amfani da halayen bayanai ba.allowList
Suna Nau'in Default Bayani
allowList abu Ƙimar ta asali Abu wanda ya ƙunshi halayen izini da alamun izini.
animation boolean true Aiwatar da canjin Fade CSS zuwa popover.
boundary kirtani, element 'clippingParents' Matsakaicin ƙayyadaddun ƙayyadaddun ƙayyadaddun iyaka na popover (ya shafi Popper's preventOverflow modifier kawai). Ta hanyar tsoho, yana 'clippingParents'kuma yana iya karɓar bayanin HTMLElement (ta JavaScript kawai). Don ƙarin bayani koma zuwa Popper's detectOverflow docs .
container kirtani, kashi, ƙarya false Haɗa popover zuwa takamaiman yanki. Misali container: 'body':. Wannan zaɓin yana da amfani musamman domin yana ba ku damar sanya popover a cikin kwararar daftarin aiki kusa da abin da ke haifarwa - wanda zai hana popover daga shawagi daga abin da ke kunnawa yayin girman taga.
content kirtani, kashi, aiki '' Tsohuwar ƙimar abun ciki idan data-bs-contentsifa ba ta nan. Idan an ba da aiki, za a kira shi tare da thissaita saitin abin da ake manne wa popover.
customClass kirtani, aiki '' Ƙara azuzuwan zuwa popover lokacin da aka nuna shi. Lura cewa waɗannan azuzuwan za a ƙara su ban da kowane nau'i da aka kayyade a cikin samfuri. Don ƙara azuzuwan da yawa, raba su da sarari: 'class-1 class-2'. Hakanan zaka iya wuce aikin da yakamata ya dawo da kirtani ɗaya mai ɗauke da ƙarin sunayen aji.
delay lamba, abu 0 Jinkirin nunawa da ɓoye popover (ms) — baya aiki ga nau'in faɗakarwa na hannu. Idan an ba da lamba, ana amfani da jinkiri ga duka ɓoye/nunawa. Tsarin abu shine delay: { "show": 500, "hide": 100 }:.
fallbackPlacements kirtani, tsararru ['top', 'right', 'bottom', 'left'] Ƙayyade jeri na faɗuwa ta hanyar samar da jerin jeri a tsararru (domin fifiko). Don ƙarin bayani koma zuwa Docs halayyar Popper .
html boolean false Bada HTML a cikin popover. Idan gaskiya ne, HTML tags a cikin popover's titleza a sanya a cikin popover. Idan ƙarya, innerTextza a yi amfani da dukiya don saka abun ciki a cikin DOM. Yi amfani da rubutu idan kun damu da harin XSS.
offset lamba, kirtani, aiki [0, 0] Matsalolin popover dangane da manufar sa. Kuna iya wuce kirtani a cikin sifofin bayanai tare da raba waƙafi kamar: data-bs-offset="10,20". Lokacin da aka yi amfani da aiki don tantance abin kashewa, ana kiran shi da wani abu mai ɗauke da popper placement, the reference, and popper rects as his first muhawara. An wuce kullin DOM mai jawowa azaman hujja ta biyu. Dole ne aikin ya dawo da jeri tare da lambobi biyu: tsalle -tsalle , nisa . Don ƙarin bayani koma zuwa Popper's offset docs .
placement kirtani, aiki 'top' Yadda ake sanya popover: auto, sama, kasa, hagu, dama. Lokacin autoda aka ƙayyade, zai sake jujjuya yanayin popover. Lokacin da aka yi amfani da aiki don ƙayyade wurin, ana kiran shi tare da kumburin DOM popover a matsayin hujjarsa ta farko da maɓallin DOM mai jawowa a matsayin na biyu. An thissaita mahallin zuwa misalin popover.
popperConfig null, abu, aiki null Don canza saitunan Popper tsoho na Bootstrap, duba Tsarin Popper . Lokacin da aka yi amfani da aiki don ƙirƙirar tsarin Popper, ana kiran shi da wani abu da ke ɗauke da tsohowar Popper na Bootstrap. Yana taimaka muku amfani da haɗa tsoho tare da tsarin ku. Dole ne aikin ya dawo da abin daidaitawa don Popper.
sanitize boolean true Kunna ko kashe tsaftar. Idan an kunna 'template', 'content'kuma 'title'za a tsabtace zaɓuɓɓuka.
sanitizeFn null, aiki null Anan zaku iya samar da aikin tsabtace ku. Wannan na iya zama da amfani idan kun fi son yin amfani da ɗakin karatu da aka keɓe don yin tsafta.
selector kirtani, karya false Idan an samar da mai zaɓe, za a ba da abubuwan da za a yi amfani da su zuwa ƙayyadaddun manufa. A aikace, ana amfani da wannan don kuma amfani da popovers zuwa abubuwan da aka ƙara DOM ( jQuery.ontallafi). Dubi wannan batu da misali mai ba da labari .
template kirtani '<div class="popover" role="popover"><div class="popover-arrow"></div><div class="popover-inner"></div></div>' Tushen HTML don amfani yayin ƙirƙirar popover. Za a yi allurar popover titlea cikin .popover-inner. .popover-arrowzai zama kibiya popover. Abun nadi na waje yakamata ya kasance yana da .popoveraji da role="popover".
title kirtani, kashi, aiki '' Tsohuwar ƙimar take idan babu titlesifa. Idan an ba da aiki, za a kira shi tare da thissaita saitin abin da ake manne wa popover.
trigger kirtani 'hover focus' Yadda ake kunna popover: danna, hover, mayar da hankali, manual. Kuna iya wuce abubuwan jan hankali da yawa; raba su da sarari. 'manual'yana nuna cewa za a kunna popover ta hanyar shirye-shirye ta hanyar .popover('show'), .popover('hide')da .popover('toggle')hanyoyin; Ba za a iya haɗa wannan ƙimar tare da kowane abin tayar da hankali ba. 'hover'da kansa zai haifar da popovers waɗanda ba za a iya kunna su ta hanyar keyboard ba, kuma yakamata a yi amfani da su kawai idan akwai wasu hanyoyin isar da bayanai iri ɗaya ga masu amfani da madannai.

Halayen bayanai don faɗuwar mutum ɗaya

Zaɓuɓɓuka don faɗuwar faɗuwar mutum ɗaya ana iya a madadin su ta hanyar amfani da sifofin bayanai, kamar yadda aka bayyana a sama.

Amfani da aiki tare dapopperConfig

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

Hanyoyin

Hanyoyi masu daidaitawa da canji

Duk hanyoyin API ba daidai ba ne kuma suna fara canji . Suna komawa ga mai kiran da zarar an fara canji amma kafin ya ƙare . Bugu da ƙari, za a yi watsi da kiran hanya akan ɓangaren canji .

Duba takaddun JavaScript ɗin mu don ƙarin bayani .

Hanya Bayani
disable Yana kawar da damar da za a iya nunawa wani abu na popover. Za a iya nunawa popover kawai idan an sake kunna shi.
dispose Yana ɓoyewa da lalata faɗuwar wani abu (Yana cire bayanan da aka adana akan ɓangaren DOM). Popovers da ke amfani da wakilai (waɗanda aka ƙirƙira ta amfani da selectorzaɓi ) ba za a iya lalata su daban-daban akan abubuwan da ke haifar da zuriya ba.
enable Yana ba da popover na kashi ikon nunawa. Ana kunna Popovers ta tsohuwa.
getInstance Hanyar a tsaye wacce ke ba ku damar samun misalin popover mai alaƙa da nau'in DOM.
getOrCreateInstance Hanyar a tsaye wacce ke ba ku damar samun misalan popover mai alaƙa da nau'in DOM, ko ƙirƙirar sabo idan ba a fara shi ba.
hide Hides an element’s popover. Returns to the caller before the popover has actually been hidden (i.e. before the hidden.bs.popover event occurs). This is considered a “manual” triggering of the popover.
setContent Gives a way to change the popover’s content after its initialization.
show Reveals an element’s popover. Returns to the caller before the popover has actually been shown (i.e. before the shown.bs.popover event occurs). This is considered a “manual” triggering of the popover. Popovers whose title and content are both zero-length are never displayed.
toggle Toggles an element’s popover. Returns to the caller before the popover has actually been shown or hidden (i.e. before the shown.bs.popover or hidden.bs.popover event occurs). This is considered a “manual” triggering of the popover.
toggleEnabled Toggles the ability for an element’s popover to be shown or hidden.
update Updates the position of an element’s popover.
// getOrCreateInstance example
const popover = bootstrap.Popover.getOrCreateInstance('#example') // Returns a Bootstrap popover instance

// setContent example
myPopover.setContent({
  '.popover-header': 'another title',
  '.popover-body': 'another content'
})
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.popover This event is fired immediately when the hide instance method has been called.
hidden.bs.popover This event is fired when the popover has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.bs.popover This event is fired after the show.bs.popover event when the popover template has been added to the DOM.
show.bs.popover This event fires immediately when the show instance method is called.
shown.bs.popover Ana korar wannan taron lokacin da aka bayyana popover ga mai amfani (zai jira canjin CSS ya kammala).
const myPopoverTrigger = document.getElementById('myPopover')
myPopoverTrigger.addEventListener('hidden.bs.popover', () => {
  // do something...
})