Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap CSS/Javascript For Carousel Only

Bootstrap CSS/Javascript For Carousel Only

Currently using Carousel With Controls

https://getbootstrap.com/docs/4.0/components/carousel/

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active">
     <video controls="true">
      <source src="www.youtube.com/watch?v=3bGNuRtlqAQ" type="video/mp4" />
    </video>
    </div>
    <div class="carousel-item">
     <video controls="true">
       <source src="www.youtube.com/watch?v=3bGNuRtlqAQ" type="video/mp4" />
    </video>

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

Anyone have CSS Codes and Javascript Codes for this function ? (Don't want the whole file as it would conflict with my main CSS)

like image 532
greenboxgoolu Avatar asked Jan 07 '20 03:01

greenboxgoolu


3 Answers

I found an old Pen and customized it a bit to suit your needs.

Here is a standalone Bootstrap 4 Carousel with no dependencies (even jQuery):

Update 1: Added images source and set the default height to 70vh

Update 2: Added video - iframe support. It seems that SOF is not allowing video/iframe backgrounds so here is the a codepen source.

const carousel = document.getElementById('carouselExampleControls')
const items = carousel.querySelectorAll('.carousel-item');
let currentItem = 0;
let isActive = true;

function setCurrentItem(index) {
  currentItem = (index + items.length) % items.length;
}

function hideItem(direction) {
  isActive = false;
  items[currentItem].classList.add(direction);
  items[currentItem].addEventListener('animationend', function() {
    this.classList.remove('active', direction);
  });
}

function showItem(direction) {
  items[currentItem].classList.add('next', direction);
  items[currentItem].addEventListener('animationend', function() {
    this.classList.remove('next', direction);
    this.classList.add('active');
    isActive = true;
  });
}

document.getElementById('carouselPrev').addEventListener('click', function(e) {
  e.preventDefault()
  if (isActive) {
    hideItem('to-right');
    setCurrentItem(currentItem - 1);
    showItem('from-left');
  }
});

document.getElementById('carouselNext').addEventListener('click', function(e) {
  e.preventDefault()
  if (isActive) {
    hideItem('to-left');
    setCurrentItem(currentItem + 1);
    showItem('from-right');
  }
});
*,
*::after,
*::before {
  box-sizing: border-box;
}

img {
  display: block;
  vertical-align: middle;
}

body {
  margin: 0;
  font-family: sans-serif;
}

.carousel {
  position: relative;
}

.carousel-inner {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.carousel-inner>.carousel-item {
  position: relative;
  display: none;
  animation: 0.6s ease-in-out;
  height: 70vh; /* Set your height */
}

.carousel-item>.carousel-img {
  width: 100%;
  min-height: 70vh; /* same height */
  height: auto;
}
.carousel-item.carousel-video {
  display: block;
  object-fit: cover;
  position: absolute;
  top: 50%;
  left: 50%;
  min-width: 100%;
  min-height: 100%;
  width: auto;
  height: auto;
  z-index: 0;
  -webkit-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
  /* uncomment the following line if you want to prevent mouse (or touch) clicks */
  /* pointer-events: none; */
}
.carousel-inner>.active,
.carousel-inner>.next {
  display: block;
}

.carousel-inner>.next {
  position: absolute;
  top: 0;
  width: 100%;
}

.carousel-inner>.to-left {
  animation-name: left;
}

.carousel-inner>.from-right {
  animation-name: right;
}

.carousel-inner>.to-right {
  animation-name: right;
  animation-direction: reverse;
}

.carousel-inner>.from-left {
  animation-name: left;
  animation-direction: reverse;
}

.carousel-control {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 15%;
  cursor: pointer;
}

.carousel-control-prev,
.carousel-control-next {
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 1;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-pack: center;
  justify-content: center;
  width: 15%;
  color: #fff;
  text-align: center;
  opacity: 0.5;
  transition: opacity 0.15s ease;
}

@media (prefers-reduced-motion: reduce) {
  .carousel-control-prev,
  .carousel-control-next {
    transition: none;
  }
}

.carousel-control-prev:hover,
.carousel-control-prev:focus,
.carousel-control-next:hover,
.carousel-control-next:focus {
  color: #fff;
  text-decoration: none;
  outline: 0;
  opacity: 0.9;
}

.carousel-control-prev {
  left: 0;
}

.carousel-control-next {
  right: 0;
}

.carousel-control-prev-icon,
.carousel-control-next-icon {
  display: inline-block;
  width: 20px;
  height: 20px;
  background: no-repeat 50% / 100% 100%;
}

.carousel-control-prev-icon {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e");
}

.carousel-control-next-icon {
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M2.75 0l-1.5 1.5L3.75 4l-2.5 2.5L2.75 8l4-4-4-4z'/%3e%3c/svg%3e");
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.sr-only-focusable:active,
.sr-only-focusable:focus {
  position: static;
  width: auto;
  height: auto;
  overflow: visible;
  clip: auto;
  white-space: normal;
}

@keyframes left {
  from {
    left: 0;
  }
  to {
    left: -100%;
  }
}

@keyframes right {
  from {
    left: 100%;
  }
  to {
    left: 0;
  }
}
<div id="carouselExampleControls" class="carousel">
  <div class="carousel-inner">
    <!-- YouTube Video -->
    <div class="carousel-item active">
      <iframe class="carousel-video" width="100%" height="100%" src="https://www.youtube.com/embed/QEbuc3cgmsI" frameborder="0"
        allowfullscreen></iframe>
      <!-- add "?autoplay=1" at the end of the URL for autoplay i.e. https://www.youtube.com/embed/QEbuc3cgmsI?autoplay=1 -->
    </div>
    <!-- Local Video -->
    <div class="carousel-item">
      <video class="carousel-video" autoplay muted loop playsinline preload="metadata"
        poster="http://techslides.com/demos/sample-videos/small.jpg">
        <source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
        <source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
        <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
      </video>
    </div>
    <!-- Image -->
    <div class="carousel-item">
      <img class="carousel-img" alt="Second slide" src="https://source.unsplash.com/collection/190728/1920x1080">
    </div>
  </div>

  <a id="carouselPrev" class="carousel-control-prev" href="#carouselExampleControls" role="button">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a id="carouselNext" class="carousel-control-next" href="#carouselExampleControls" role="button">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
like image 89
awran5 Avatar answered Oct 28 '22 08:10

awran5


If you are using bundler like Webpack, your could import and compile individual components as explained in here

like image 2
ibex Avatar answered Oct 28 '22 07:10

ibex


If you don't have to use bootstrap for your website for other sections except slider section then I have suggest you to use external library only for slider because from bootstrap libraries it is not easy to filter only slider code.

This is the reference website link for only slider: https://kenwheeler.github.io/slick/

This slier is easy to customize and there are so many options to set slider properties using Jquery.

I hope this will help you to solve your problem and this is only SLider Library so it will not conflict your main css so you can easily add this library to your code.

Thank you...

like image 2
KuldipKoradia Avatar answered Oct 28 '22 07:10

KuldipKoradia