Creating a Carousel Using Only Jade(Pug), Javascript, jQuery and CSS

Ai Santos
HackerNoon.com
5 min readJun 16, 2018

--

STEP 1: Create divs inside of your page that will contain the carousel elements

STEP 2: Set the styles for all of the elements inside of your carousel:

The Displayed Image Gallery:

  • The div around the display for the selected image
  • The div that contains the div that contains the image itself
  • The image itself

The Filmstrip that contains thumbnails of the images to be displayed:

  • The container for the filmstrip
  • Everything inside of the filmstrip
  • Position everything inside of the filmstrip in the center
  • Animation for the images in the filmstrip
  • Set a height and width for all images in the container

STEP 3: Create functions using jQuery and Javascript that will allow you to iterate through your images using the styles and animations you just created.

  • Initialize the Gallery
  • Determine the indices that will be visible in the filmstrip and use an offset to to ensure that whatever index we are on gives us either a result of 0, 1, 2, 3, 4. How do we do this by using a for loop and modulo
  • Determine the Z Index, Opacity, and Transform for each position depending on where it is on the filmstrip
  • Determine the index of the image (cell) based on its location in the filmstrip (array)
  • the most complex part of the function — positioning the image so that it applies the css styles to that specific index to be displayed within an array that contains 5 image elements

Whoa!! What is happening here? Let’s break it out and go over it step by step:

  • create a function that positions the gallery filmstrip and set that to animate=true
  • assign some variables that will contain the filmstrip element, the number of cells, and the children of the filmstrip
  • if the offset is less than zero then we want to add the total number of cells to that amount. For example if we are at offset -1 and we have an array of images with the length 39, our new offset value will be 38.
  • if the above condition is true, which means the cell is visible then pass the new offset, length, and number of cells into the function visibleIndices. We will then store the value of this function into visibleCells
  • if animate is true then we will add the animated class to the filmstrip, otherwise do not add that class
  • now apply the following css styles to each cell which will give it the opacity, zIndex, and transform functions we need for each cell depending on its position in the filmstrip
  • Now we need to assign the attribute to the images that will allow us to fade it out and in
  • last but not least we need to determine if a cell is before or after the cell

--

--