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.
<div class="container">
<div class="wrapper">
<div class="descriptions">
<div class="title">TITLE ONE</div>
<div class="title">TITLE TWO</div>
<div class="title">TITLE THREE</div>
</div>
<div class="content">
<div class="track"></div>
<div class="item"><div class="child">1</div></div>
<div class="item"><div class="child">2</div></div>
<div class="item"><div class="child">3</div></div>
</div>
</div>
</div>
<div style="text-align: center"><button class="previous">previous</button> <button class="next">next</button></div>
body {
margin: 0;
padding: 0;
position: relative;
}
.container {
box-sizing: border-box;
position: relative;
width: 100%;
height: auto;
background-color: #aac9d0;
padding: 0;
margin: 0;
overflow: hidden;
padding: 50px;
}
.wrapper {
position: relative;
width: 100%;
max-width: 500px;
height: auto;
margin: 0 auto;
padding: 0;
}
#path-svg {
position: relative;
width: 100%;
height: auto;
z-index: 1;
}
.content {
position: relative;
top: 0;
left: 0;
/*width: 100%;*/
/*height: 0;*/
overflow: visible;
/*padding-bottom: 84%;*/
z-index: 2;
}
.content::before {
float: left;
padding-top: 84%;
content: "";
}
.content::after {
display: block;
content: "";
clear: both;
}
.track {
border: 1px solid #708a91;
border-radius: 50%;
position: absolute;
width: 100%;
height: 100%;
}
.item {
width: 50px;
height: 50px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #708a91;
border-radius: 50%;
}
.item .child {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
color: #fee3c9;
display: flex;
justify-content: center;
align-items: center;
}
.descriptions {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 3;
pointer-events: none;
}
.title {
position: absolute;
top: 50px;
left: 50%;
transform: translateX(-50%);
width: fit-content;
height: auto;
opacity: 0;
/* transition: 0.3s; */
font-size: 20px;
}
.title.active {
opacity: 1;
}
.item.active {
background-color: red;
}
@media (max-width: 799px) {
.wrapper {
max-width: 360px;
}
}
gsap.registerPlugin(Draggable, InertiaPlugin);
let descriptions = gsap.utils.toArray(".descriptions .title");
let items = gsap.utils.toArray(".content .item");
gsap.set(descriptions[0], {autoAlpha: 1}); // make the first description visible.
let carousel = buildCarousel(items, {
radiusX: 250,
radiusY: 210,
activeAngle: -90,
draggable: true,
autoAdvance: 2, // seconds between next() calls
onClick(element, self) {
self.to(element, {duration: 1, ease: "power1.inOut"}, "short");
},
onActivate(element, self) {
element.classList.add("active");
},
onDeactivate(element, self) {
element.classList.remove("active");
},
// when a drag or animation starts (via the Carousel's to()/next()/previous() methods)
onStart(element, self) {
gsap.to(descriptions[items.indexOf(element)], {autoAlpha: 0, duration: 0.25, overwrite: "auto"});
},
onStop(element, self) {
gsap.to(descriptions[items.indexOf(element)], {autoAlpha: 1, overwrite: "auto"});
}
});
document.querySelector(".next").addEventListener("click", () => carousel.next());
document.querySelector(".previous").addEventListener("click", () => carousel.previous());
// example of resizing
let mm = gsap.matchMedia();
mm.add("(min-width: 800px)", () => { // desktop
carousel.resize(250, 210);
});
mm.add("(max-width: 799px)", () => { // mobile
carousel.resize(180, 150);
});
/*
Setup: place all the targets on top of each other, position: absolute, and then pass them in to this helper function and it'll spread them out from their starting position as if that's the origin.
The config object (2nd parameter) can have any of the following optional properties:
- radiusX: Number - radius (in pixels) on the x-axis
- radiusY: Number - radius (in pixels) on the y-axis
- activeAngle: Number - angle (in degrees) that's considered the "active" spot (defaults to -90 meaning the 12 o'clock position). 0 would be on the far right side. 90 would be bottom, etc.
- activeElement: Element | String - the element that should initially be active
- autoAdvance: Number [optional] - number of seconds between next() calls
- onActivate: Function - called when a new element is considered the "active" one (closest to the active slot). The first parameter is the active element, the second is the Carousel instance itself.
- onDeactivate: Function - called when an element goes from being the active one to NOT the active one. The first parameter is the formerly active element, the second is the Carousel instance itself.
- onClick: Function - called when an element is clicked
- onStart: Function - called when a drag or animation starts (via the Carousel's to()/next()/previous() methods)
- onStop: Function - called when a drag or animation stops (via the Carousel's to()/next()/previous() methods);
- draggable: Boolean - if true, the carousel will be draggable. Don't forget to load Draggable and InertiaPlugin
An object gets returned that has the following methods:
- rotation() - a getter/setter for the rotation (in degrees) of the overall carousel.
- activeElement() - returns the currently active element (the one closest to the "active" slot)
- elementRotation() - pass in an element and it'll return the rotation of the carousel corresponding to when that particular element is active
- to() - lets you animate to a particular element or rotation and you get total control of that animation by using the vars and direction parameters. Method signature:
- elementOrRotation: Element | String | Number - the destination element or rotation value
- vars: Object - configuration object for the tween, just like any gsap.to() vars object so you can define duration, ease, onComplete, whatever.
- direction: String [optional] - "short" goes in the shortest direction, "cw" goes in the clockwise direction, and "ccw" goes in the counter-clockwise direction.
- next() - goes to the next element. You can pass in a vars object and direction if you'd like to control the animation
- previous() - goes to the previous element. You can pass in a vars object and direction if you'd like to control the animation
- resize(radiusX, radiusY) - for resizing
- kill() - kills the carousel, cleaning up event listeners, etc.
*/
function buildCarousel(targets, {radiusX = 200, radiusY = 200, activeAngle = -90, activeElement, onClick, onActivate, onDeactivate, onStart, onStop, draggable, autoAdvance}) {
targets = gsap.utils.toArray(targets);
gsap.set(targets, {xPercent: -50, x: 0, yPercent: -50, y: 0});
let DEG2RAD = Math.PI / 180,
eventTypes = ("ontouchstart" in document.documentElement ? "touchstart,touchmove,touchcancel,touchend" : !("onpointerdown" in document.documentElement) ? "mousedown,mousemove,mouseup,mouseup" : "pointerdown,pointermove,pointercancel,pointerup").split(","),
round = value => Math.round(value * 10000) / 10000,
tempDiv = document.createElement("div"),
quantity = targets.length,
angleInc = 360 / quantity,
wrap = gsap.utils.wrap(0, quantity),
angleWrap = gsap.utils.wrap(0, 360),
rotation = 0,
dragged,
onPressRotation,
autoAdvanceCall = autoAdvance && gsap.delayedCall(parseFloat(autoAdvance) || 2, () => { self.next(); autoAdvanceCall.restart(true); }),
xSetters = targets.map(el => gsap.quickSetter(el, "x", "px")),
ySetters = targets.map(el => gsap.quickSetter(el, "y", "px")),
self = {
rotation(value) {
if (arguments.length) {
let prevActive = activeElement;
rotation = angleWrap(value);
activeElement = targets[wrap(Math.round(-value / angleInc))];
self.render();
if (prevActive !== activeElement) {
onDeactivate && prevActive && onDeactivate(prevActive, self);
onActivate && onActivate(activeElement, self);
}
}
return rotation;
},
resize(rx, ry) {
radiusX = rx;
radiusY = ry;
self.render();
},
render() {
let inc = angleInc * DEG2RAD,
a = (rotation + activeAngle) * DEG2RAD,
i = 0;
for (; i < quantity; i++) {
xSetters[i](round(Math.cos(a) * radiusX));
ySetters[i](round(Math.sin(a) * radiusY));
a += inc;
}
},
activeElement(value) {
if (arguments.length) {
self.rotation(self.elementRotation(value));
}
return activeElement;
},
elementRotation(element) {
let index = targets.indexOf(gsap.utils.toArray(element)[0]);
return (quantity - index) * angleInc;
},
to(elOrRotation, vars, direction) {
vars = vars || {};
vars.rotation = typeof(elOrRotation) === "number" ? elOrRotation : (self.elementRotation(elOrRotation) || parseFloat(elOrRotation));
vars.overwrite = true;
let { onUpdate, onComplete } = vars,
_onStart = vars.onStart;
autoAdvanceCall && autoAdvanceCall.pause();
vars.onStart = function() {
onStart && onStart(activeElement, self);
_onStart && _onStart.call(this);
}
vars.onComplete = function() {
onStop && onStop(activeElement, self);
onComplete && onComplete.call(this);
autoAdvanceCall && autoAdvanceCall.restart(true);
};
if (direction) {
let getter = gsap.getProperty(tempDiv);
vars.onUpdate = function() {
self.rotation(getter("rotation"));
onUpdate && onUpdate.call(this);
}
vars.rotation += "_" + direction;
return gsap.fromTo(tempDiv, {rotation: rotation}, vars);
}
return gsap.to(self, vars);
},
next(vars, direction) {
let element = targets[wrap(targets.indexOf(activeElement) + 1)];
self.to(element, vars, direction || "ccw");
},
previous(vars, direction) {
let element = targets[wrap(targets.indexOf(activeElement) - 1)];
self.to(element, vars, direction || "cw");
},
kill() {
targets.forEach(el => {
el.removeEventListener("click", _onClick);
el.removeEventListener(eventTypes[0], onPress);
el.removeEventListener(eventTypes[2], onRelease);
el.removeEventListener(eventTypes[3], onRelease);
});
gsap.killTweensOf(self);
tempDiv.parentNode && tempDiv.parentNode.removeChild(tempDiv);
autoAdvanceCall && autoAdvanceCall.kill();
draggable && draggable.kill();
},
autoAdvance: autoAdvanceCall
},
_onClick = e => {
if (!dragged) {
autoAdvanceCall && autoAdvanceCall.restart(true);
onClick && onClick(e.currentTarget, self);
}
},
onPress = e => {
onPressRotation = rotation;
gsap.set(tempDiv, {rotation: rotation});
autoAdvanceCall && autoAdvanceCall.pause();
gsap.killTweensOf(self);
draggable.startDrag(e);
dragged = false;
},
onRelease = e => {
draggable.endDrag(e);
if (rotation === onPressRotation) {
autoAdvanceCall && autoAdvanceCall.restart(true);
draggable.tween && draggable.tween.kill();
_onClick(e);
}
},
syncDraggable = () => {
if (!dragged) {
onStart && onStart(activeElement, self);
dragged = true;
}
self.rotation(draggable.rotation);
};
targets[0].parentNode.appendChild(tempDiv);
gsap.set(tempDiv, {visibility: "hidden", position: "absolute", width: 0, height: 0, top: "50%", left: "50%", xPercent: -50, yPercent: -50});
targets.forEach(el => {
if (draggable) {
el.addEventListener(eventTypes[0], onPress);
el.addEventListener(eventTypes[2], onRelease);
el.addEventListener(eventTypes[3], onRelease);
} else {
el.addEventListener("click", _onClick);
}
});
self.snap = angleInc;
draggable && (self.draggable = draggable = Draggable.create(tempDiv, {type: "rotation", snap: gsap.utils.snap(angleInc), inertia: true, onThrowComplete: () => {autoAdvanceCall && autoAdvanceCall.restart(true); onStop && onStop(activeElement, self)}, onThrowUpdate: syncDraggable, onDrag: syncDraggable})[0])
self.activeElement(gsap.utils.toArray(activeElement)[0] || targets[0]);
return self;
}
Also see: Tab Triggers