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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<script>
const slides = [
{
id: 36,
pauseDelay: 4000,
background: {
type: 'image',
value: 'TODO: get image from this property',
attributes: {
class: 'bg-purple-300 bg-cover',
style: 'background-image: url(https://images.unsplash.com/photo-1427751840561-9852520f8ce8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=3455&q=80)'
}
},
layers: [
{
id: 111,
type: 'text',
delay: 'off',
content: "<h1 class='font-black text-4xl text-purple-900 uppercase'>Lorem ipsum dolor</h1>",
attributes: {
class: '',
style: {},
},
},
{
id: 222,
type: 'text',
delay: 1000,
content: "<div class='max-w-3xl text-lg'>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>",
attributes: {
class: '',
style: {},
},
},
{
id: 333,
type: 'text',
delay: 1500,
content: "<a href='#' class='bg-purple-900 text-white px-6 py-2 mt-6 inline-block rounded text-lg'>Learn more ></a>",
attributes: {
class: '',
style: {},
},
},
]
},
{
id: 34,
pauseDelay: 4000,
background: {
type: 'image',
value: '',
attributes: {
class: 'bg-purple-300 bg-cover bg-center',
style: 'background-image: url(https://images.unsplash.com/photo-1552757909-2bc6e5e16ab9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=3805&q=80)'
}
},
layers: [
{
id: 666,
type: 'text',
delay: 'off',
content: '<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="29.-Diamond" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"><polygon id="Layer-1" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="11.4331321 4 87.7530578 4 97.1577608 32.5148515 2.10825583 32.5148515"></polygon><polygon id="Layer-2" stroke="#55AB94" stroke-width="4" fill="#55AB94" stroke-linejoin="round" points="2.10825583 33.5148515 97.1577608 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-3" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="2.10825583 33.5148515 30.796684 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-4" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="97.1577608 33.5148515 68.4693326 33.5148515 49.6330083 95.2970297"></polygon><path d="M12.6137908,4 C12.0611833,4 11.8956471,4.34297998 12.2433835,4.76525116 L30.553491,27 L48.8815452,4.76525116 C49.2299235,4.34261461 49.7987118,4.34297998 50.1512881,4.76525116 L68.7162385,27 L86.8146093,4.77843033 C87.164754,4.34851513 87.0032497,4 86.4480179,4 L12.6137908,4 Z" id="Layer-5" stroke="#6FCCB3" stroke-width="4" fill="#6FCCB3"></path></g></svg>',
attributes: {
class: 'absolute',
style: {'top': '100px', 'left': '100px' },
},
},
{
id: 667,
type: 'text',
delay: 00,
content: '<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="29.-Diamond" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"><polygon id="Layer-1" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="11.4331321 4 87.7530578 4 97.1577608 32.5148515 2.10825583 32.5148515"></polygon><polygon id="Layer-2" stroke="#55AB94" stroke-width="4" fill="#55AB94" stroke-linejoin="round" points="2.10825583 33.5148515 97.1577608 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-3" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="2.10825583 33.5148515 30.796684 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-4" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="97.1577608 33.5148515 68.4693326 33.5148515 49.6330083 95.2970297"></polygon><path d="M12.6137908,4 C12.0611833,4 11.8956471,4.34297998 12.2433835,4.76525116 L30.553491,27 L48.8815452,4.76525116 C49.2299235,4.34261461 49.7987118,4.34297998 50.1512881,4.76525116 L68.7162385,27 L86.8146093,4.77843033 C87.164754,4.34851513 87.0032497,4 86.4480179,4 L12.6137908,4 Z" id="Layer-5" stroke="#6FCCB3" stroke-width="4" fill="#6FCCB3"></path></g></svg>',
attributes: {
class: 'absolute',
style: {'top': '200px', 'left': '300px' },
},
},
{
id: 668,
type: 'text',
delay: 800,
content: '<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="29.-Diamond" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"><polygon id="Layer-1" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="11.4331321 4 87.7530578 4 97.1577608 32.5148515 2.10825583 32.5148515"></polygon><polygon id="Layer-2" stroke="#55AB94" stroke-width="4" fill="#55AB94" stroke-linejoin="round" points="2.10825583 33.5148515 97.1577608 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-3" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="2.10825583 33.5148515 30.796684 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-4" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="97.1577608 33.5148515 68.4693326 33.5148515 49.6330083 95.2970297"></polygon><path d="M12.6137908,4 C12.0611833,4 11.8956471,4.34297998 12.2433835,4.76525116 L30.553491,27 L48.8815452,4.76525116 C49.2299235,4.34261461 49.7987118,4.34297998 50.1512881,4.76525116 L68.7162385,27 L86.8146093,4.77843033 C87.164754,4.34851513 87.0032497,4 86.4480179,4 L12.6137908,4 Z" id="Layer-5" stroke="#6FCCB3" stroke-width="4" fill="#6FCCB3"></path></g></svg>',
attributes: {
class: 'absolute',
style: {'top': '300px', 'left': '700px' },
},
},
{
id: 669,
type: 'text',
delay: 1000,
content: '<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="29.-Diamond" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"><polygon id="Layer-1" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="11.4331321 4 87.7530578 4 97.1577608 32.5148515 2.10825583 32.5148515"></polygon><polygon id="Layer-2" stroke="#55AB94" stroke-width="4" fill="#55AB94" stroke-linejoin="round" points="2.10825583 33.5148515 97.1577608 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-3" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="2.10825583 33.5148515 30.796684 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-4" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="97.1577608 33.5148515 68.4693326 33.5148515 49.6330083 95.2970297"></polygon><path d="M12.6137908,4 C12.0611833,4 11.8956471,4.34297998 12.2433835,4.76525116 L30.553491,27 L48.8815452,4.76525116 C49.2299235,4.34261461 49.7987118,4.34297998 50.1512881,4.76525116 L68.7162385,27 L86.8146093,4.77843033 C87.164754,4.34851513 87.0032497,4 86.4480179,4 L12.6137908,4 Z" id="Layer-5" stroke="#6FCCB3" stroke-width="4" fill="#6FCCB3"></path></g></svg>',
attributes: {
class: 'absolute',
style: {'top': '100px', 'left': '500px' },
},
},
{
id: 670,
type: 'text',
delay: 1200,
content: '<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="29.-Diamond" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"><polygon id="Layer-1" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="11.4331321 4 87.7530578 4 97.1577608 32.5148515 2.10825583 32.5148515"></polygon><polygon id="Layer-2" stroke="#55AB94" stroke-width="4" fill="#55AB94" stroke-linejoin="round" points="2.10825583 33.5148515 97.1577608 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-3" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="2.10825583 33.5148515 30.796684 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-4" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="97.1577608 33.5148515 68.4693326 33.5148515 49.6330083 95.2970297"></polygon><path d="M12.6137908,4 C12.0611833,4 11.8956471,4.34297998 12.2433835,4.76525116 L30.553491,27 L48.8815452,4.76525116 C49.2299235,4.34261461 49.7987118,4.34297998 50.1512881,4.76525116 L68.7162385,27 L86.8146093,4.77843033 C87.164754,4.34851513 87.0032497,4 86.4480179,4 L12.6137908,4 Z" id="Layer-5" stroke="#6FCCB3" stroke-width="4" fill="#6FCCB3"></path></g></svg>',
attributes: {
class: 'absolute',
style: {'top': '200px', 'right': '200px' },
},
},
{
id: 671,
type: 'text',
delay: 1400,
content: '<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="29.-Diamond" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"><polygon id="Layer-1" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="11.4331321 4 87.7530578 4 97.1577608 32.5148515 2.10825583 32.5148515"></polygon><polygon id="Layer-2" stroke="#55AB94" stroke-width="4" fill="#55AB94" stroke-linejoin="round" points="2.10825583 33.5148515 97.1577608 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-3" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="2.10825583 33.5148515 30.796684 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-4" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="97.1577608 33.5148515 68.4693326 33.5148515 49.6330083 95.2970297"></polygon><path d="M12.6137908,4 C12.0611833,4 11.8956471,4.34297998 12.2433835,4.76525116 L30.553491,27 L48.8815452,4.76525116 C49.2299235,4.34261461 49.7987118,4.34297998 50.1512881,4.76525116 L68.7162385,27 L86.8146093,4.77843033 C87.164754,4.34851513 87.0032497,4 86.4480179,4 L12.6137908,4 Z" id="Layer-5" stroke="#6FCCB3" stroke-width="4" fill="#6FCCB3"></path></g></svg>',
attributes: {
class: 'absolute',
style: {'top': '350px', 'right': '300px' },
},
},
{
id: 672,
type: 'text',
delay: 1600,
content: '<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="29.-Diamond" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round"><polygon id="Layer-1" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="11.4331321 4 87.7530578 4 97.1577608 32.5148515 2.10825583 32.5148515"></polygon><polygon id="Layer-2" stroke="#55AB94" stroke-width="4" fill="#55AB94" stroke-linejoin="round" points="2.10825583 33.5148515 97.1577608 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-3" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="2.10825583 33.5148515 30.796684 33.5148515 49.6330083 95.2970297"></polygon><polygon id="Layer-4" stroke="#63BBA4" stroke-width="4" fill="#63BBA4" stroke-linejoin="round" points="97.1577608 33.5148515 68.4693326 33.5148515 49.6330083 95.2970297"></polygon><path d="M12.6137908,4 C12.0611833,4 11.8956471,4.34297998 12.2433835,4.76525116 L30.553491,27 L48.8815452,4.76525116 C49.2299235,4.34261461 49.7987118,4.34297998 50.1512881,4.76525116 L68.7162385,27 L86.8146093,4.77843033 C87.164754,4.34851513 87.0032497,4 86.4480179,4 L12.6137908,4 Z" id="Layer-5" stroke="#6FCCB3" stroke-width="4" fill="#6FCCB3"></path></g></svg>',
attributes: {
class: 'absolute',
style: {'top': '100px', 'right': '500px' },
},
},
]
},
{
id: 35,
pauseDelay: 4000,
background: {
// https://codepen.io/dudleystorey/pen/knqyK
type: 'video-selfhosted',
poster: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/polina.jpg',
video: {
webm: 'http://thenewcode.com/assets/videos/polina.webm',
mp4: 'http://thenewcode.com/assets/videos/polina.mp4',
},
attributes: {
class: '',
style: ''
}
},
layers: [
{
id: 9666,
type: 'text',
delay: 1000,
content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.",
attributes: {
class: 'text-white text-6xl',
style: {},
},
},
]
},
];
const config = {
dots: true,
autoplay: false,
transition: 'slide',
}
</script>
<section
aria-labelledby="carouselheading"
class="flex flex-col items-center justify-center relative"
style="height:650px"
@click="state.looping = false;state.userInteracted = true"
@click.away="state.looping = state.userInteracted && settings.autoplayAfterInteraction"
@mouseenter="state.looping = !settings.stopOnHover"
@mouseleave="state.looping = settings.autoplayAfterMouseLeave && !state.userInteracted"
@focusin="state.looping = false"
@focusout="state.looping = settings.autoplayAfterFocusOut && !state.userInteracted"
x-data="slideshow_1234(slides, config)"
x-init="setup();"
>
<div id="carouselheading" class="sr-only">Slideshow 1234</div>
<ul class="relative w-full h-full overflow-hidden">
<template x-for="(slide, index) in slides" :key="slide.id">
<li
x-show="state.order[state.currentSlide] == slide.id"
x-bind:aria-hidden="(state.order[state.currentSlide] != slide.id).toString()"
:x-ref="slide.id"
style="opacity:0"
class="w-full h-full"
x-bind:class="slideClass(slide)"
:x-transition:enter="transitions('enter')"
:x-transition:enter-start="transitions('enter-start')"
:x-transition:enter-end="transitions('enter-end')"
:x-transition:leave="transitions('leave')"
:x-transition:leave-start="transitions('leave-start')"
:x-transition:leave-end="transitions('leave-end')"
>
<template x-if="'video-selfhosted' === slide.background.type">
<video
:x-ref="slide.id + '-video-background'"
:poster="slide.background.poster"
style="z-index:-100;object-fit:cover"
class="w-full h-full absolute"
playsinline autoplay muted loop>
<source :src="slide.background.video.webm" type="video/webm">
<source :src="slide.background.video.mp4" type="video/mp4">
</video>
</template>
<div class="p-16">
<template x-for="layer in slide.layers" :key="layer.id">
<div
x-bind:class="layer.attributes.class"
x-show="visibleLayers(layer)"
:x-ref="layer.id"
x-transition:enter="transition-all duration-300"
x-transition:enter-start="transform opacity-0 scale-95"
x-transition:enter-end="transform opacity-100 scale-100"
x-transition:leave="transition-all duration-300"
x-transition:leave-start="transform opacity-100 scale-100"
x-transition:leave-end="transform opacity-0 scale-95"
x-html="layer.content">
</div>
</template>
</div>
</li>
</template>
</ul>
<div
aria-live="polite"
aria-atomic="true"
class="sr-only"
x-text="'Slide ' + (state.currentSlide + 1) + ' of ' + slides.length"
></div>
<div>
<ul>
<li>
<button
@click="setCurrent(previousSlide)"
class="z-50 absolute h-12 left-0 -mt-6 ml-4 text-4xl w-10 text-white"
style="top:50%">
<svg role="button" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="svg-inline--fa fa-angle-double-left fa-w-14 fa-3x"><path fill="currentColor" d="M223.7 239l136-136c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9L319.9 256l96.4 96.4c9.4 9.4 9.4 24.6 0 33.9L393.7 409c-9.4 9.4-24.6 9.4-33.9 0l-136-136c-9.5-9.4-9.5-24.6-.1-34zm-192 34l136 136c9.4 9.4 24.6 9.4 33.9 0l22.6-22.6c9.4-9.4 9.4-24.6 0-33.9L127.9 256l96.4-96.4c9.4-9.4 9.4-24.6 0-33.9L201.7 103c-9.4-9.4-24.6-9.4-33.9 0l-136 136c-9.5 9.4-9.5 24.6-.1 34z" class=""></path></svg>
</button>
</li>
<li>
<button
@click="setCurrent(nextSlide)"
class="z-50 absolute h-12 right-0 -mt-6 mr-4 text-4xl w-10 text-white"
style="top:50%">
<svg role="button" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="svg-inline--fa fa-angle-double-right fa-w-14 fa-3x"><path fill="currentColor" d="M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34zm192-34l-136-136c-9.4-9.4-24.6-9.4-33.9 0l-22.6 22.6c-9.4 9.4-9.4 24.6 0 33.9l96.4 96.4-96.4 96.4c-9.4 9.4-9.4 24.6 0 33.9l22.6 22.6c9.4 9.4 24.6 9.4 33.9 0l136-136c9.4-9.2 9.4-24.4 0-33.8z" class=""></path></svg>
</button>
</li>
</ul>
</div>
<div class="bg-purple-500 w-full flex items-center justify-center h-10">
<template x-if="settings.dots || settings.playButton">
<ul class="flex space-x-2">
<template x-if="settings.playButton">
<li class="leading-none">
<button
@click="state.looping = !state.looping;state.autoplayStarted || autoPlay()"
x-html="state.looping ? 'pause' : 'start'">
</button>
</li>
</template>
<template x-if="settings.dots" x-for="(slide, index) in Array.from({ length: slides.length })" :key="index">
<li class="leading-none">
<button
class="w-3 h-3 rounded-full text-white inline-flex"
:class="{
'bg-purple-900' : state.currentSlide == index,
'bg-white' : state.currentSlide != index
}"
style="text-indent:-9999px"
@click="setCurrent(index)"
x-text="index + 1"
></button>
</li>
</template>
</ul>
</template>
</div>
</section>
// TODO: Use a separate function or have a hook (or something) that is used for fade, slide, etc. Make it more modular
// Alpine 2.3.5
function slideshow_1234(slides, config) {
return {
state: {
currentSlide: 0,
moving: false,
autoplayStarted: false,
looping: false,
order: [],
nextSlideDirection: '',
userInteracted: false,
visibleLayers: [],
alwaysVisibleLayers: [],
loaded: false,
},
settings: Object.assign({
transition: 'fade',
direction: 'right-left', // TODO: impliment this as an RTL option
duration: 1000,
autoplay: true,
autoplayStartDelay: false,
autoplayAfterInteraction: true,
stopOnHover: true,
autoplayAfterMouseLeave: false,
autoplayAfterFocusOut: true,
dots: true,
playButton: false, // TODO: this needs a lot of work!
}, config),
slides: slides,
setup() {
// setup initial order. Important because the order changes depending on what's next
this.state.order = this.slides.map(slide => slide.id)
//Set the slide order so that the current slide is first
const newSlideOrder = this.slides.filter(slide => this.current.id != slide.id);
newSlideOrder.unshift(this.current);
this.slides = newSlideOrder;
// Add style attributes from slide object
// TODO: some of this is duplicated in the set current function
for (const ref of this.state.order) {
// TODO: consider moving this to each cycle instead of setup
this.$nextTick(() => {
const slide = this.slides.find(slide => slide.id === ref)
this.$refs[ref].style = slide.background.attributes.style;
// Update styles for layers
for (const layer of slide.layers) {
for (const [style, value] of Object.entries(layer.attributes.style)) {
this.$refs[layer.id] && this.$refs[layer.id].style.setProperty(style, value, 'important')
}
}
});
}
// Start the autoplay and start checking for layers
this.settings.autoplay && this.autoPlay();
this.showLayers();
// Slideshow is loaded // TODO: fire an event?
setTimeout(() => {
this.state.loaded = true;
}, this.settings.duration);
},
// TODO: These two will get the next slide based on when a user clicked. Consider renaming to make that obvious
get previousSlide() {
// Return the previous slide TODO: Add an option to continuously move to last slide
return (this.state.currentSlide - 1) > -1 ? this.state.currentSlide - 1 : this.state.currentSlide;
},
get nextSlide() {
// Return the next slide TODO: Add an option to continuously move to last slide
return (this.state.currentSlide + 1) < this.slides.length ? this.state.currentSlide + 1 : this.state.currentSlide;
},
get current() {
// This grabs the current slide (and it's details) based on the user provided order
return this.slides.find(slide => slide.id == this.state.order[this.state.currentSlide]);
},
setCurrent(nextSlide) {
console.log(nextSlide)
// If we don't need to move, or we aren't ready to move, dont move
if (nextSlide == this.state.currentSlide) return;
if (this.state.moving) return;
// If the layers haven't finished loading, don't move
if (JSON.stringify(this.allLayers) !== JSON.stringify(this.state.visibleLayers)) return;
// Now we're moving but not transitioning just yet
this.state.moving = true;
// Reset the layers that are shown on the current "previous" slide (Maybe move this to the end...)
// this.state.visibleLayers.length = 0;
// Give it time to finish the transition before loading new layers
// Find info about the next slide
const next = this.slides.find(slide => slide.id == this.state.order[nextSlide]);
console.log(next)
// If it's a video then set the timer to 0
if ('video-selfhosted' === next.background.type) {
this.$refs[next.id + '-video-background'].currentTime = 0;
}
// Reorder the slides for a smoother transition (this is why the initial order is cached)
const newSlideOrder = this.slides.filter(slide => {
return ![this.current.id, this.state.order[nextSlide]].includes(slide.id);
});
// The two ipmortant slides are the current "previous" and next
const activeSlides = [this.current, next];
// Determines if we need to slide left to right or right to left
this.state.nextSlideDirection = nextSlide > this.state.currentSlide ? 'right-to-left' : 'left-to-right';
newSlideOrder.unshift(...(this.state.nextSlideDirection == 'right-to-left' ? activeSlides : activeSlides.reverse()));
// This will set the new live order and then instruct trnaistion to the next slide
this.slides = newSlideOrder;
this.state.currentSlide = nextSlide;
// Give some breathing room before the next slide is allowed to transition
setTimeout(() => {
this.state.visibleLayers.length = 0;
this.showLayers();
this.state.moving = false;
}, this.settings.duration + 100)
// For debugging
console.log(this);
},
slideClass(slide) {
// TODO: hard coded for fade
if (slide.id === this.current.id) {
return slide.background.attributes.class + ' absolute top-0 z-20';
}
return slide.background.attributes.class + ' absolute top-0 z-10';
},
get allLayers() {
return this.current.layers.map(layer => layer.id);
},
transitions(state) {
if (!this.state.loaded) return '';
return 'fade' === this.settings.transition ? this.fadeTransition(state) : this.slideTransition(state);
},
fadeTransition(state) {
if (!this.state.loaded) return '';
// TODO: these should eventually be defined by the slide config
const rightToLeft = this.state.nextSlideDirection === 'right-to-left';
switch (state) {
case 'enter':
return `z-20 transition duration-${this.settings.duration}`;
break;
case 'enter-start':
return 'transform opacity-0';
break;
case 'enter-end':
return 'transform opacity-100';
break;
case 'leave':
return `z-10 absolute top-0 transition duration-${this.settings.duration}`;
break;
case 'leave-start':
return 'transform opacity-100';
break;
case 'leave-end':
return 'transform opacity-0';
break;
}
},
slideTransition(state) {
// TODO: these should eventually be defined by the slide config
const rightToLeft = this.state.nextSlideDirection === 'right-to-left';
switch (state) {
case 'enter':
return `transition duration-${this.settings.duration}`;
break;
case 'enter-start':
return rightToLeft ? 'transition transform translate-x-full' : 'transition transform -translate-x-full';
break;
case 'enter-end':
return 'transition transform translate-x-0';
break;
case 'leave':
return `absolute top-0 transition duration-${this.settings.duration}`;
break;
case 'leave-start':
return 'transition transform translate-x-0';
break;
case 'leave-end':
return rightToLeft ? 'transition transform -translate-x-full' : 'transition transform translate-x-full';
break;
}
},
autoPlay() {
// Basic autoplay, you can pause this using stste.looping
// TODO: add RTL or reverse direction
this.state.autoplayStarted = true;
this.loop = () => {
const next = (this.state.currentSlide === (this.slides.length - 1))
? 0 : this.state.currentSlide + 1;
this.setCurrent(this.state.looping ? next : this.state.currentSlide);
setTimeout(() => { requestAnimationFrame(this.loop) }, this.current.pauseDelay);
}
setTimeout(() => {
this.state.looping = true;
requestAnimationFrame(this.loop);
}, this.settings.autoplayStartDelay ? this.settings.autoplayStartDelay : this.current.pauseDelay);
},
visibleLayers(layer) {
if ('off' === layer.delay) return true
return this.state.visibleLayers.includes(layer.id);
},
showLayers() {
if (!this.current.layers.length) return;
for (layer of this.current.layers) {
const id = layer.id;
const attributes = layer.attributes;
this.$nextTick(() => {
// In case any layer styles have changed
for (const [style, value] of Object.entries(attributes.style)) {
this.$refs[id] && this.$refs[id].style.setProperty(style, value, 'important')
}
});
setTimeout(() => {
this.state.visibleLayers.push(id);
}, layer.delay);
}
}
}
}
Also see: Tab Triggers