Display the adjacent slides to the active slide in the Bootstrap 4 carousel for better visibility

I have a unique challenge ahead as I am creating a full-screen Bootstrap 4 carousel that features videos instead of traditional images, accompanied by caption overlays.

My vision for this project is to position the captions on the left and right sides of the active slide, overlaying the slider controls so that they appear to be integrated into the navigation. This desired effect can be visualized in the illustration below:

https://i.sstatic.net/wfHpa.png

To implement this concept, I've developed the following code snippet:

var slider = $('#full_slider');
var slidesNo = slider.find('.carousel-item').length;

var fullSliderNavigation = function(index) {
  var slide = slider.find('.carousel-item').eq(index);
  if (slide.is(':first-child')) {
    var slidePrev = slider.find('.carousel-item').eq(slidesNo - 1);
  } else {
    var slidePrev = slider.find('.carousel-item').eq(index - 1);
  }

  if (slide.is(':last-child')) {
    var slideNext = slider.find('.carousel-item').eq(0);
  } else {
    var slideNext = slider.find('.carousel-item').eq(index + 1);
  }

  $('.carousel-item').removeClass('right-slide left-slide');
  slideNext.addClass('right-slide');
  slidePrev.addClass('left-slide');
}

$(document).ready(function() {
  fullSliderNavigation(0);

  $(slider).on('slide.bs.carousel', function(event) {
    var index = $(event.relatedTarget).index();
    fullSliderNavigation(index);
  });
});
#full_slider {
  position: relative;
  justify-content: flex-start;
  align-items: center;
}
#full_slider .carousel-item {
  position: relative;
  height: 100vh;
  justify-content: center;
  align-items: center;
}
#full_slider .carousel-item .video-caption {
  position: absolute;
  left: 0;
  top: 50%;
  transition: all 500ms;
  transform: translateX(-60px) translateY(-50%);
  width: 100%;
  max-width: 600px;
  color: #fff;
}
#full_slider .carousel-item .allcases {
  font-size: 18px;
  margin-top: auto;
  display: none;
}
#full_slider .carousel-item.active,
#full_slider .carousel-item-left,
#full_slider .carousel-item-right {
  display: flex !important;
}
#full_slider video {
  position: absolute;
  left: 0;
  top: 0;
  width: 100vw;
  height: auto;
  z-index: -1;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<div class="page-wrapper">
  <div id="full_slider" class="carousel slide" data-ride="carousel" data-interval="false">
    <div class="carousel-inner" role="listbox">
      <div class="carousel-item">
        <video src="https://code-love.tk/video/flamenco.mp4" autoplay loop muted></video>
        <div class="video-caption">
          <h3 class="capt text-boldest">Lorem ipsum dolor</h3>
          <p class="allcases">
            <a class="inherit" href="#">See more</a>
          </p>
        </div>
      </div>
      <div class="carousel-item">
        <video src="https://code-love.tk/video/protest.mp4" autoplay loop muted></video>
        <div class="video-caption">
          <h3 class="capt text-boldest">Falling in love</h3>
          <p class="allcases">
            <a class="inherit" href="#">See more</a>
          </p>
        </div>
      </div>
      <div class="carousel-item">
        <video src="https://code-love.tk/video/commerciala.mp4" autoplay loop muted></video>
        <div class="video-caption">
          <h3 class="capt text-boldest">Coffe</h3>
          <p class="allcases">
            <a class="inherit" href="#">See more</a>
          </p>
        </div>
      </div>
    </div>
    <a class="carousel-control carousel-control-prev" href="#full_slider" role="button" data-slide="prev">
 <span class="control text-left"></span>
 <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control carousel-control-next" href="#full_slider" role="button" data-slide="next">
 <span class="control text-left"></span>
 <span class="sr-only">Next</span>
   </a>
  </div>
</div>

A notable hurdle I've encountered is that due to certain CSS properties set to display: none; on the slides adjacent to the active one, their captions remain invisible.

This behaviour is intrinsic to how Bootstrap 4 carousels typically function. What approach should I take to reveal these captions without impeding the functionality of the carousel?

Answer №1

Live Demo :

View the full page demo here: https://codepen.io/devanshj/full/MXXdvO/

Demo with code snippets available at: https://codepen.io/devanshj/pen/MXXdvO

Explanation of Implementation :

  1. We utilized the Flickity library to simplify our task.

  2. To align the content of adjacent slides with the selected slide, we applied the following CSS translations:

    translateX(calc( (100vw-100%)/2 + 5vw )*-1)
    and
    translateX(calc( (100vw-100%)/2 + 5vw ))
    for previous and next slides respectively as default .content is centered.

  3. An event handler for flickity.scroll was added to adjust the marginLeft property according to the movement of the selected slide, ensuring that .content returns to its original centered position eventually.

Note: The autoplay feature on the video might not function correctly due to being within an iframe.

Answer №2

If you are looking to create a dynamic slider, consider using the "flickity" jQuery plugin. Here is an example of how you can implement it:

$('#full_slider').flickity({
  wrapAround: true,
  on: {
    ready: function() {
        $('.carousel-cell:first-child()').find('video').get(0).play();
    },
    change: function( index ) {
        $('.is-selected').find('video').get(0).play();
    }
}
});
#full_slider .slide-content {
  position: relative;
  height: 100vh;
  width: 70vw;
  justify-content: center;
  align-items: center;
}

#full_slider .slide-content .video-caption {
  position: absolute;
  left: 50%;
  top: 50%;
  transition: all 500ms;
  transform: translate(-50%, -50%);
  width: 100%;
  max-width: 600px;
  color: #fff;
}
#full_slider .slide-content .allcases {
  font-size: 18px;
  margin-top: auto;
  display: none;
}
#full_slider .slide-content .allcases a {
  color: #fff;
}


#full_slider video {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 100%;
  height: auto;
  z-index: -1;
  transform: translate(-50%, -50%);
}
#full_slider h3 {
  font-weight: 900;
  font-size: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- CSS -->
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
<!-- JavaScript -->
<script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script>

<div class="page-wrapper">
<div class="main-carousel" id="full_slider">
<div class="carousel-cell is-selected">
<div class="slide-content">
<video src="https://code-love.tk/video/koffee.mp4" autoplay loop muted></video>
<div class="video-caption">
<h3 class="capt text-boldest">All about us</h3>
<p class="allcases">
<a class="inherit" href="#">See more</a>
</p>
</div>
</div>
</div>
<div class="carousel-cell">
<div class="slide-content">
<video src="https://code-love.tk/video/flamenco.mp4" autoplay loop muted></video>
<div class="video-caption">
<h3 class="capt text-boldest">Lorem ipsum dolor</h3>
<p class="allcases">
<a class="inherit" href="#">See more</a>
</p>
</div>
</div>
</div>
<div class="carousel-cell">
<div class="slide-content">
<video src="https://code-love.tk/video/protest.mp4" autoplay loop muted></video>
<div class="video-caption">
<h3 class="capt text-boldest">Falling in love</h3>
<p class="allcases">
<a class="inherit" href="#">See more</a>
</p>
</div>
</div>
</div>
<div class="carousel-cell">
<div class="slide-content">
<video src="https://code-love.tk/video/commerciala.mp4" autoplay loop muted></video>
<div class="video-caption">
<h3 class="capt text-boldest">Coffe</h3>
<p class="allcases">
<a class="inherit" href="#">See more</a>
</p>
</div>
</div>
</div>
<div class="carousel-cell">
<div class="slide-content">
<video src="https://code-love.tk/video/commerciala.mp4" autoplay loop muted></video>
<div class="video-caption">
<h3 class="capt text-boldest">Dealing with danger</h3>
<p class="allcases">
<a class="inherit" href="#">See more</a>
</p>
</div>
</div>
</div>
</div>
</div>

To access the "flickity" jQuery plugin and learn more about its capabilities, visit their official website.

Answer №3

To incorporate captions from the left and right slides as controls for the slider, you can use jQuery to copy the content of the captions to the corresponding controls. Below is the code snippet with some CSS for proper display of the content; feel free to add your own custom styles:

var slider = $('#full_slider');
var slidesNo = slider.find('.carousel-item').length;

var fullSliderNavigation = function(index) {
  var slide = slider.find('.carousel-item').eq(index);
  if (slide.is(':first-child')) {
    var slidePrev = slider.find('.carousel-item').eq(slidesNo - 1);
  } else {
    var slidePrev = slider.find('.carousel-item').eq(index - 1);
  }

  if (slide.is(':last-child')) {
    var slideNext = slider.find('.carousel-item').eq(0);
  } else {
    var slideNext = slider.find('.carousel-item').eq(index + 1);
  }

  $('.carousel-item').removeClass('right-slide left-slide');
  slideNext.addClass('right-slide');
  slidePrev.addClass('left-slide');
  
  $('.carousel-control-next').html(slideNext.find('.video-caption').html());
  $('.carousel-control-prev').html(slidePrev.find('.video-caption').html());
}

$(document).ready(function() {
  fullSliderNavigation(0);

  $(slider).on('slide.bs.carousel', function(event) {
    var index = $(event.relatedTarget).index();
    fullSliderNavigation(index);
  });
});
#full_slider {
  position: relative;
  justify-content: flex-start;
  align-items: center;
}
#full_slider .carousel-item {
  position: relative;
  height: 100vh;
  justify-content: center;
  align-items: center;
}
/* More CSS styles here */
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<div class="page-wrapper">
  <div id="full_slider" class="carousel slide" data-ride="carousel" data-interval="false">
    <div class="carousel-inner" role="listbox">
      <!-- Carousel items will go here -->
      
      <a class="carousel-control carousel-control-prev" href="#full_slider" role="button" data-slide="prev"></a>
      
      <a class="carousel-control carousel-control-next" href="#full_slider" role="button" data-slide="next"></a>
    </div>
  </div>
</div>

Answer №4

Styles have been applied to the specified div

No modifications are necessary in JS

#full_slider .carousel-item.left-slide,
#full_slider .carousel-item.right-slide {
  position: absolute;
  top: 0;
  display: block;
}

#full_slider .carousel-item.left-slide {
  z-index: 2;
  left: -90%;
  overflow: hidden;
}

#full_slider .carousel-item.active {
  width: 100%;
  z-index: 1;
}

#full_slider .carousel-item.right-slide {
  z-index: 2;
  right: -90%;
}

#full_slider .carousel-item.left-slide video,
#full_slider .carousel-item.right-slide video {
  display: none;
}

.carousel-control-next,
.carousel-control-prev {
  z-index: 5;
}

#full_slider .carousel-item.left-slide .video-caption {
  right: 0;
  left: auto;
  text-align: right;
}

#full_slider .carousel-item.left-slide .video-caption,
#full_slider .carousel-item.right-slide .video-caption {
  transform: translate(0, -50%);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with table formatting occurs exclusively in Chrome browser

I'm really struggling to figure this out - the table functions perfectly on Firefox and IE, but not on Chrome. Take a look at it: On Chrome: Here is the CSS for the table: table { padding: 24px; margin: 0 auto; width: 550px; } The ...

Is there a way to automatically populate an AngularJS input field?

Attempting to automate filling out a website using JavaScript. The script below: document.getElementsByClassName('form-control')[1].value = "MYNAME"; The input text changes, but upon clicking the submit button it displays as empty. Any as ...

What is the best way to find a specific string within an array of strings?

I have a list of tasks as strings: todo=[ 'Get up', 'Brush my teeth', 'Go to work', 'Play games' ]; I am attempting to compare it with this: Template: <input (input)="checkArrays($event)" /> In my ...

What could be causing the browser.get method to fail to redirect to the specified URL?

I have organized my files in a folder structure that looks like this: project structure Currently, I am following the steps outlined in this particular tutorial The contents of my package.json file are as follows: { "name": "node_cucumber_e2e", "ver ...

Centering a div vertically results in a slight horizontal misalignment

I'm attempting to vertically center a div using the position and transform CSS method. However, it seems to be slightly off-centered horizontally. Here is my code: HTML: <div class="row container-fluid top_header"> <div class="col-12 ...

Harnessing the power of JavaScript functions to display an image when clicked

Looking for help with making an image appear when clicking on one of three images? Despite trying different approaches, the desired result is still not achieved. I'm aware of using if else statements but exploring other methods. Any insights on what m ...

What is the method for squaring the sides of the top border?

Can the top border be squared (straightened) as shown in the image below? https://i.sstatic.net/7GKhj.png .nav.nav-tabs.custom-nav-tabs .nav-link { border-radius: 0px; } .nav.nav-tabs.custom-nav-tabs .nav-link.active { border-top: 2px solid gr ...

Tips for positioning the calendar icon inside the input field using Angular Material

Just beginning my journey with Angular Material and currently focusing on building a datepicker. I am looking to reposition the icon from outside of the input field to inside. After trying out various Material themes, none seem to have this specific style. ...

Mastering the Art of Nodemailer

Can anyone advise on how to utilize nodemailer with dynamic email and password retrieved from a database? export const mailTransporter = nodemailer.createTransport({ service: 'gmail', auth: { user: email_user, pass: email_pass, }, } ...

Mootools tweening color transition denied for child elements when color is explicitly defined in CSS

Check out this link for testing: http://jsfiddle.net/EnJSM/ You might observe that by removing "color: #6CB5FF;", the transition behaves differently - it only works for the second part of the line. I'm interested to see what solution you come up wit ...

Ensure that the bar width in react-chart-js-2 remains consistent regardless of the number of bars displayed

Currently, I am developing a React Chart JS component that displays a set of horizontal bars. My objective is to maintain the width of the bars at a consistent size regardless of the number of bars present (e.g., all bars at 30px). However, when additiona ...

Is there a way to implement jQuery for displaying and hiding DIVs according to checkboxes in a form submitted from a separate page?

How can I achieve the functionality of submitting a form on one page, and then dynamically showing or hiding DIVs on the next page based on the checkboxes that were selected? It's also important for the script to display custom messages depending on w ...

Extracting Referring Page URLs Using jQuery

Is there a way to extract URLs from an referring page using jQuery? The URLs are embedded inside <a> elements, similar to this HTML structure: <a href="http://localhost/sr19repairables/vehicles/repairables/2011-chrysler-town-country-touring-miniv ...

Adding the Edit action in React-Redux is a crucial step towards creating a dynamic

I am looking to add an edit action to my blog page in addition to the existing ADD, DELETE and GET actions. Any suggestions on how I can implement the EDIT action to make my blog editable with just a button click? Your help would be greatly appreciated. ...

What is the TypeScript equivalent of the Java interface.class?

Can you write a Java code in TypeScript that achieves the same functionality as the code below: Class<?> meta = Object.class; and meta = Processor.class; // Processor is an interface In TypeScript, what would be the equivalent of .class? Specifica ...

Server returning emptiness in response to jQuery AJAX POST request

When I send this AJAX request, the array on the server ends up empty. Can anyone offer some assistance? I can confirm in Firebug that the request is being sent. $.ajax({ type: 'POST', url: 'presupuesto_guardar.php', data: ...

Issue showing hidden content that was loaded using ajax

I'm currently working on a personal project where I am using ajax and jQuery to load elements onto a page. However, I am facing an issue with displaying specific elements under certain conditions after they have been appended to the DOM. There are ce ...

404 error on GitHub Pages due to inability to load resource

Despite researching similar topics for help, I am still struggling to resolve my issue. I recently completed a project and am attempting to host it on Github pages. You can view the project here: However, upon checking my pages, I encountered 404 errors ...

Issue with ng-grid in AngularJS: Data does not appear after resizing columns

Currently, I am encountering a problem where ng-grid data is not being displayed when column resizing occurs after changing the column names. To demonstrate this issue, I have created a plunkr at - http://plnkr.co/edit/eV9baoDOV9A46FZmKW8G?p=preview I wo ...

Using the debug module, we're able to set up debugging for our Express application with the specific tag "express-locallibrary-tutorial:server". I am curious to understand the purpose and significance of this setup

I've been diving into backend development with Express lately. I decided to work on the express-locallibrary-tutorial project from GitHub. However, I'm having trouble grasping something. var debug = require('debug')('express-locall ...