Bootstrap 4 - Carousel
Descrizione
Carousel è un modo flessibile e reattivo per aggiungere un dispositivo di scorrimento al tuo sito. Per creare una giostra,
Aggiungi le classi .carousel e .slide al contenitore insieme a un ID.
Specificare le diapositive in un <div> con la classe .carousel-inner e ogni diapositiva definita con la classe .carousel-item .
Aggiungi la classe .active a una delle diapositive per rendere visibile il carosello; altrimenti la giostra non sarà visibile.
Giostra di base
Una semplice presentazione di seguito mostra un carosello di base con indicatori e controlli:
Esempio
<html lang = "en">
<head>
<!-- Meta tags -->
<meta charset = "utf-8">
<meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
<!-- Bootstrap CSS -->
<link rel = "stylesheet"
href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin = "anonymous">
<title>Bootstrap 4 Example</title>
</head>
<body>
<div class = "container">
<h2>Basic Carousel</h2>
<div id = "carouselwithIndicators" class = "carousel slide w-50" 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 = "2s"></li>
</ol>
<div class =" carousel-inner">
<div class = "carousel-item active">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide1.png"
alt = "First slide">
</div>
<div class = "carousel-item">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide2.png"
alt = "Second slide">
</div>
<div class = "carousel-item">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide3.png"
alt = "Third slide">
</div>
</div>
<a class = "carousel-control-prev" href = "#carouselwithIndicators" 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 = "#carouselwithIndicators" role = "button" data-slide = "next">
<span class = "carousel-control-next-icon" aria-hidden = "true"></span>
<span class = "sr-only">Next</span>
</a>
</div>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin = "anonymous">
</script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin = "anonymous">
</script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin = "anonymous">
</script>
</body>
</html>
Produrrà il seguente risultato:
Produzione
Note- Nell'esempio, abbiamo usato w-50 per il carosello e w-100 per le immagini, che fornisce una larghezza del 50% e un margine sinistro e destro di auto e una larghezza del 100% per le immagini del carosello.
Didascalie delle diapositive
Aggiungi le didascalie alla presentazione utilizzando la classe .carousel-caption all'interno della classe .carousel-item .
L'esempio seguente mostra l'aggiunta di didascalie alla presentazione:
Esempio
<html lang = "en">
<head>
<!-- Meta tags -->
<meta charset = "utf-8">
<meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
<!-- Bootstrap CSS -->
<link rel = "stylesheet"
href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin = "anonymous">
<title>Bootstrap 4 Example</title>
</head>
<body>
<div class = "container">
<h2>Carousel with Caption</h2>
<div id = "carouselwithIndicators" class = "carousel slide w-50" 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 = "https://www.tutorialspoint.com/bootstrap/images/slide1.png"
alt = "First slide">
<div class = "carousel-caption d-none d-md-block">
<h5>HTML</h5>
<p>Hypertext Markup Language</p>
</div>
</div>
<div class = "carousel-item">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide2.png"
alt = "Second slide">
<div class = "carousel-caption d-none d-md-block">
<h5>CSS</h5>
<p>Cascading Style Sheets</p>
</div>
</div>
<div class = "carousel-item">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide3.png"
alt = "Third slide">
<div class = "carousel-caption d-none d-md-block">
<h5>PHP</h5>
<p>Hypertext Preprocessor</p>
</div>
</div>
</div>
<a class = "carousel-control-prev" href = "#carouselwithIndicators" 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 = "#carouselwithIndicators" role =" button" data-slide = "next">
<span class = "carousel-control-next-icon" aria-hidden = "true"></span>
<span class = "sr-only">Next</span>
</a>
</div>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin = "anonymous">
</script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin = "anonymous">
</script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin = "anonymous">
</script>
</body>
</html>
Produrrà il seguente risultato:
Produzione
Dissolvenza incrociata
Puoi aggiungere un effetto di transizione dissolvenza per animare le diapositive del carosello utilizzando la classe .carousel-fade come mostrato nell'esempio seguente:
Esempio
<html lang = "en">
<head>
<!-- Meta tags -->
<meta charset = "utf-8">
<meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
<!-- Bootstrap CSS -->
<link rel = "stylesheet"
href = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity = "sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin = "anonymous">
<title>Bootstrap 4 Example</title>
</head>
<body>
<div class = "container">
<h2>Carousel with Crossfade</h2>
<div id = "carouselExampleFade" class = "carousel carousel-fade w-50" data-ride = "carousel">
<div class = "carousel-inner">
<div class = "carousel-item active">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide1.png"
alt = "First slide">
</div>
<div class = "carousel-item">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide2.png"
alt = "Second slide">
</div>
<div class = "carousel-item">
<img class = "d-block w-100"
src = "https://www.tutorialspoint.com/bootstrap/images/slide3.png"
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>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src = "https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin = "anonymous">
</script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity = "sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin = "anonymous">
</script>
<script src = "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity = "sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin = "anonymous">
</script>
</body>
</html>
Produrrà il seguente risultato: