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 URL's 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 it's URL and the proper URL extention.
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.
<article class="panels-wrapper">
<div class="panels">
<section class="panel">
<p>Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Nullam quis risus eget urna mollis ornare vel eu leo.</p>
</section>
<section class="panel">
<p>Maecenas sed diam eget risus varius blandit sit amet non magna. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.</p>
</section>
<section class="panel">
<p>Nullam quis risus eget urna mollis ornare vel eu leo. Integer posuere erat a ante venenatis dapibus posuere velit aliquet.</p>
</section>
</div>
<nav class="panels-nav">
<button class="panels-nav-btn panels-nav-prev">←</button>
<ul class="panels-nav-menu">
<li class="panels-nav-menu-item">•</li>
<li class="panels-nav-menu-item">•</li>
<li class="panels-nav-menu-item">•</li>
</ul>
<button class="panels-nav-btn panels-nav-next">→</button>
</nav>
</article>
// THE IMPORTANT BITS:
// ------------------
$panel-width: 400px;
// The wrapper has to have a width set, and it should have
// overflow:hidden set to hide everything but the current slide:
.panels-wrapper {
width: $panel-width;
overflow-x: hidden;
// overflow-x: visible; // leave visible for debugging
}
// The inner wrapper has display:flex to allow the items to sit side-by side
// and take up the same vertical space. No width is set - this is calculated in
// the JS. The transforamtion is also done in the JS. ANimations can be
// added here through.
.panels {
display: flex;
transition: all .275s ease-out;
}
// This needs the same width as the outer container, so that the panels
// fit exactly.
.panel {
// problems with padding/box-sizing unless I explicitly specify this:
flex-basis: $panel-width;
}
// FANCY STYLING
// -------------
$amethyst: #9b59b6; // light purple
$wisteria: #8e44ad; // purple
$clouds: #ecf0f1; // light grey
$silver: #bdc3c7; // grey
$westasphalt: #34495e; // lighter blue-grey
$midnightblue: #2c3e50; // blue-grey
$panel-bg: $clouds;
$panel-text-color: $westasphalt;
$nav-bg: $amethyst;
$nav-btn-color: $midnightblue;
body {
width: 100vw;
min-height: 100vh;
overflow-x: hidden;
display: flex;
align-items: center;
justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
}
.panels-wrapper {
background-color: $panel-bg;
color: $panel-text-color;
font-weight: 300;
}
.panel {
padding: 1rem;
}
.panels-nav {
background-color: $nav-bg;
display: flex;
justify-content: space-between;
align-items: center;
}
.panels-nav-btn {
background: transparent;
border: 0;
box-shadow: 0;
color: $nav-btn-color;
font-size: 2rem;
&:focus, &:active, &:hover {
color: darken($nav-btn-color, 20%);
}
}
.panels-nav-btn.disabled,
.panels-nav-menu-item.active {
pointer-events: none;
opacity: 0.25;
}
.panels-nav-menu {
display: flex;
}
.panels-nav-menu-item {
color: $nav-btn-color;
cursor: pointer;
margin: .25rem;
}
function SliderCore({
slideContainerSelector = '[data-panels-id]',
slideControlSelector = '[data-panel-control]'
}) {
const slides = document.querySelector
}
function nextable() {}
function previousable() {}
function conditionallyNextable() {}
function conditionallyPreviousable() {}
function jumptoable() {}
function slidable() {}
function swipeable() {}
function autoplayable() {}
// This is fairly primitive, but it works well enough.
// With this being a class, I can create any number of sliders on
// a page by using `new Slider(options)`.
// FIXME: this is not possible at the minute, because I'm selecting
// on a `document` basis; needs to be scoped to the outer selector.
class Slider {
// The constructor takes a selector for the element that contains the
// panels, and the next/prev nav buttons.
// The selectors are defined in an object, like:
// {
// panelsSelector: `.panels`,
// prevBtnSelector: `.panels-nav-prev`,
// nextBtnSelector: `.panels-nav-next`
// }
constructor({ panelsSelector, prevBtnSelector, nextBtnSelector, panelNavSelector = null }) {
// In the constructor, I take the selectors, and turn them into actual
// references to elements in the document.
this.panelsContainer = document.querySelector(panelsSelector);
// I don't need to search for the individual panels - I just grab the children
// of their container.
this.panels = this.panelsContainer.children;
// I could have simplified this further by just selecting
// the block with the buttons in and using `.children` again,
// but I want to be able to individually enable/disable them.
this.prevBtn = document.querySelector(prevBtnSelector);
this.nextBtn = document.querySelector(nextBtnSelector);
// I want a set of nav items so if necessary I can select
// a specific panel.
this.panelNav = document.querySelector(panelNavSelector);
this.panelNavItems = Array.from(this.panelNav.children);
// Instead of keeping references in the dom, I keep a record
// of whereabout the slider is at in a state object. This is
// similar to how React deals with things:
this.state = {
currentPanelIndex: 0,
firstPanelIndex: 0,
lastPanelIndex: this.panels.length - 1
}
}
// If I'm on the last panel, I can't logically go further right. And
// if I'm on the first panel, I can't go further left. I'll just disble
// the buttons if that's the case.
enableDisableButtons() {
if(this.state.currentPanelIndex === this.state.firstPanelIndex) { this.prevBtn.classList.add(`disabled`); } else { this.prevBtn.classList.remove(`disabled`); }
if(this.state.currentPanelIndex === this.state.lastPanelIndex) { this.nextBtn.classList.add(`disabled`); } else { this.nextBtn.classList.remove(`disabled`); }
}
// The next/previous handlers do three things:
// As long as it isn't the lest/first panel (when they do nothing), they:
// 1. change the `currentPanelIndex` in the state by +/- 1
// 2. use a CSS translate to slide the whole slide container horizontally
// 3. Enable or disable the next/prev button if necessary.
nextPanelHandler() {
if (this.state.currentPanelIndex < this.state.lastPanelIndex) {
this.panelNavItems[this.state.currentPanelIndex].classList.remove(`active`)
this.state.currentPanelIndex += 1;
this.panelNavItems[this.state.currentPanelIndex].classList.add(`active`)
this.slideHorizontal();
this.enableDisableButtons();
}
}
prevPanelHandler() {
if (this.state.currentPanelIndex > this.state.firstPanelIndex) {
this.panelNavItems[this.state.currentPanelIndex].classList.remove(`active`)
this.state.currentPanelIndex -= 1;
this.panelNavItems[this.state.currentPanelIndex].classList.add(`active`)
this.slideHorizontal();
this.enableDisableButtons();
}
}
gotoPanelHandler(e) {
console.log(`current index: ${this.state.currentPanelIndex}, target index: ${this.panelNavItems.indexOf(e.currentTarget)}`);
if (this.state.currentPanelIndex !== this.panelNavItems.indexOf(e.currentTarget)) {
this.panelNavItems[this.state.currentPanelIndex].classList.remove(`active`);
this.state.currentPanelIndex = this.panelNavItems.indexOf(e.currentTarget);
this.panelNavItems[this.state.currentPanelIndex].classList.add(`active`);
this.slideHorizontal();
this.enableDisableButtons();
}
}
// By translating horizontally by a % of the total width, the slide is acheived.
// So, for example, say there are 3 slides. The container for the slides is
// 300% of the width of the parent. If the slider goes to the first
// slide (at index 0), then the calculation is -(0 * 100 / 3), or -0.
// If the slider goes to the third slide (at index 2), then the calculation
// is -(2 * 100 / 3), or -66.6%. The slide container will move 66.6% of it's total
// width to the left, which will bring the last slide fully into view.
slideHorizontal() {
this.panelsContainer.style.transform = `translateX(-${(this.state.currentPanelIndex * 100) / this.panels.length}%)`;
}
// The init function triggers the slider.
init() {
// Set the width of the panels container to panels.lenth * 100% of the parent, to allow it to slide.
this.panelsContainer.style.width = `${this.panels.length * 100}%`;
// The slider always starts on the first item.
this.prevBtn.classList.add(`disabled`);
// Add an index data attribute to all the nav items, and make the first item active.
// FIXME Hacky, adding event handler to each item for the minute
// this.panelNavItems.forEach(navItem => addEventListener('click', this.gotoPanelHandler.bind(this), true));
this.panelNavItems[0].classList.add(`active`);
this.prevBtn.addEventListener('click', this.prevPanelHandler.bind(this), true);
this.nextBtn.addEventListener('click', this.nextPanelHandler.bind(this), true);
}
}
const mySlider = new Slider({panelsSelector: `.panels`, prevBtnSelector: `.panels-nav-prev`, nextBtnSelector: `.panels-nav-next`, panelNavSelector: `.panels-nav-menu`});
mySlider.init();
Also see: Tab Triggers