Source

Carousel

Ib qho slideshow tivthaiv rau kev caij tsheb kauj vab los ntawm cov ntsiab lus-cov duab lossis cov ntawv swb-zoo li lub voj voog.

Nws ua haujlwm li cas

Lub carousel yog slideshow rau cycling los ntawm ib tug series ntawm cov ntsiab lus, ua nrog CSS 3D transforms thiab me ntsis ntawm JavaScript. Nws ua haujlwm nrog cov duab, ntawv nyeem, lossis kev cai cim. Nws kuj suav nrog kev txhawb nqa rau yav dhau los / tom ntej tswj thiab ntsuas.

Hauv cov browsers uas qhov Page Visibility API tau txais kev txhawb nqa, lub carousel yuav zam zawv zawg thaum lub vev xaib tsis pom rau tus neeg siv (xws li thaum browser tab tsis ua haujlwm, qhov browser qhov rai raug txo qis, thiab lwm yam).

Thov nco ntsoov tias cov carousels nested tsis tau txais kev txhawb nqa, thiab carousels feem ntau tsis ua raws li cov qauv kev siv tau.

Thaum kawg, yog tias koj tab tom tsim peb JavaScript los ntawm qhov chaw, nws xav tauutil.js .

Piv txwv

Carousels tsis cia li normalize swb qhov ntev. Yog li ntawd, tej zaum koj yuav tau siv cov khoom siv ntxiv lossis cov qauv kev cai kom haum rau cov ntsiab lus loj. Thaum carousels txhawb nqa yav dhau los / tom ntej tswj thiab ntsuas, lawv tsis xav tau meej. Ntxiv thiab kho raws li koj pom haum.

Cov .activechav kawm yuav tsum tau ntxiv rau ib qho ntawm cov swb txwv tsis pub lub carousel yuav tsis pom. Tsis tas li ntawd nco ntsoov teem ib tus cim tshwj xeeb rau ntawm .carouselkev xaiv tswj, tshwj xeeb tshaj yog tias koj siv ntau lub carousels ntawm ib nplooj ntawv. Kev tswj thiab ntsuas cov ntsiab lus yuav tsum muaj tus data-targetcwj pwm (lossis hrefrau kev sib txuas) uas phim tus id ntawm lub .carouselcaij.

Slides nkaus xwb

Ntawm no yog ib tug carousel nrog slides xwb. Nco ntsoov lub xub ntiag ntawm .d-blockthiab .w-100ntawm cov duab carousel los tiv thaiv qhov browser default duab sib dhos.

<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
</div>

Nrog tswj

Ntxiv rau hauv cov kev tswj yav dhau los thiab tom ntej:

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Nrog ntsuas

Koj tuaj yeem ntxiv cov ntsuas rau lub carousel, nrog rau cov tswj, ib yam nkaus.

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Nrog cov ntawv sau

Ntxiv cov ntawv sau rau koj cov slides yooj yim nrog cov .carousel-captionkhoom hauv ib qho twg .carousel-item. Lawv tuaj yeem muab zais tau yooj yim ntawm cov chaw saib me me, raws li qhia hauv qab no, nrog rau kev xaiv cov khoom siv hluav taws xob . Peb zais lawv thawj zaug nrog .d-nonethiab coj lawv rov qab rau ntawm cov khoom siv nruab nrab nrog .d-md-block.

<div class="carousel-item">
  <img src="..." alt="...">
  <div class="carousel-caption d-none d-md-block">
    <h5>...</h5>
    <p>...</p>
  </div>
</div>

Crossfade

Ntxiv .carousel-faderau koj lub carousel rau animate slides nrog ib tug ploj mus hloov tsis yog ib tug swb.

<div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=777&fg=555&text=First slide" alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Kev siv

Los ntawm cov ntaub ntawv attributes

Siv cov ntaub ntawv cwj pwm kom yooj yim tswj txoj hauj lwm ntawm lub carousel. data-slidelees txais cov lus tseem ceeb prevlossis next, uas hloov txoj haujlwm swb txheeb ze rau nws txoj haujlwm tam sim no. Xwb, siv data-slide-tokom dhau qhov ntsuas qhov ntsuas tsis sib haum mus rau lub carousel data-slide-to="2", uas hloov txoj haujlwm swb mus rau qhov ntsuas tshwj xeeb pib nrog 0.

Tus data-ride="carousel"cwj pwm yog siv los khij lub carousel li animating pib ntawm nplooj ntawv load. Nws tsis tuaj yeem siv ua ke nrog (rov ua dua thiab tsis tsim nyog) qhia meej JavaScript pib ntawm tib lub carousel.

Ntawm JavaScript

Hu rau carousel manually nrog:

$('.carousel').carousel()

Kev xaiv

Cov kev xaiv tuaj yeem dhau los ntawm cov ntaub ntawv tus cwj pwm lossis JavaScript. Rau cov ntaub ntawv tus cwj pwm, ntxiv rau qhov kev xaiv npe rau data-, xws li hauv data-interval="".

Lub npe Hom Default Kev piav qhia
lub sijhawm tus lej 5000 Lub sijhawm ncua sijhawm ntawm kev caij tsheb kauj vab ib yam khoom. Yog hais tias tsis tseeb, carousel yuav tsis cia li voj voog.
keyboard boolean muaj tseeb Seb lub carousel yuav tsum teb rau cov xwm txheej keyboard.
nres hlua | boolean "hover"

Yog tias tau teeb tsa "hover", nres lub caij tsheb kauj vab ntawm lub carousel rau mouseenterthiab rov pib caij tsheb kauj vab ntawm lub carousel rau mouseleave. Yog tias teem rau false, hovering hla lub carousel yuav tsis nres nws.

Ntawm cov khoom siv kov, thaum teem rau "hover", kev caij tsheb kauj vab yuav nres rau touchend(thaum tus neeg siv ua tiav kev cuam tshuam nrog lub carousel) rau ob lub sijhawm, ua ntej yuav rov pib dua. Nco ntsoov tias qhov no yog ntxiv rau tus cwj pwm nas saum toj no.

caij txoj hlua cuav Autoplays lub carousel tom qab tus neeg siv manually cycles thawj yam khoom. Yog hais tias "carousel", autoplays lub carousel ntawm load.
qhwv boolean muaj tseeb Txawm hais tias lub carousel yuav tsum tsis tu ncua los yog muaj zog nres.

Cov txheej txheem

Asynchronous txoj kev thiab kev hloov

Txhua txoj kev API yog asynchronous thiab pib hloov pauv . Lawv rov qab mus rau tus neeg hu xov tooj sai li sai tau thaum qhov kev hloov pauv pib tab sis ua ntej nws xaus . Tsis tas li ntawd, ib txoj kev hu rau ib qho kev hloov pauv yuav raug tsis quav ntsej .

Saib peb cov ntaub ntawv JavaScript kom paub ntau ntxiv.

.carousel(options)

Pib lub carousel nrog ib qho kev xaiv xaiv objectthiab pib caij tsheb kauj vab los ntawm cov khoom.

$('.carousel').carousel({
  interval: 2000
})

.carousel('cycle')

Cycles los ntawm cov khoom carousel ntawm sab laug mus rau sab xis.

.carousel('pause')

Nres lub carousel los ntawm kev caij tsheb kauj vab los ntawm cov khoom.

.carousel(number)

Cycles lub carousel rau ib lub thav duab tshwj xeeb (0 raws, zoo ib yam li ib qho array). Rov qab mus rau tus neeg hu ua ntej lub hom phiaj khoom tau tshwm sim (piv txwv li ua ntej qhov slid.bs.carouselxwm txheej tshwm sim).

.carousel('prev')

Lub voj voog mus rau qhov khoom dhau los. Rov qab mus rau tus neeg hu ua ntej qhov khoom dhau los tau tshwm sim (piv txwv li ua ntej qhov slid.bs.carouselxwm txheej tshwm sim).

.carousel('next')

Cycles mus rau yam khoom tom ntej. Rov qab mus rau tus neeg hu ua ntej cov khoom tom ntej tau tshwm sim (piv txwv li ua ntej qhov slid.bs.carouselxwm txheej tshwm sim).

.carousel('dispose')

Ua kom lub ntsiab ntawm lub carousel.

Cov xwm txheej

Bootstrap's carousel class exposes ob txheej xwm rau hooking rau hauv carousel functionality. Ob qho xwm txheej muaj cov khoom nram qab no ntxiv:

  • direction: Cov kev taw qhia uas lub carousel zawv zawg (txawm "left"los yog "right").
  • relatedTarget: Lub caij DOM uas tau swb rau hauv qhov chaw ua cov khoom nquag.
  • from: Qhov Performance index ntawm cov khoom tam sim no
  • to: Qhov Performance index ntawm cov khoom tom ntej

Tag nrho cov xwm txheej carousel raug rho tawm haujlwm ntawm lub carousel nws tus kheej (piv txwv li ntawm <div class="carousel">).

Hom xwm txheej Kev piav qhia
swb.bs.carousel Qhov xwm txheej no tua hluav taws tam sim ntawd thaum tus slidetxheej txheem piv txwv raug hu.
swb.bs.carousel Qhov kev tshwm sim no raug rho tawm haujlwm thaum lub carousel tau ua tiav nws txoj kev hloov pauv.
$('#myCarousel').on('slide.bs.carousel', function () {
  // do something…
})

Hloov lub sijhawm hloov pauv

Lub sijhawm hloov pauv ntawm .carousel-itemtuaj yeem hloov pauv nrog $carousel-transitionSass hloov pauv ua ntej sau lossis cov qauv kev cai yog tias koj siv CSS suav nrog. Yog tias siv ntau qhov kev hloov pauv, xyuas kom meej tias qhov kev hloov pauv hloov tau txhais ua ntej (xws li. transition: transform 2s ease, opacity .5s ease-out).