Source

Carousel

Komponén pintonan slaid pikeun ngurilingan elemen—gambar atanapi slide téks—sapertos carousel.

Kumaha gawéna

Carousel nyaéta pintonan slaid pikeun ngurilingan runtuyan eusi, diwangun ku CSS 3D transforms sarta saeutik JavaScript. Gawéna sareng séri gambar, téks, atanapi markup khusus. Éta ogé kalebet dukungan pikeun kontrol sareng indikator sateuacana / salajengna.

Dina panyungsi dimana Page Visibility API dirojong, carousel bakal ngahindarkeun ngageser nalika halaman wéb henteu katingali ku pangguna (sapertos nalika tab browser teu aktip, jandela browser diminimalkeun, jsb.).

Pangaruh animasi tina komponén ieu gumantung kana prefers-reduced-motionpamundut média. Tempo bagian gerak ngurangan tina dokuméntasi aksés kami .

Punten perhatikeun yén carousel bersarang henteu didukung, sareng carousel umumna henteu patuh kana standar aksés.

Anu pamungkas, upami anjeun nuju ngawangun JavaScript kami tina sumber, peryogiutil.js .

Contona

Carousel henteu otomatis nganormalkeun dimensi slide. Sapertos kitu, anjeun panginten kedah nganggo utilitas tambahan atanapi gaya khusus pikeun ukuran eusi anu pas. Nalika carousel ngadukung kontrol sareng indikator sateuacana/salajengna, aranjeunna henteu diperyogikeun sacara eksplisit. Tambihkeun sareng saluyukeun sakumaha anu anjeun pikahoyong.

Kelas .activekudu ditambahkeun kana salah sahiji slides disebutkeun carousel moal katingali. Ogé pastikeun pikeun nyetel id unik dina .carouselkadali pilihan, utamana lamun Anjeun keur make sababaraha carousels dina hiji kaca. Unsur kontrol sareng indikator kedah gaduh data-targetatribut (atanapi hrefpikeun tautan) anu cocog sareng id .carouselunsur.

Ngageser wungkul

Ieu carousel kalayan slide wungkul. Catet ayana gambar carousel .d-blocksareng .w-100on pikeun nyegah alignment gambar standar browser.

<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
        <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
  </div>
</div>

Kalawan kadali

Nambahkeun kadali saméméhna sareng salajengna:

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </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>

Kalayan indikator

Anjeun ogé tiasa nambihan indikator kana carousel, sareng kadali ogé.

<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 src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </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>

Kalayan caption

Tambihkeun caption kana slide anjeun kalayan gampang kalayan .carousel-captionunsur anu aya dina .carousel-item. Éta bisa gampang disumputkeun dina viewports leutik, ditémbongkeun saperti di handap ieu, kalawan utilitas tampilan pilihan . Urang nyumputkeunana mimitina .d-nonejeung mawa eta deui dina alat-ukuran sedeng jeung .d-md-block.

<div id="carouselExampleCaptions" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleCaptions" data-slide-to="1"></li>
    <li data-target="#carouselExampleCaptions" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="..." class="d-block w-100" alt="...">
      <div class="carousel-caption d-none d-md-block">
        <h5>First slide label</h5>
        <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
      </div>
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
      <div class="carousel-caption d-none d-md-block">
        <h5>Second slide label</h5>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      </div>
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
      <div class="carousel-caption d-none d-md-block">
        <h5>Third slide label</h5>
        <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
      </div>
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleCaptions" 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="#carouselExampleCaptions" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Crossfade

Tambihkeun .carousel-fadekana carousel anjeun pikeun ngahirupkeun slide kalayan transisi luntur tibatan slide.

<div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </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>

Tambahkeun data-interval=""ka a .carousel-itemngarobah jumlah waktu reureuh antara otomatis Ngabuburit ka item salajengna.

<div id="carouselExampleInterval" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active" data-interval="10000">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item" data-interval="2000">
      <img src="..." class="d-block w-100" alt="...">
    </div>
    <div class="carousel-item">
      <img src="..." class="d-block w-100" alt="...">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleInterval" 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="#carouselExampleInterval" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Pamakéan

Via atribut data

Paké atribut data pikeun gampang ngadalikeun posisi carousel nu. data-slidenarima kecap konci prevatawa next, nu ngarobah posisi slide relatif ka posisi ayeuna. Alternatipna, pake data-slide-topikeun ngalirkeun indéks slide atah ka carousel data-slide-to="2", nu shifts posisi slide ka indéks husus dimimitian ku 0.

Atribut data-ride="carousel"ieu dianggo pikeun nandaan carousel salaku animasi dimimitian dina beban halaman. Upami anjeun henteu nganggo data-ride="carousel"pikeun ngamimitian carousel anjeun, anjeun kedah ngamimitian nyalira. Ieu teu bisa dipaké dina kombinasi kalayan (kaleuleuwihan sarta teu perlu) initialization JavaScript eksplisit tina carousel sarua.

Ngaliwatan JavaScript

Telepon carousel sacara manual nganggo:

$('.carousel').carousel()

Pilihan

Pilihan bisa diliwatan via atribut data atawa JavaScript. Pikeun atribut data, tambahkeun nami pilihan ka data-, sapertos dina data-interval="".

Ngaran Tipe Default Katerangan
interval angka 5000 Jumlah waktu reureuh antara otomatis Ngabuburit hiji item. Lamun palsu, carousel moal otomatis siklus.
kibor boolean leres Naha carousel kedah ngaréspon kana acara keyboard.
reureuh sakeudeung string | boolean "ngalayang"

Lamun disetel ka "hover", ngareureuhkeun muter tina carousel on mouseentersarta neruskeun muter tina carousel on mouseleave. Upami disetel ka false, hovering dina carousel moal ngareureuhkeun eta.

Dina alat-alat touch-diaktipkeun, lamun disetel ka "hover", Ngabuburit bakal ngareureuhkeun on touchend(sakali pamaké réngsé berinteraksi sareng carousel) pikeun dua interval, saméméh otomatis neruskeun. Catet yén ieu salian ti kabiasaan beurit di luhur.

numpak senar palsu Autoplays carousel sanggeus pamaké sacara manual siklus item kahiji. Lamun "carousel", autoplays carousel on beban.
bungkus boolean leres Naha carousel kedah ngurilingan kontinyu atanapi kedah eureun.
keuna boolean leres Naha carousel kedah ngadukung interaksi gesek kenca/katuhu dina alat layar rampa.

Métode

Métode Asynchronous sareng transisi

Sadaya metode API henteu sinkron sareng ngamimitian transisi . Aranjeunna balik deui ka panelepon pas transisi dimimitian tapi saméméh éta ends . Sajaba ti éta, panggero métode dina komponén transisi bakal dipaliré .

Tempo dokuméntasi JavaScript kami pikeun inpo nu leuwih lengkep .

.carousel(options)

Initializes carousel kalawan pilihan pilihan objecttur mimitian Ngabuburit ngaliwatan item.

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

.carousel('cycle')

Siklus ngaliwatan item carousel ti kénca ka katuhu.

.carousel('pause')

Ngeureunkeun carousel tina ngurilingan barang.

.carousel(number)

Siklus carousel kana pigura nu tangtu (0 dumasar, sarupa hiji Asép Sunandar Sunarya). Mulih ka nu nelepon saméméh item target geus ditémbongkeun (ie saméméh slid.bs.carouselacara lumangsung).

.carousel('prev')

Siklus ka item saméméhna. Mulih ka nu nelepon saméméh item saméméhna geus ditémbongkeun (ie saméméh slid.bs.carouselacara lumangsung).

.carousel('next')

Siklus ka item salajengna. Mulih ka nu nelepon saméméh item salajengna geus ditémbongkeun (ie saméméh slid.bs.carouselacara lumangsung).

.carousel('dispose')

Ngancurkeun carousel hiji unsur.

Kajadian

Kelas carousel Bootstrap ngungkabkeun dua acara pikeun ngaitkeun kana fungsionalitas carousel. Kadua acara gaduh sipat tambahan di handap ieu:

  • direction: Arah nu carousel nu ngageser (boh "left"atawa "right").
  • relatedTarget: Unsur DOM nu keur slid kana tempat salaku item aktip.
  • from: Indéks item ayeuna
  • to: Indéks item salajengna

Sadaya acara carousel dipecat dina carousel sorangan (ie di <div class="carousel">).

Jenis Acara Katerangan
slide.bs.carousel Kajadian ieu langsung hurung nalika slidemetodeu invoked.
slid.bs.carousel Acara ieu dipecat nalika carousel parantos réngsé transisi slide na.
$('#myCarousel').on('slide.bs.carousel', function () {
  // do something...
})

Robah durasi transisi

Durasi transisi .carousel-itemtiasa dirobih ku $carousel-transitionvariabel Sass sateuacan kompilasi atanapi gaya khusus upami anjeun nganggo CSS anu disusun. Upami sababaraha transisi diterapkeun, pastikeun transisi transformasi ditetepkeun heula (contona. transition: transform 2s ease, opacity .5s ease-out).