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="container">
<div class="axisX"></div>
<div class="axisY"></div>
<div class="ball"></div>
<div class="button-container">
<div class="button" onClick="linear()">匀速直线</div>
<div class="button" onClick="gravity()">重力</div>
<div class="button" onClick="friction()">摩擦力</div>
<div class="button" onClick="horizontalMotion()">平抛</div>
<div class="button" onClick="horizontalMotionWidthRotate()">旋转+平抛</div>
<div class="button" onClick="arrowMove()">拉弓</div>
<div class="button" onClick="bezier()">贝塞尔曲线</div>
<div class="button" onClick="bounceMove()">缓动函数实现弹球</div>
<div class="button" onClick="asyncBounce()">弹球</div>
<div class="button" onClick="ellipsis()">椭圆</div>
</div>
</div>
body {
margin: 0;
padding: 0;
}
#container {
position: absolute;
width: 100%;
height: 100vh;
}
.axisX {
height: 2px;
background: black;
width: 100%;
position: absolute;
top: 60%;
transform: translate(0, -1px);
}
.axisY {
width: 2px;
background: black;
height: 100vh;
position: absolute;
left: 50%;
transform: translate(-1px, 0);
}
.ball {
position: absolute;
width: 40px;
height: 40px;
border-radius: 20px;
background: red;
top: calc(60% - 20px);
left: calc(50% - 20px);
}
.ball::before {
content: "";
position: absolute;
left: 10px;
top: 10px;
background: yellow;
width: 10px;
height: 10px;
border-radius: 5px;
}
.button-container {
position: absolute;
bottom: 50px;
left: 100px;
}
.button {
cursor: pointer;
border: 2px solid black;
padding: 10px 20px;
font-size: 20px;
display: inline-block;
}
.button:hover {
background: gray;
}
function animate ({easing, draw, duration}) {
let start = performance.now();
return new Promise(resolve => {
requestAnimationFrame(function animate(time) {
// timeFraction goes from 0 to 1
let timeFraction = (time - start) / duration;
if (timeFraction > 1) timeFraction = 1;
// calculate the current animation state
let progress = easing(timeFraction)
draw(progress); // draw it
if (timeFraction < 1) {
requestAnimationFrame(animate);
} else {
resolve();
}
});
});
}
const ball = document.querySelector('.ball');
// 沿着x轴匀速直线运动
const linear = () => {
const draw = (progress) => {
ball.style.transform = `translate(${progress}px, 0)`;
}
// 沿着x轴匀速运动
animate({
duration: 1000,
easing(timeFraction) {
return timeFraction * 100;
},
draw,
});
};
// 重力
const gravity = () => {
const draw = (progress) => {
ball.style.transform = `translate(0, ${500 * (progress - 1)}px)`;
};
// 沿着x轴匀速运动
animate({
duration: 1000,
easing(timeFraction) {
return timeFraction ** 2;
},
draw,
});
};
// 摩擦力
const friction = () => {
// 初始高度500px
const draw = (progress) => {
ball.style.transform = `translate(0, ${500 * (progress - 1)}px)`;
};
// 沿着x轴匀速运动
animate({
duration: 1000,
easing(timeFraction) {
// 初始速度系数为2
return timeFraction * (2 - timeFraction);
},
draw,
});
};
// 平抛
const horizontalMotion = () => {
const draw = (progress) => {
ball.style.transform = `translate(${500 * progress.x}px, ${500 * (progress.y - 1)}px)`;
};
// 有两个方向,沿着x轴匀速运动,沿着y轴加速运动
animate({
duration: 1000,
easing(timeFraction) {
return {
x: timeFraction,
y: timeFraction ** 2,
}
},
draw,
});
};
// 旋转 + 平抛
const horizontalMotionWidthRotate = () => {
const draw = (progress) => {
ball.style.transform = `translate(${500 * progress.x}px, ${500 * (progress.y - 1)}px) rotate(${2000 * progress.rotate}deg)`;
};
// 有两个方向,沿着x轴匀速运动,沿着y轴加速运动
animate({
duration: 2000,
easing(timeFraction) {
return {
x: timeFraction,
y: timeFraction ** 2,
rotate: timeFraction,
}
},
draw,
});
};
// 拉弓
const arrowMove = () => {
const back = (x, timeFraction) => {
return Math.pow(timeFraction, 2) * ((x + 1) * timeFraction - x);
}
const draw = (progress) => {
ball.style.transform = `translate(${200*progress.x}px, ${ - 500 * progress.y}px)`;
};
animate({
duration: 1000,
easing(timeFraction) {
return {
x: timeFraction,
y: back(2, timeFraction),
};
},
draw,
});
};
// 贝塞尔
const bezier = () => {
const bezierPath = (x1, y1, x2, y2, timeFraction) => {
const x = 3 * x1 * timeFraction * (1 - timeFraction) ** 2 + 3 * x2 * timeFraction ** 2 * (1 - timeFraction) + timeFraction ** 3;
const y = 3 * y1 * timeFraction * (1 - timeFraction) ** 2 + 3 * y2 * timeFraction ** 2 * (1 - timeFraction) + timeFraction ** 3;
return [x, y];
}
const draw = (progress) => {
const [px, py] = bezierPath(0.2, 0.6, 0.8, 0.2, progress);
ball.style.transform = `translate(${300 * px}px, ${-300 * py}px)`;
}
animate({
duration: 2000,
easing(timeFraction) {
return timeFraction * (2 - timeFraction);
},
draw,
});
}
// 缓动函数实现弹球
const bounceMove = () => {
const bounce = (timeFraction) => {
if (timeFraction < 1 / 2.75) {
return 7.5625 * timeFraction * timeFraction
} else if (timeFraction < 2 / 2.75) {
return 7.5625 * (timeFraction -= 1.5 / 2.75) * timeFraction + 0.75
} else if (amount < 2.5 / 2.75) {
return 7.5625 * (timeFraction -= 2.25 / 2.75) * timeFraction + 0.9375
} else {
return 7.5625 * (timeFraction -= 2.625 / 2.75) * timeFraction + 0.984375
}
}
const draw = (progress) => {
ball.style.transform = `translate(${200*progress.x}px, ${200 * (progress.y - 1)}px)`;
};
animate({
duration: 1000,
easing(timeFraction) {
return {
x: timeFraction,
y: bounce(timeFraction),
};
},
draw,
});
};
// 弹球
const asyncBounce = () => {
(async function() {
let damping = 0.7, // 衰变系数
duration = 1000,
height = 300;
while(height >= 1) {
const down = (progress) => {
ball.style.transform = `translate(0, ${height * (progress - 1)}px)`;
};
await animate({
duration: duration,
easing(timeFraction) {
return timeFraction ** 2;
},
draw: down,
});
height *= damping ** 2;
duration *= damping;
const up = (progress) => {
ball.style.transform = `translate(0, ${-height * progress}px)`;
}
await animate({
duration: duration,
easing(timeFraction) {
return timeFraction * (2 - timeFraction);
},
draw: up,
});
}
}());
};
// 椭圆
const ellipsis = () => {
const draw = (progress) => {
const x = 150 * Math.cos(Math.PI * 2 * progress);
const y = 100 * Math.sin(Math.PI * 2 * progress);
ball.style.transform = `translate(${x}px, ${y}px)`;
}
animate({
duration: 2000,
easing(timeFraction) {
return timeFraction * (2 - timeFraction);
},
draw,
});
};
// 延时函数
// await animate({
// duration: 1000,
// easing: () => {},
// draw: () => {},
// });
Also see: Tab Triggers