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 id="tracker">
<!-- I found that mouse tracking doesn't work well with an SVG; the browser changes the window position when hovering over the SVG. The solution was to instead create an invisible element (this one) over the top of the page that tracks the movements, and to use JS to fake the other hover effects. -->
</div>
<div id="text">
<p>Move your mouse to <span class="highlight">play with the cat.</span></p>
<p>Click the button to <span class="highlight">get back to work.</span></p>
</div>
<main>
<button id="target">
Stop<br />playing
<img id="cursor" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/240751/mac-osx-arrow-cursor.png" alt="">
</button>
<!-- SVG created with Affinity Designer -->
<svg id="Cat" width="100%" height="100%" viewBox="-80 0 200 199" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:1.5;"><rect id="Artboard1" x="0" y="0" width="104.557" height="198.931" style="fill:none;"/><path id="Body" d="M21.605,75.113c2.759,17.247 -5.015,37.589 -6.724,59.831c-1.71,22.241 0.016,62.893 30.999,62.893c0.946,0 1.873,-0.075 2.784,-0.058c34.988,0.655 40.376,-21.661 35.482,-45.091c-5.047,-24.167 -22.908,-35.604 -24.535,-74.071c-1.627,-38.467 -42.011,-28.548 -38.006,-3.504Z"/><path id="Head-main" serif:id="Head main" d="M60.011,27.971c4.811,-11.449 12.131,-28.237 13.564,-27.938c2.015,0.422 6.111,41.652 6.688,47.562c0.027,0.233 0.049,0.467 0.068,0.702l0,0.001l0,0c0.043,0.537 -0.064,1.099 0.064,1.639c4.823,20.225 -6.8,28.68 -39.075,28.68c-32.275,0 -43.354,-10.637 -39.075,-28.68c0.128,-0.541 0.022,-1.102 0.064,-1.64l-0.064,0.001c0,0 5.295,-47.81 7.466,-48.265c1.425,-0.298 8.674,16.308 13.486,27.752c5.404,-1.544 11.553,-2.337 18.078,-2.337c6.783,0 13.168,0.856 18.736,2.523Z"/><g id="Left-eye" serif:id="Left eye"><circle cx="17.962" cy="55.289" r="11.393" style="fill:#fff;"/><g id="Left-eye-pupil" serif:id="Left eye pupil"><circle cx="16.068" cy="53.396" r="9.499"/><circle cx="13.934" cy="48.382" r="2.134" style="fill:#fff;"/></g></g><g id="Right-eye" serif:id="Right eye"><circle cx="64.679" cy="55.289" r="11.393" style="fill:#fff;"/><g id="Right-eye-pupil" serif:id="Right eye pupil"><circle cx="61.777" cy="53.396" r="9.499"/><circle cx="59.643" cy="48.382" r="2.134" style="fill:#fff;"/></g></g><path id="Tail" d="M80.004,178.286c-0.131,0.058 12.828,-18.528 5.347,-42.302c-7.482,-23.774 2.767,-37.597 13.789,-42.67" style="fill:none;stroke:#000;stroke-width:8px;"/><path id="Paw" d="M29.354,99.466c-0.04,-20.799 15.679,-43.724 6.404,-62.895" style="fill:none;stroke:#000;stroke-width:10px;"/></svg>
</main>
@import url('https://fonts.googleapis.com/css?family=Luckiest+Guy&display=swap');
$accent: #7BA7BC;
$paw: 1rem;
$black: #333;
*, *:before, *:after {
box-sizing: inherit;
font-family: inherit;
position: inherit;
}
body {
background: #feeeed /* me */;
margin: 0;
box-sizing: border-box;
position: relative;
display: grid;
place-items: center;
min-height: 100vh;
font-size: 18px;
font-family: 'Luckiest Guy', sans-serif;
color: $accent;
grid-template-rows: 1fr 4fr;
overflow: hidden;
#tracker {
width: 100vw;
height: 100vh;
position: fixed;
top: 0;
left: 0;
z-index: 5;
}
}
p {
font-size:calc(1.1rem + 1vw);
line-height: 1.4em;
margin: 0;
text-align: center;
color: $black;
.highlight {
color: $accent;
}
}
main {
width: 100%;
max-width: 22rem;
display: flex;
align-items: center;
button {
padding: .5em .8em .3em;
font-weight: bold;
background: $accent;
color: #fff;
border-radius: 5rem;
margin-right: -10rem;
cursor: none;
border: none;
font-size: 1.5rem;
line-height: 1;
letter-spacing: .05em;
color: $black;
border: .2rem solid $black;
transition: margin .05s ease;
transform-origin: center;
&:focus, &:hover, &:active {
outline: none;
border: .3rem solid $black;
}
}
#cursor {
position: absolute;
width: 16px;
left: 50%;
top: 50%;
z-index: -2;
transform-origin: center;
opacity: 0;
&.swatted {
opacity: 1;
animation: swatted .15s ease-out;
}
}
#Cat {
transform-origin: bottom center;
animation: tail 7s infinite alternate;
z-index: 0;
pointer-events: none;
}
#Tail {
transform-origin: bottom;
animation: tail 2s ease infinite alternate;
}
#Paw {
transform-origin: 16% 53%;
transform: rotate(-185deg);
transition: all .2s;
&.ready-to-swat {
transform: rotate(-56deg);
}
}
.swat {
animation: swat .2s;
filter: blur(10px);
}
#Left-eye-pupil, #right-eye-pupil {
transform-origin: center;
}
#Left-eye, #Right-eye {
animation: blink 8s infinite alternate;
transform-origin: 50% 26%;
transition: all .25s ease;
}
}
@keyframes tail {
0% {
transform: rotate(0deg);
}
27% {
transform: rotate(2deg);
}
69% {
transform: rotate(-2deg);
}
100% {
transform: rotate(0deg);
}
}
@keyframes swat {
0% {
transform: rotate(-20deg) scale(1.2);
}
25% {
transform: rotate(-82.5deg) scale(1.2);
}
50% {
transform: rotate(-185deg) scale(1.2);
}
51% {
transform: rotate(-20deg) scale(1.2);
}
75.5% {
transform: rotate(-82.5deg) scale(1.2);
}
100% {
transform: rotate(-185deg) scale(1.2);
}
}
@keyframes blink {
0% {
transform: scaleY(1);
}
39% {
transform: scaleY(1);
}
40% {
transform: scaleY(0);
}
41% {
transform: scaleY(1);
}
69% {
transform: scaleY(1);
}
70% {
transform: scaleY(0);
}
71% {
transform: scaleY(1);
}
100% {
transform: scaleY(1);
}
}
@keyframes swatted {
0% {
opacity: 1;
transform: scale(1) translate(0, 0) rotate(0deg);
}
100% {
opacity: 1;
transform: scale(3) translate(-15vw, 20vh) rotate(1080deg);
}
}
//Just a shorthand since there's a lot of selectors
const get = arg => document.querySelector(arg);
//Select all the things
const tracker = get('#tracker');
const button = get('button');
const paw = get('#Paw');
const tail = get('#Tail');
const leftEye = get('#Left-eye-pupil');
const rightEye = get('#Right-eye-pupil');
const cat = get('svg');
const cursor = get('#cursor');
//What happens when mousing over the button
const mouseOver = () => {
paw.style.transform = 'rotate(-185deg)';
if(!paw.classList.value.includes('swat')) {
paw.classList.add('swat');
cursor.classList.add('swatted');
document.body.style.cursor = 'none';
cat.style.animation = 'none';
tail.style.animation = 'none';
document.body.style.cursor = 'none';
button.style.marginRight = '-11rem';
}
}
//What happens when mousing out from the button
const mouseOut = () => {
if(paw.classList.value.includes('swat')) {
paw.classList.remove('swat');
cursor.classList.remove('swatted');
cat.style.animation = 'tail 7s infinite alternate';
tail.style.animation = 'tail 2s ease infinite alternate';
document.body.style.cursor = 'unset';
button.style.marginRight = '-10rem';
}
}
//What happens when we're near the button
const mouseNear = (coords, x, y) => {
const minY = coords.y - coords.height;
const maxY = coords.y + (coords.height * 2);
const minX = coords.x - coords.width;
const maxX = coords.x + (coords.width * 2);
const zeroX = (maxX + minX) / 2;
const zeroY = (maxY + minY) / 2;
paw.style.transform = `rotate(-${60 / (zeroX + zeroY) * (x + y)}deg)`;
}
//Boolean; whether the cursor is over the button
const overTheButton = (coords, x, y) => {
return y >= coords.y && y <= coords.y + coords.height && x >= coords.x && x <= coords.x + coords.width;
}
//Boolean; whether the cursor is near the button
const nearTheButton = (coords, x, y) => {
return y >= coords.y - (coords.height * 2) && y <= coords.y + (coords.height * 3) && x >= coords.x - coords.width && x <= coords.x + (coords.width * 2)
}
//The actual event listener that tracks the mouse (see the HTML note)
tracker.addEventListener('mousemove', e => {
const btnCoords = button.getBoundingClientRect();
const x = e.offsetX;
const y = e.offsetY;
const windowHeight = window.innerHeight;
const windowWidth = window.innerWidth;
const max = 3;
//Execute the proper function(s) from above based on cursor placement
if(overTheButton(btnCoords, x, y)) {
mouseOver()
} else {
mouseOut();
if (nearTheButton(btnCoords, x, y)){
mouseNear(btnCoords, x, y);
} else {
paw.style.transform = 'rotate(-185deg)';
}
}
//Eye movement
[leftEye, rightEye].forEach(eye => {
eye.style.transform =
`translate(
${max / (windowWidth / x)}%,
${max / (windowHeight / y)}%
)`;
});
});
Also see: Tab Triggers