HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!-- Section -->
<div class="section section--transparent">
<!-- Section inner -->
<div class="section__inner">
<p>Phasellus dapibus ligula tempor arcu sollicitudin, id aliquet ante dictum. Suspendisse in arcu a orci facilisis pretium. Ut pretium elit non mollis vehicula. Donec mauris libero, dapibus eget imperdiet quis, auctor vestibulum lorem. In posuere viverra luctus. Morbi sed sapien ipsum. Fusce hendrerit a arcu vel aliquet. Cras ac nibh lorem. Pellentesque vehicula tristique tellus facilisis condimentum.</p>
</div>
<!-- END Section inner -->
</div>
<!-- END Section -->
<!-- Section -->
<div class="section">
<!-- Section inner -->
<div class="section__inner">
<!-- Carousel block -->
<div id="js-carousel" class="carousel">
<!-- Carousel items container -->
<ul class="carousel__items">
<!-- Carousel item -->
<li class="carousel__item is-active">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum dolor sit</strong></p>
<p><small>Nunc sed rhoncus ante</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item is-active">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum dolor sit</strong></p>
<p><small>Nunc sed rhoncus ante</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item is-active">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum dolor sit</strong></p>
<p><small>Nunc sed rhoncus ante</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item is-active">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum dolor sit</strong></p>
<p><small>Nunc sed rhoncus ante</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item is-active">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum dolor sit</strong></p>
<p><small>Nunc sed rhoncus ante</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
<!-- Carousel item -->
<li class="carousel__item">
<article class="card">
<div class="card__icon">
<span class="icon icon--circle"></span>
</div>
<div class="card__details">
<p><strong>Lorem ipsum</strong></p>
<p><small>Dolor sit amet</small></p>
</div>
</article>
</li>
<!-- END Carousel item -->
</ul>
<!-- END Carousel items container -->
<!-- Carousel nav items container -->
<div class="carousel__nav">
<!-- Carousel nav item -->
<button class="carousel__nav-item" role="previous" disabled>
<span class="icon icon--chevron-left">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="currentColor" d="M8.053 8.355c.193-.195.193-.517 0-.711l-3.26-3.289c-.193-.195-.192-.514.002-.709l1.371-1.371c.194-.194.512-.193.706.001l5.335 5.369c.195.195.195.515 0 .708l-5.335 5.37c-.194.192-.512.193-.706.002l-1.371-1.371c-.194-.195-.195-.514-.002-.709l3.26-3.29z"/></svg>
</span>
</button>
<!-- END Carousel nav item -->
<!-- Carousel nav item -->
<button class="carousel__nav-item" role="next">
<span class="icon icon--chevron-right">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="currentColor" d="M8.053 8.355c.193-.195.193-.517 0-.711l-3.26-3.289c-.193-.195-.192-.514.002-.709l1.371-1.371c.194-.194.512-.193.706.001l5.335 5.369c.195.195.195.515 0 .708l-5.335 5.37c-.194.192-.512.193-.706.002l-1.371-1.371c-.194-.195-.195-.514-.002-.709l3.26-3.29z"/></svg>
</span>
</button>
<!-- END Carousel nav item -->
</div>
<!-- END Carousel nav items container -->
</div>
<!-- END Carousel block -->
</div>
<!-- END Section inner -->
</div>
<!-- END Section -->
<!-- Section -->
<div class="section section--transparent">
<!-- Section inner -->
<div class="section__inner">
<p>Phasellus dapibus ligula tempor arcu sollicitudin, id aliquet ante dictum. Suspendisse in arcu a orci facilisis pretium. Ut pretium elit non mollis vehicula. Donec mauris libero, dapibus eget imperdiet quis, auctor vestibulum lorem. In posuere viverra luctus. Morbi sed sapien ipsum. Fusce hendrerit a arcu vel aliquet. Cras ac nibh lorem. Pellentesque vehicula tristique tellus facilisis condimentum.</p>
</div>
<!-- END Section inner -->
</div>
<!-- END Section -->
/*---------------------------------------*\
Settings
\*---------------------------------------*/
$base-size: 8px;
$debug-outline: false;
/*---------------------------------------*\
Global styles
\*---------------------------------------*/
html,
body {
display: flex;
flex-wrap: wrap;
align-content: center;
justify-content: center;
width: 100%;
height: 100%;
margin: 0;
font-family: Arial, sans-serif;
line-height: 1.44;
background-color: #eee;
overflow-x: hidden;
}
*,
*::after,
*::before {
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
}
strong {
font-weight: 700;
}
small {
font-size: 0.875em;
}
/*---------------------------------------*\
Section
\*---------------------------------------*/
.section {
width: 100%;
padding-right: $base-size * 6;
padding-left: $base-size * 6;
background-color: #fff;
+ .section {
margin-top: $base-size * 6;
}
}
.section--transparent {
background-color: transparent;
}
.section__inner {
max-width: 150 * $base-size;
margin-right: auto;
margin-left: auto;
}
/*---------------------------------------*\
Carousel
\*---------------------------------------*/
.carousel {
position: relative;
}
.carousel__items {
position: relative;
transition: transform 0.3s ease-in-out;
&::after {
content: "";
clear: both;
display: table;
}
}
.carousel__item {
position: relative;
float: left;
opacity: 0.25;
cursor: pointer;
user-select: none;
transition: opacity 0.3s linear;
&:focus {
opacity: 0.5;
}
&:hover {
opacity: 0.75;
}
&.is-active {
opacity: 1;
&::before {
transform: scale(1);
}
}
&::before {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
background-color: currentcolor;
transform: scale(1, 0);
transform-origin: bottom left;
transition: transform 0.8s cubic-bezier(0, 1, 0, 1);
}
+ .carousel__item {
position: absolute;
top: 0;
left: 0;
}
}
.carousel__nav {
position: absolute;
top: 0;
left: 50%;
width: 100vw;
height: 100%;
margin-left: -50vw;
pointer-events: none;
}
.carousel__nav-item {
z-index: 1;
position: absolute;
top: 0;
width: $base-size * 6;
height: 100%;
font: inherit;
border: 0;
pointer-events: initial;
transition: transform 0.3s ease-in-out;
// States
&:hover,
&:active {
color: inherit;
}
// Pseudo-elements
&::after,
&::before {
content: "";
position: absolute;
}
&::after {
z-index: -1;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #fff;
}
&::before {
z-index: -2;
top: $base-size * 2;
bottom: $base-size * 2;
width: 1px;
background-color: currentColor;
box-shadow: 0 0 $base-size rgba(#000, 0.5);
opacity: 1;
transition: opacity 0.3s linear, transform 0.3s ease-in-out;
}
&[role="previous"] {
left: 0;
&::before {
right: 0;
}
}
&[role="next"] {
right: 0;
&::before {
left: 0;
}
}
&[disabled] {
&::before {
opacity: 0;
}
&[role="previous"] {
transform: translate(-100%, 0);
&::before {
transform: translate($base-size * -1, 0);
}
}
&[role="next"] {
transform: translate(100%, 0);
&::before {
transform: translate($base-size, 0);
}
}
}
span {
z-index: 1;
position: relative;
}
}
/*---------------------------------------*\
Car
\*---------------------------------------*/
.card {
padding: $base-size * 3;
&::after {
content: "";
clear: both;
display: table;
}
}
.card__icon,
.card__details {
float: left;
}
.card__details {
margin-left: $base-size * 2;
}
/*---------------------------------------*\
Icon
\*---------------------------------------*/
.icon {
display: inline-block;
vertical-align: middle;
}
.icon--circle {
width: $base-size * 6;
height: $base-size * 6;
border: $base-size solid;
border-radius: $base-size * 4;
}
.icon--chevron-left,
.icon--chevron-right {
width: $base-size * 2;
height: $base-size * 2;
}
.icon--chevron-left {
transform: scale(-1);
}
/*---------------------------------------*\
Debug helper
Add a coloured outline to every
elements in the document
\*---------------------------------------*/
@if $debug-outline == true {
*:after,
*:before {
outline: 1px solid rgba(#f00, 0.5);
}
body { outline: 1px solid rgba(#2980b9, 0.5) !important; }
article { outline: 1px solid rgba(#3498db, 0.5) !important; }
nav { outline: 1px solid rgba(#0088c3, 0.5) !important; }
aside { outline: 1px solid rgba(#33a0ce, 0.5) !important; }
section { outline: 1px solid rgba(#66b8da, 0.5) !important; }
header { outline: 1px solid rgba(#99cfe7, 0.5) !important; }
footer { outline: 1px solid rgba(#cce7f3, 0.5) !important; }
h1 { outline: 1px solid rgba(#162544, 0.5) !important; }
h2 { outline: 1px solid rgba(#314e6e, 0.5) !important; }
h3 { outline: 1px solid rgba(#3e5e85, 0.5) !important; }
h4 { outline: 1px solid rgba(#449baf, 0.5) !important; }
h5 { outline: 1px solid rgba(#c7d1cb, 0.5) !important; }
h6 { outline: 1px solid rgba(#4371d0, 0.5) !important; }
main { outline: 1px solid rgba(#2f4f90, 0.5) !important; }
address { outline: 1px solid rgba(#1a2c51, 0.5) !important; }
div { outline: 1px solid rgba(#036cdb, 0.5) !important; }
p { outline: 1px solid rgba(#ac050b, 0.5) !important; }
hr { outline: 1px solid rgba(#ff063f, 0.5) !important; }
pre { outline: 1px solid rgba(#850440, 0.5) !important; }
blockquote { outline: 1px solid rgba(#f1b8e7, 0.5) !important; }
ol { outline: 1px solid rgba(#ff050c, 0.5) !important; }
ul { outline: 1px solid rgba(#d90416, 0.5) !important; }
li { outline: 1px solid rgba(#d90416, 0.5) !important; }
dl { outline: 1px solid rgba(#fd3427, 0.5) !important; }
dt { outline: 1px solid rgba(#ff0043, 0.5) !important; }
dd { outline: 1px solid rgba(#e80174, 0.5) !important; }
figure { outline: 1px solid rgba(#f0b, 0.5) !important; }
figcaption { outline: 1px solid rgba(#bf0032, 0.5) !important; }
table { outline: 1px solid rgba(#0c9, 0.5) !important; }
caption { outline: 1px solid rgba(#37ffc4, 0.5) !important; }
thead { outline: 1px solid rgba(#98daca, 0.5) !important; }
tbody { outline: 1px solid rgba(#64a7a0, 0.5) !important; }
tfoot { outline: 1px solid rgba(#22746b, 0.5) !important; }
tr { outline: 1px solid rgba(#86c0b2, 0.5) !important; }
th { outline: 1px solid rgba(#a1e7d6, 0.5) !important; }
td { outline: 1px solid rgba(#3f5a54, 0.5) !important; }
col { outline: 1px solid rgba(#6c9a8f, 0.5) !important; }
colgroup { outline: 1px solid rgba(#6c9a9d, 0.5) !important; }
button { outline: 1px solid rgba(#da8301, 0.5) !important; }
datalist { outline: 1px solid rgba(#c06000, 0.5) !important; }
fieldset { outline: 1px solid rgba(#d95100, 0.5) !important; }
form { outline: 1px solid rgba(#d23600, 0.5) !important; }
input { outline: 1px solid rgba(#fca600, 0.5) !important; }
keygen { outline: 1px solid rgba(#b31e00, 0.5) !important; }
label { outline: 1px solid rgba(#ee8900, 0.5) !important; }
legend { outline: 1px solid rgba(#de6d00, 0.5) !important; }
meter { outline: 1px solid rgba(#e8630c, 0.5) !important; }
optgroup { outline: 1px solid rgba(#b33600, 0.5) !important; }
option { outline: 1px solid rgba(#ff8a00, 0.5) !important; }
output { outline: 1px solid rgba(#ff9619, 0.5) !important; }
progress { outline: 1px solid rgba(#e57c00, 0.5) !important; }
select { outline: 1px solid rgba(#e26e0f, 0.5) !important; }
textarea { outline: 1px solid rgba(#cc5400, 0.5) !important; }
details { outline: 1px solid rgba(#33848f, 0.5) !important; }
summary { outline: 1px solid rgba(#60a1a6, 0.5) !important; }
command { outline: 1px solid rgba(#438da1, 0.5) !important; }
menu { outline: 1px solid rgba(#449da6, 0.5) !important; }
del { outline: 1px solid rgba(#bf0000, 0.5) !important; }
ins { outline: 1px solid rgba(#400000, 0.5) !important; }
img { outline: 1px solid rgba(#22746b, 0.5) !important; }
iframe { outline: 1px solid rgba(#64a7a0, 0.5) !important; }
embed { outline: 1px solid rgba(#98daca, 0.5) !important; }
object { outline: 1px solid rgba(#0c9, 0.5) !important; }
param { outline: 1px solid rgba(#37ffc4, 0.5) !important; }
video { outline: 1px solid rgba(#6ee866, 0.5) !important; }
audio { outline: 1px solid rgba(#027353, 0.5) !important; }
source { outline: 1px solid rgba(#012426, 0.5) !important; }
canvas { outline: 1px solid rgba(#a2f570, 0.5) !important; }
track { outline: 1px solid rgba(#59a600, 0.5) !important; }
map { outline: 1px solid rgba(#7be500, 0.5) !important; }
area { outline: 1px solid rgba(#305900, 0.5) !important; }
a { outline: 1px solid rgba(#ff62ab, 0.5) !important; }
em { outline: 1px solid rgba(#800b41, 0.5) !important; }
strong { outline: 1px solid rgba(#ff1583, 0.5) !important; }
i { outline: 1px solid rgba(#803156, 0.5) !important; }
b { outline: 1px solid rgba(#cc1169, 0.5) !important; }
u { outline: 1px solid rgba(#ff0430, 0.5) !important; }
s { outline: 1px solid rgba(#f805e3, 0.5) !important; }
small { outline: 1px solid rgba(#d107b2, 0.5) !important; }
abbr { outline: 1px solid rgba(#4a0263, 0.5) !important; }
q { outline: 1px solid rgba(#240018, 0.5) !important; }
cite { outline: 1px solid rgba(#64003c, 0.5) !important; }
dfn { outline: 1px solid rgba(#b4005a, 0.5) !important; }
sub { outline: 1px solid rgba(#dba0c8, 0.5) !important; }
sup { outline: 1px solid rgba(#cc0256, 0.5) !important; }
time { outline: 1px solid rgba(#d6606d, 0.5) !important; }
code { outline: 1px solid rgba(#e04251, 0.5) !important; }
kbd { outline: 1px solid rgba(#5e001f, 0.5) !important; }
samp { outline: 1px solid rgba(#9c0033, 0.5) !important; }
var { outline: 1px solid rgba(#d90047, 0.5) !important; }
mark { outline: 1px solid rgba(#ff0053, 0.5) !important; }
bdi { outline: 1px solid rgba(#bf3668, 0.5) !important; }
bdo { outline: 1px solid rgba(#6f1400, 0.5) !important; }
ruby { outline: 1px solid rgba(#ff7b93, 0.5) !important; }
rt { outline: 1px solid rgba(#ff2f54, 0.5) !important; }
rp { outline: 1px solid rgba(#803e49, 0.5) !important; }
span { outline: 1px solid rgba(#cc2643, 0.5) !important; }
br { outline: 1px solid rgba(#db687d, 0.5) !important; }
wbr { outline: 1px solid rgba(#db175b, 0.5) !important; }
}
/** @type {Boolean} Whether to show log in the console or not */
var debug = true;
/** @type {Function} Log alias */
var log = debug
? window.console.log.bind(window, 'Carousel:')
: function() {};
/**
* Carousel class
*/
class Carousel {
/**
* Class constructor
*
* @return {Object} The created instance
*/
constructor() {
log('constructor');
// Get DOM elements
this.itemsElement = document.querySelector('#js-carousel .carousel__items');
this.itemElements = document.querySelectorAll('#js-carousel .carousel__item');
this.navItemElements = document.querySelectorAll('#js-carousel .carousel__nav-item');
// Carousel infromations
this.length = this.itemElements.length;
this.index = 0;
this.x = 0;
// Set up everything
this.bindEvents()
.setItemsPosition()
.updatePosition(this.index)
.updateItems(this.index)
.updateNav(this.index);
return this;
}
/**
* Bind navigation event on:
* - navigation elements
* - each item
*
* @return {Object} The current instance
*/
bindEvents() {
log('bindEvents');
const itemElementClickHandler = this.itemElementClickHandler.bind(this);
this.itemElements.forEach((itemElement, index) => {
itemElement.setAttribute('data-index', index);
itemElement.addEventListener('click', itemElementClickHandler);
});
const navItemElementClickHandler = this.navItemElementClickHandler.bind(this);
this.navItemElements.forEach((navItemElement) => {
navItemElement.addEventListener('click', navItemElementClickHandler);
});
return this;
}
/**
* Handler for the `click` event on each item of the carousel
*
* @return {void}
*/
itemElementClickHandler(e) {
log('itemElementClickHandler');
const index = e.currentTarget.getAttribute('data-index');
this.goto(parseInt(index, 10));
}
/**
* Handler for the `click` event on navigation buttons
*
* @return {void}
*/
navItemElementClickHandler(e) {
log('navItemElementClickHandler');
const direction = e.currentTarget.getAttribute('role');
this[direction]();
}
/**
* Go to the previous item
*
* @return {Object} The current instance
*/
previous() {
log('previous');
this.goto(this.index - 1);
return this;
}
/**
* Go to the next item
*
* @return {Object} The current instance
*/
next() {
log('next');
this.goto(this.index + 1);
return this;
}
/**
* Go to the given index if it is not lower than
* the minimum index (0) nor greater than the total
* index (length - 1)
*
* @return {Object} The current instance
*/
goto(index) {
log('goto', index);
// Always update nav
this.updateNav(index);
// Check if the target index is not
// too small or too big
if (index < 0 || index > this.length - 1) {
log('already at one end');
return;
}
// Update everything to the current index
this.updatePosition(index)
.updateItems(index);
this.index = index;
return this;
}
/**
* Set all items position to display
* all of them on the same line
*
* @return {Object} The current instance
*/
setItemsPosition() {
log('setItemsPosition');
let x = 0;
this.itemElements.forEach((itemElement, index) => {
itemElement.style.transform = `translate3d(${x}px, 0, 0)`;
x += itemElement.getBoundingClientRect().width;
});
return this;
}
/**
* Update the items container position to display
* the target index element in the first position
*
* @return {Object} The current instance
*/
updatePosition(index) {
log('updatePosition');
if (index === this.index) {
log('index has not changed, do not update position');
return this;
}
let itemsWidth = 0;
this.itemElements.forEach((itemElement, i) => {
if (i < index) {
itemsWidth += itemElement.getBoundingClientRect().width;
}
});
this.x = -itemsWidth;
this.itemsElement.style.transform = `translate3d(${this.x}px, 0, 0)`;
return this;
}
/**
* Update the target item's class
*
* @return {Object} The current instance
*/
updateItems(index) {
log('updateItems');
this.itemElements.forEach((itemElement) => {
itemElement.classList.remove('is-active');
});
this.itemElements.item(index).classList.add('is-active');
return this;
}
/**
* Update the nav buttons states given an index
*
* @return {Object} The current instance
*/
updateNav(index) {
log('updateNav');
const previous = this.navItemElements.item(0);
const next = this.navItemElements.item(1);
// Update previous button
if (index <= 0) {
previous.setAttribute('disabled', true);
} else {
previous.removeAttribute('disabled');
}
// Update next button
if (index >= this.length - 1) {
next.setAttribute('disabled', true);
} else {
next.removeAttribute('disabled');
}
return this;
}
}
// Init a carousel
const carousel = new Carousel();
Also see: Tab Triggers