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.
<svg id="demo" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 0 0" width="0" height="0" stroke-linecap="round" stroke-linejoin="round">
<rect id="background" x="0" y="0" width="100%" height="100%" fill="transparent" />
<g id="outlines"></g>
<g id="text"></g>
<g id="circles"></g>
</svg>
<div id="wrap">
<div class="panel"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/314556/squareDog1.jpg"></div>
<div class="panel"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/314556/squareDog2.jpg"></div>
<div class="panel"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/314556/squareDog3.jpg"></div>
<div class="panel"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/314556/squareDog4.jpg"></div>
<div class="panel"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/314556/squareDog5.jpg"></div>
<div class="panel"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/314556/squareDog6.jpg"></div>
</div>
* {
box-sizing: border-box;
}
body {
height: 100vh;
width: 100%;
padding: 0;
margin: 0;
overflow: hidden;
font-family: "Roboto", sans-serif;
}
.panel {
height: 100%;
width: 100%;
border-radius: 100%;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
border: solid 10px white;
}
.panel img {
width: 100%;
height: 100%;
}
#demo {
position: absolute;
width: auto;
height: auto;
max-height: 140px;
max-width: 90%;
font-weight: 700;
left: 50%;
bottom: 20px;
opacity: 0;
}
#circles path {
cursor: pointer;
}
#wrap {
width: 50%;
height: 50%;
left: 50%;
position: absolute;
border: solid 10px #42a6e0;
border-radius: 100%;
overflow: hidden;
opacity: 0;
}
console.clear();
TweenLite.defaultEase = Linear.easeNone;
var demo = document.querySelector("#demo");
var groups = [document.querySelector("#demo #outlines"), document.querySelector("#demo #circles") ];
var textGroup = document.querySelector("#demo #text");
var textElements = [];
var svgns = "http://www.w3.org/2000/svg";
var panels = document.querySelectorAll(".panel");
// *********************** adjustable stuff ***********************
var count = panels.length; // how many circles - automatically adjusts to number of panels
var r = 100; // circle radius
var spacer = 25; // space between circles
var strokes = [2, 12]; // [outline stroke width, animating stroke width]
var circleFill = ["transparent", "transparent"]; // [outline circle fill, animating circle fill]
var colors = ["#000", "#42a6e0"]; // [outline stroke color, animating stroke color]
var opacity = [0.2, 1]; // [outline stroke opacity, animating stroke opacity]
var fs = r/2; // font-size
// ***********************************************************
var middles = []; // array of circle centers
var oldTarget = 0; // starting target
var newTarget = 0; // new target
var tl; // timeline
var master; // timeline
var oldDraw; // circle animation direction calculation variable
var newDraw; // circle animation direction calculation variable
var lineTarget; // line calculation percentage variable
var stretchTarget; // line calculation percentage variable
var demoHeight = r * 2 + strokes[1];
var demoWidth = (r * 2 + spacer) * count - spacer + strokes[1];
var rd;
// trim the SVG to the circles & position gallery panels
TweenMax.set(demo, { attr: { viewBox: "0 0 " + demoWidth + " " + demoHeight, width:demoWidth, height:demoHeight }, xPercent:-50 });
TweenMax.set("#wrap", {xPercent:-50, yPercent:-50});
TweenMax.set(panels, {rotation:180, transformOrigin:"center bottom"});
TweenMax.set(panels[0], {rotation:0});
// loops to create the circles, text and lines needed
for (let j = 0; j < 2; j++) {
for (let i = 0; i < count; i++) {
var middle = i * (r * 2) + r + spacer * i + strokes[1] / 2;
if (j === 0) {
middles.push(middle);
}
var circle = document.createElementNS(svgns, "circle");
circle.setAttributeNS(null, "cx", middle);
circle.setAttributeNS(null, "cy", demoHeight/2);
circle.setAttributeNS(null, "r", r);
groups[j].appendChild(circle);
TweenLite.set(circle, {
stroke: colors[j],
strokeWidth: strokes[j],
opacity: opacity[j],
fill: circleFill[j]
});
if (j === 0) {
var txt = document.createElementNS(svgns, "text");
txt.setAttributeNS(null, "x", middle);
txt.setAttributeNS(null, "y", demoHeight/2);
txt.setAttributeNS(null, "font-size", fs);
txt.setAttributeNS(null, "text-anchor", "middle");
txt.setAttributeNS(null, "fill", colors[0]);
txt.setAttributeNS(null, "opacity", opacity[0]);
txt.setAttributeNS(null, "dominant-baseline", "central"); //MS Edge doesn't like this
txt.innerHTML = i + 1;
textGroup.appendChild(txt);
textElements.push(txt);
}
}
var line = document.createElementNS(svgns, "line");
line.setAttributeNS(null, "x1", middles[0]);
line.setAttributeNS(null, "x2", middles[count - 1]);
line.setAttributeNS(null, "y1", demoHeight/2 + r);
line.setAttributeNS(null, "y2", demoHeight/2 + r);
groups[j].appendChild(line);
TweenLite.set(line, {
fill: "none",
stroke: colors[j],
strokeWidth: strokes[j],
opacity: opacity[j]
});
}
// convert the circles to paths so the correct 6 o'clock starting point works in all browsers
MorphSVGPlugin.convertToPath("#circles circle");
var targets = document.querySelectorAll("#circles path");
// make targets from the paths, add listeners
for (let i = 0; i < targets.length; i++) {
targets[i].index = i;
targets[i].addEventListener("click", doCoolStuff);
}
var lineA = document.querySelectorAll("line")[1]; // target for animating line
TweenMax.set(targets, {rotation: 90, transformOrigin: "center center", drawSVG: 0}); // start at 6 o'clock
TweenMax.set(targets[0], {drawSVG: true}); // highlight starting circle
TweenMax.set(textElements[0], {fill:colors[1], opacity:opacity[1]}); // highlight starting text
var startMiddle = middles[0];
TweenMax.set(lineA, {attr:{x1:middles[0], x2:middles[0]}}); // zero out the animating line
var cf = DrawSVGPlugin.getLength( targets[0] ); // circumference needed for durtion calculations
// do all the fancy stuff on click
function doCoolStuff() {
newTarget = this.index;
if (oldTarget === newTarget) {
return;
}
if (master && master.isActive()) {
master.progress(1);
}
master = new TimelineMax(); // main timeline
// figure which way the old and new circle need to draw on/off depending on the travel direction of the line.
if (newTarget > oldTarget) {
// traveling left to right
oldDraw = { drawSVG: "0% 0%" };
newDraw = { drawSVG: "100% 100%" };
lineTarget = middles[oldTarget] + cf;
stretchTarget = middles[newTarget] - cf;
rd = 180;
} else {
// traveling right to left
oldDraw = { drawSVG: "100% 100%" };
newDraw = { drawSVG: "0% 0%" };
lineTarget = middles[oldTarget] - cf;
stretchTarget = middles[newTarget] + cf;
rd = -180;
}
var dur = 10; // duration value is irrelevant since actual animation is a progress() tween
var distance = Math.abs(startMiddle - middles[newTarget]); // how far away is the clicked target
startMiddle = middles[newTarget]; // middle point of target circle
var totalDistance = distance + cf; // travel distance plus circumference of the circle
var circDur = cf / totalDistance * dur; // duration for the circle unwrap/wrap
tl = new TimelineMax({ paused: true }); // line animation timeline
tl.fromTo(targets[oldTarget], circDur, { drawSVG: "0% 100%" }, oldDraw); // erase active circle
if (distance > cf) {
// line needs to 'unwrap' and travel a bit by itself before wrapping around new target
tl.to(lineA, circDur, {attr:{x1:middles[oldTarget], x2:lineTarget}}, 0);
tl.to(lineA, dur-circDur*2, {attr:{x1:stretchTarget, x2:middles[newTarget]}});
tl.to(lineA, circDur, {attr:{x1:middles[newTarget], x2:middles[newTarget]}}, "part2");
} else {
// line needs to start wrapping around new target before fully unwrapped on old target
tl.to(lineA, dur - circDur, {attr:{x1:middles[oldTarget], x2:middles[newTarget]}}, 0);
tl.add("part2", dur - circDur);
tl.to(lineA, dur - circDur, {attr:{x1:middles[newTarget], x2:middles[newTarget]}}, circDur);
}
tl.fromTo(targets[newTarget], circDur, newDraw, {drawSVG:"0% 100%"}, "part2"); // draw on target circle
tl.progress(1).progress(0);
master.to(tl, 1, {progress:1, ease:Power2.easeInOut});
master.to(textElements[oldTarget], 1, {fill:colors[0], opacity:opacity[0]}, 0);
master.to(textElements[newTarget], 1, {fill:colors[1], opacity:opacity[1]}, 0);
master.to(panels[oldTarget], 1, {rotation:-rd, ease:Back.easeIn}, 0);
master.fromTo(panels[newTarget], 1, {rotation:rd}, {rotation:0, ease:Back.easeOut}, "-=0.35");
oldTarget = newTarget; // flip values for next click;
}
// center gallery in the available space above the circle controls
function newSize() {
var wh = window.innerHeight - demo.getBoundingClientRect().height;
var ww = window.innerWidth;
TweenMax.set("#wrap", { top: wh / 2 });
var factor = 0.88;
if (wh > ww) {
TweenMax.set("#wrap", { width: ww * factor, height: ww * factor });
} else {
TweenMax.set("#wrap", { width: wh * factor, height: wh * factor });
}
}
TweenMax.set("#wrap, #demo", { autoAlpha: 1 });
newSize();
window.addEventListener("resize", newSize);
Also see: Tab Triggers