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

Auto Save

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="view view-carousel">
  <h2 class="view-title">Carousel A</h2>
  <div class="view-content">
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title A</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title B</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title C</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title D</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title E</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title F</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title G</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title H</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title I</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title J</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title K</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title L</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title M</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title N</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title O</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title P</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title Q</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title R</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title S</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title T</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title U</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title V</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title W</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title X</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title Y</h3>
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-a">
        <img src="https://via.placeholder.com/300x450" />
        <h3>Item Title Z</h3>
      </div>
    </div>

  </div>
</div>
<div class="view view-carousel">
  <h2 class="view-title">Carousel B</h2>
  <div class="view-content">
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title A</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title B</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title C</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title D</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title E</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title F</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title G</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title H</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title I</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title J</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title K</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title L</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title M</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title N</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title O</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title P</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title Q</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title R</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title S</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title T</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title U</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title V</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title W</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title X</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title Y</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
    <div class="views-row">
      <div class="item-type-b">
        <h3>Item Title Z</h3>
        <img src="https://via.placeholder.com/600x300" />
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                $button-size-bp: 600px;
$button-size-small: 32px;
$button-margin-small: 5px;
$button-margin: 10px;
$button-size: 50px;
$slide-margin: 5px;

.view-carousel {
  width: 100%;
  padding: 20px #{($button-size-small * 1.2) + (2 * $button-margin-small)};
  box-sizing: border-box;
  background: #efefef;
  border-bottom: 1px solid #000;
  display: block;
  margin: 0;

  @media screen and (min-width: $button-size-bp) {
    padding-left: #{($button-size * 1.2) + (2 * $button-margin)};
    padding-right: #{($button-size * 1.2) + (2 * $button-margin)};
  }
}
.slick-list {
  width: 100%;
  overflow: hidden;
}
.view-content {
  position: relative; // keep buttons vertically centered relative to slides
}

.view-content,
.slick-track {
  display: flex; // to keep items equal height
  flex-wrap: nowrap;
  flex-direction: row;
  width: 100%;
}

.views-row {
  display: block;
  flex: 0 0 auto;
  // vertical-align: top;
  // white-space: normal;
  margin: $slide-margin;

  &:first-child {
    margin-left: 2 * $slide-margin;
  }
  &:last-child {
    margin-right: 2 * $slide-margin;
  }

  > * {
    max-width: 100%;
  }
}

.carousel-button {
  height: $button-size-small * 1.2;
  width: $button-size-small * 1.2;
  line-height: 1.2;
  color: orange;
  position: absolute;
  top: 50%;
  z-index: 99;
  font-size: $button-size-small;
  transform: translateY(-50%);
  border: 0;
  text-align: center;
  padding: 0;
  margin: $button-margin-small;
  text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.6);
  transition: text-shadow 0.2s;
  cursor: pointer;
  background: transparent;

  @media screen and (min-width: $button-size-bp) {
    height: $button-size * 1.2;
    width: $button-size * 1.2;
    margin: $button-margin;
    font-size: $button-size;
  }

  &:hover,
  &:focus {
    text-shadow: none;
  }

  &.slick-disabled {
    color: #ddd;
    text-shadow: none;
    cursor: not-allowed;
    pointer-events: none;
  }
}
.carousel-button--prev {
  right: 100%;
  @media screen and (min-width: $button-size-bp) {
    right: 100%;
  }
}
.carousel-button--next {
  left: 100%;
  @media screen and (min-width: $button-size-bp) {
    left: 100%;
  }
}

//Item styling, independent of carousel
.item-type-a {
  box-shadow: 2px 2px 4px #000;
  // margin: 6px;
  width: 235px;
  border-left: 1px solid blue;
  border-right: 1px solid red;
  h3 {
    text-align: center;
    margin: 0.5em 0 0;
  }
  img {
    width: 100%;
  }
}

.item-type-b {
  width: 100%;
  padding: 10px 0 0;
  background: #54bc3a;
  border-left: 1px solid blue;
  border-right: 1px solid red;
  img {
    width: 100%;
    display: block;
  }
  h3 {
    margin: 0.5em;
  }
}

              
            
!

JS

              
                const slideSelector = ".views-row";
const $carousels = $(".view-carousel .view-content");

const prevBtn =
  '<button class="carousel-button carousel-button--prev" type="button" aria-label="previous"><i class="fas fa-arrow-circle-left"></i></button>';
const nextBtn =
  '<button class="carousel-button carousel-button--next" type="button" aria-label="next"><i class="fas fa-arrow-circle-right"></i></button>';
const carouselDefaults = {
  dots: false,
  infinite: false,
  nextArrow: nextBtn,
  prevArrow: prevBtn,
  speed: 500,
  centerMode: false,
  variableWidth: false,
  mobileFirst: true,
  slidesToScroll: 4,
  slidesToShow: 4
};

/**
 * Calculate the number of slides to show
 * and the number of slides to scroll
 */
function slidesTo($carousel) {
  const $slides = $carousel.find(slideSelector);
  $slides.removeAttr("style");
  const $first = $slides.first();
  let slideWidth = $first.width();
  let toShow = 1;
  let toScroll = 1;
  let carouselWidth = $carousel.width();
  if (slideWidth * 1.2 < carouselWidth) {
    toShow = carouselWidth / slideWidth; // show a decimal peeking out
    toScroll = Math.floor(carouselWidth / slideWidth); // scroll only the fully visible
  }
  var options = {
    slidesToShow: toShow,
    slidesToScroll: toScroll
  };
  console.log(options);
  return options;
}

/**
 * Build options based on slides and screen size
 */
function carouselOptions($carousel) {
  var options = carouselDefaults;
  Object.assign(options, slidesTo($carousel));
  console.log(options);
  // Set any properties from the .view-carousel attributes
  for (var prop in options) {
    if (Object.prototype.hasOwnProperty.call(options, prop)) {
      // do stuff
      var attr = "data-" + prop.toLowerCase();
      if ($carousel.parent().attr(attr)) {
        var value = $carousel.parent().attr(attr);
        options[prop] = value;
      }
    }
  }
  return options;
}

/**
 * Fires after each change.
 * Align the active slide to the left edge of the carousel
 * to prevent items from getting cut off.
 */
function carouselAfterChange(event, slick, currentSlideIndex) {
  // align left edge in case of drift.
  const $current = jQuery(slick.$slides[currentSlideIndex]);
  const $track = jQuery(slick.$slideTrack);
  let left = $current[0].offsetLeft * -1;
  let style = $track.attr("style");
  style = style.replace(/translate3d\([^,]+,/, "translate3d(" + left + "px,");
  setTimeout(function () {
    $track.attr("style", style);
  }, 10);
}

/**
 * Unset the carousel and re-init
 */
function carouselReset($carousel) {
  $carousel.slick("unslick");
  var options = carouselOptions($carousel);
  $carousel.slick(options);
}

// Initialize carousels.
$carousels.each(function () {
  var $carousel = jQuery(this);
  $carousel.on("afterChange", carouselAfterChange);
  var options = carouselOptions($carousel);
  $carousel.slick(options);
});

// Reset and re-Config carousels on window resize
let winResizeBounce = null;
jQuery(window).on("resize", function () {
  clearTimeout(winResizeBounce);
  winResizeBounce = setTimeout(function () {
    $carousels.each(function () {
      var $carousel = jQuery(this);
      carouselReset($carousel);
    });
  }, 50);
});

              
            
!
999px

Console