Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="C-carousel">
  <div class="C-slide"><h1 class="C-slide-title C-slide-title--h1">Vertical / Horizontal carousel</h1></div>
  <div class="C-slide"><div class="C-slide-title">Slide 2</div></div>
  <div class="C-slide"><div class="C-slide-title">Slide 3</div></div>
  <div class="C-slide"><div class="C-slide-title">Slide 4</div></div>
  <div class="C-slide"><div class="C-slide-title">Slide 5</div></div>
</div>

<div class="pen-controls">
  <button class="js-change-carousel-direction" data-carousel-id="C-carousel-0">Change slider direction</button>
</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
}

.C-carousel {
  background: #212121;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

.C-track {
  opacity: 1;
  transition: all 400ms ease;
  
  &--reinit {
    opacity: 0;
    transition: all 0ms linear;
  }
}

.C-slide {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: blueviolet no-repeat center / cover;
  background-blend-mode: overlay;
  font-family: arial;
  font-weight: bold;
  font-size: 22px;
  letter-spacing: 2px;
  text-transform: uppercase;
  overflow: hidden;
  
  .C-carousel.initialized & {
    border-radius: 100vw;
    transform: scale(0.5) skew(-5deg) rotate(0);
    transition: all 700ms ease;
    
    &.active {
      transform: scale(1) skew(0) rotate(0);
      border-radius: 0;
    }
  }
 
 
  @for $i from 1 through 5 {
    &:nth-child(#{$i}) {
      background-image: url(https://picsum.photos/1480/800?image=#{$i * 11});
    }
  }
 
}

.C-navigation {
  position: absolute;
  bottom: 5px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  
  .C-carousel--vertical & {
    bottom: auto;
    left: auto;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
  }
  
  @media (max-width: 480px) {
    .C-carousel--horizontal & {
      bottom: 50px;
    }
  } 
}

.C-navigation__dot {
  width: 12px;
  height: 12px;
  background: #fff;
  display: inline-block;
  margin: 4px;
  cursor: pointer;
  border-radius: 50%;
  
  &.active {
    background: lighten(blue, 10%);
    transform: scale(1.3);
  }
}

.C-carousel-navigation-button {
  position: absolute;
  top: 50%;
  left: 10px;
  z-index: 2;
  border: 2px solid #fff;
  box-shadow: none;
  border-radius: 5px;
  font-weight: bold;
  color: #fff;
  padding: 10px;
  background: transparent;
  cursor: pointer;
  transform: translateY(-50%);
  transition: all 300mes ease;
  text-transform: capitalize;
  
  &:hover {
    background: #eee;
    color: blueviolet;
  }
  
  &:active {
    background: #fff;
  }

  &--next {
    left: auto;
    right: 10px;
  }
  
  .C-carousel--vertical & {
    bottom: 10px;
    top: auto;
    left: auto;
    right: 25px;
    transform: translateY(0);
    &--next {
      right: 20px;
    }
    
    &--prev {
      transform: translatex(calc(-100% - 5px));
    }
  }
 
}

.C-slide-title {
  padding: 1rem 2rem;
  font-size: 1.5rem;
  position: relative;
  transform: translateY(100%);
  opacity: 0;
  transition: all 300ms ease;
  transition-delay: 500ms;
  
  @media (min-width: 480px) {
    font-size: 2rem;
  }
  
  @media (min-width: 768px) {
    font-size: 42px;
  }
  
  .active & {
      transform: translateY(0) translateX(0);
      opacity: 1;
   }
  
}

/* Pen controls */
.pen-controls {
  position: fixed;
  bottom: 10px;
  left: 10px;
  
  button {
    border: 2px solid blueviolet;
    box-shadow: none;
    border-radius: 0;
    font-weight: bold;
    color: blueviolet;
    padding: 10px;
    background: #fff;
    cursor: pointer;
  }
 
}
              
            
!

JS

              
                class Carousel {
  constructor(el, options = {}) {
    const DEFAULTS = {
      infinite : true,
      slideSelector: '.C-slide',
      initialSlideIndex: 0,
      vertical: false,
    };
    this.carousel = el;
    this.settings = {
      ...DEFAULTS,
      ...options,
    };

    this.slides = document.querySelectorAll(
      this.settings.slideSelector
    );

    this.state = {
      currentSlide: this.settings.initialSlideIndex,
    };

    this.track = null;
    this.navigation = null;
    this.navigationButtons = [];

    // Init
    this.init();
  }

  setState(newState, callback = () => undefined) {
    this.state = {
      ...this.state,
      ...newState,
    };
    this.updateCarousel();
    return callback();
  }
  
  getSettings = () => {
    return this.settings;
  }

  reInitWithOptions = (options = {}) => {
    this.carousel.classList.remove('initialized');
    this.track.classList.add('C-track--reinit');
    this.settings = {
      ...this.getSettings(),
      ...options,
    };
    
    this.init();
    this.onResizeActions();
    this.track.classList.remove('C-track--reinit');
  };

  init() {
    const {
      carousel,
      slides,
    } = this;

    const {
      vertical,
    } = this.settings;

    carousel.classList.remove('C-carousel--vertical');
    carousel.classList.remove('C-carousel--horizontal');
    if (vertical) {
      carousel.classList.add('C-carousel--vertical');
    } else {
      carousel.classList.add('C-carousel--horizontal');
      slides.forEach(slide => {
        slide.style.float = 'left';
      });
    }

    this.onInitActions();

    window.addEventListener('resize', () => {
      setTimeout(() => { this.onResizeActions(); }, 100);
    });

  }

  onInitActions() {
    this.createSlideTrack();
    this.createNavigation();
    this.createNavigationButtons();
    this.updateCarousel();
    this.carousel.classList.add('initialized');
  }

  onResizeActions() {
    this.setSlideTrackDimensions();
  }

  createSlideTrack() {
    const {
      slides,
      carousel,
      track,
    } = this;
    
    // Do not re-create track if it exists
    if(!track) {
      const track = document.createElement('div');
      track.classList.add('C-track');
      slides.forEach(slide => {
        track.appendChild(slide);
      });

      carousel.appendChild(track);
      this.track = track;
    }
    this.setSlideTrackDimensions();
  }

  setSlideTrackDimensions() {

    const {
      track,
      slides,
    } = this;
    slides.forEach(slide => {
      slide.style.transition = 'none';
    });
    const numberOfSlides = slides.length;

    if (!track) { return; }

    const {
      vertical,
    } = this.settings;

    const height = [...slides].reduce((acc, slide) => (
      acc + slide.offsetHeight
    ), 0);
    const width = [...slides].reduce((acc, slide) => (
      acc + slide.offsetWidth
    ), 0);
    
    track.style.transition = 'none';

    if (!!vertical) {
      track.style.width = width / numberOfSlides + 'px';
      track.style.height = height + 'px';
    } else {
      track.style.width = width + 'px';
      track.style.height = height / numberOfSlides + 'px';
    }

    track.style.transition = '';
    slides.forEach(slide => {
      slide.style.transition = '';
    });
  }

  updateTrackPosition() {
    const {
      slides,
      track,
    } = this;

    const numberOfSlides = slides.length;
    const basePercentage = 100 / numberOfSlides;

    const {
      vertical,
    } = this.settings;

    const {
      currentSlide,
    } = this.state;

    const translateValue = !!vertical
    ? `translateY(-${basePercentage * currentSlide}%)`
    : `translateX(-${basePercentage * currentSlide}%)`

    track.style.transform = translateValue;

  }

  createNavigation () {
    const {
      slides,
      carousel,
      navigation,
    } = this;
    
    if (!navigation) {
      const navigationContainer = document.createElement('div');
      navigationContainer.classList.add('C-navigation');
      [...slides].forEach((slide, index) => {
        navigationContainer.appendChild(this.createNavigationDot(index));
      });
      carousel.appendChild(navigationContainer);
      this.navigation = navigationContainer; 
    }
  }

  createNavigationDot(index) {
    const { currentSlide } = this.state;
    const navigationDot = document.createElement('div');
    navigationDot.classList.add('C-navigation__dot');
    navigationDot.setAttribute('data-slideIndex', index);
    navigationDot.addEventListener('click', () => {
      this.goTo(index);
    });

    return navigationDot;
  }

  createNavigationButtons() {
    const createNavigationButton = (buttonType) => {
      const { carousel } = this;
      const navigationButton = document.createElement('button');
      navigationButton.setAttribute('type', 'button');
      navigationButton.classList.add('C-carousel-navigation-button');
      navigationButton.classList.add(`C-carousel-navigation-button--${buttonType}`);
      navigationButton.addEventListener('click', () => {
        const { slides } = this;
        const { currentSlide } = this.state;
        const numberOfSLides = slides.length;
        const lastSlideIndex = numberOfSLides - 1;
        
        if (buttonType === 'next') {
          const slideToGo = currentSlide + 1;
          if (slideToGo > lastSlideIndex) {
            if(!!this.settings.infinite) {
              return this.goTo(0);
            }
            return;
          } else {
            this.goTo(slideToGo);
          }
          
        } else {
          const slideToGo = currentSlide - 1;
          if (slideToGo < 0) {
            if(!!this.settings.infinite) {
              return this.goTo(lastSlideIndex);
            }
            return;
          } else {
            this.goTo(slideToGo);
          }
        }
      });
      
      navigationButton.textContent = buttonType;
      carousel.appendChild(navigationButton);
      this.navigationButtons.push(navigationButton);
    }
    
    if (this.navigationButtons.length <= 0) {
      createNavigationButton('next');
      createNavigationButton('prev');
    } 
  }

  updateNavigation() {
    const {
      navigation,
    } = this;

    const {
      currentSlide,
    } = this.state;

    const navigationDots = navigation.querySelectorAll('.C-navigation__dot');
    navigationDots.forEach(dot => {
      const dotIndex = parseInt(dot.getAttribute('data-slideIndex'), 10);
      if (dotIndex === currentSlide) {
        dot.classList.add('active');
      } else {
        dot.classList.remove('active');
      }
    });
  }

  updateSlides() {
    const {
      slides,
    } = this;

    const {
      currentSlide,
    } = this.state;

    slides.forEach((slide, index) => {
      if (index === currentSlide) {
        slide.classList.add('active');
      } else {
        slide.classList.remove('active');
      }
    });
  }

  goTo = (index) => {
    this.setState({
      currentSlide: index,
    }, () => {
      //console.log('Current slide is now: ', this.state.currentSlide);
    });
  }

  updateCarousel() {
    this.updateTrackPosition();
    this.updateNavigation();
    this.updateSlides();
  }

}

const registeredCarousels = [];
const carousels = document.querySelectorAll('.C-carousel');
carousels.forEach((carousel, index) => {
  registeredCarousels.push({  
    id: `C-carousel-${index}`,
    carousel: new Carousel(carousel, {
      vertical: true,
    }),
  })
});

const changeCarouselDirectionButton = document.querySelector('.js-change-carousel-direction');
changeCarouselDirectionButton.addEventListener('click', function(event) {
  const carouselId = this.getAttribute('data-carousel-id');
  const carouselToChange = registeredCarousels.find(carousel => carousel.id === carouselId);
  const settings = carouselToChange.carousel.getSettings();
  const { vertical } = settings;
  carouselToChange.carousel.reInitWithOptions({
    vertical: !vertical,
  });

});
              
            
!
999px

Console