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.
<h1>Intersection Observer Scroll Animation</h1>
<div class="label-img-box js-anime-box">
<div class="label js-anime-elm is-anime-scaleLeftRight is-anime-delay4"></div>
<img src="https://picsum.photos/id/1043/1200/560" alt="" class="label-img js-anime-elm is-anime-delay8 is-anime-duration1" width="1200" height="560">
</div>
<div class="label-img-box label-img-box-right js-anime-box">
<div class="label js-anime-elm is-anime-scaleRightLeft is-anime-delay4"></div>
<img src="https://picsum.photos/id/369/1200/560" alt="" class="label-img js-anime-elm is-anime-delay8 is-anime-duration1" width="1200" height="560">
</div>
<ul class="list js-anime-box">
<li class="list-elm js-anime-elm is-anime-fadeInUp is-anime-delay1"><img src="https://picsum.photos/id/292/500/400" alt="" width="500" height="400"></li>
<li class="list-elm js-anime-elm is-anime-fadeInUp is-anime-delay3"><img src="https://picsum.photos/id/230/500/400" alt="" width="500" height="400"></li>
<li class="list-elm js-anime-elm is-anime-fadeInUp is-anime-delay5"><img src="https://picsum.photos/id/242/500/400" alt="" width="500" height="400"></li>
</ul>
<p class="js-anime-box js-anime-elm is-anime-scaleUp"><img src="https://picsum.photos/id/1003/500/600" alt="" width="500" height="600"></p>
<p class="js-anime-box js-anime-elm is-anime-scaleDown"><img src="https://picsum.photos/id/433/700/550" alt="" width="700" height="550"></p>
<p class="js-anime-box js-anime-elm is-anime-fadeInLeft"><img src="https://picsum.photos/id/1062/1600/500" alt="" width="1600" height="500"></p>
<p class="js-anime-box js-anime-elm is-anime-fadeInRight"><img src="https://picsum.photos/id/308/1600/500" alt="" width="1600" height="500"></p>
@charset "UTF-8";
/* アニメーション設定 */
.js-anime-elm {
opacity: 0;
animation-fill-mode: both;
animation-duration: 1.2s;
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}
.is-anime {
animation-name: fadeIn;
&.is-anime-fadeInUp {
animation-name: fadeInUp;
}
&.is-anime-fadeInLeft {
animation-name: fadeInLeft;
}
&.is-anime-fadeInRight {
animation-name: fadeInRight;
}
&.is-anime-scaleLeftRight {
animation-name: scaleLeftRight;
}
&.is-anime-scaleRightLeft {
animation-name: scaleRightLeft;
}
&.is-anime-scaleUp {
animation-name: scaleUp;
}
&.is-anime-scaleDown {
animation-name: scaleDown;
}
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes fadeInUp {
0% {
opacity: 0;
transform: translate3d(0,100px,0);
}
100% {
opacity: 1;
transform: translate3d(0,0,0);
}
}
@keyframes fadeInLeft {
0% {
opacity: 0;
transform: translate3d(-30%,0,0);
}
100% {
opacity: 1;
transform: translate3d(0,0,0);
}
}
@keyframes fadeInRight {
0% {
opacity: 0;
transform: translate3d(30%,0,0);
}
100% {
opacity: 1;
transform: translate3d(0,0,0);
}
}
@keyframes scaleLeftRight {
0% {
opacity: 1;
transform: scale(0,1);
transform-origin: left top;
}
50% {
opacity: 1;
transform: scale(1,1);
transform-origin: left top;
}
50.1% {
opacity: 1;
transform: scale(1,1);
transform-origin: right top;
}
100% {
opacity: 1;
transform: scale(0,1);
transform-origin: right top;
}
}
@keyframes scaleRightLeft {
0% {
opacity: 1;
transform: scale(0,1);
transform-origin: right top;
}
50% {
opacity: 1;
transform: scale(1,1);
transform-origin: right top;
}
50.1% {
opacity: 1;
transform: scale(1,1);
transform-origin: left top;
}
100% {
opacity: 1;
transform: scale(0,1);
transform-origin: left top;
}
}
@keyframes scaleUp {
0% {
opacity: 0;
transform: scale(0.5);
}
100% {
opacity: 1;
transform: scale(1);
}
}
@keyframes scaleDown {
0% {
opacity: 0;
transform: scale(1.5);
}
100% {
opacity: 1;
transform: scale(1);
}
}
@for $i from 1 through 10 {
.is-anime-duration#{$i} {
animation-duration:#{$i*0.1}s;
}
}
@for $i from 1 through 10 {
.is-anime-delay#{$i} {
animation-delay:#{$i*0.1}s;
}
}
/* テスト環境style */
body {
margin: 0;
padding: 40vh 0;
text-align: center;
}
h1 {
text-align: center;
margin-bottom: 40vh;
font-size: 40px;
}
img {
height: auto;
max-width: 100%;
vertical-align: top;
}
p {
margin: 200px 0;
}
.label-img-box {
position: relative;
max-width: 900px;
margin: 0 0 200px;
}
.label-img-box-right {
margin-left: auto;
}
.label {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
background: #000;
}
.list {
display: flex;
justify-content: space-between;
list-style: none;
padding: 0 40px;
margin: 0 0 200px;
}
.list-elm {
width: 30%;
}
//Intersection Observerスクロールアニメーション
observerAnime({
rootMargin: '-50%', //画面の中央で発火。IntersectionObserverのrootMargin デフォルトは'0px'
loop: false, //アニメーション繰り返し設定 デフォルトはfalse
threshold: 0, //threshold デフォルトは0
responsive: [{
breakpoint: 1024, //ブレイクポイント max-width
rootMargin: '-30%',
}, {
breakpoint: 400,
rootMargin: '0px',
loop: true, //trueにする場合、rootMarginは'0px'が良さそうです
}]
});
function observerAnime(setOptions) {
//options
const defaultOptions = {
root: null, //IntersectionObserverのroot
rootMargin: '0px', //IntersectionObserverのrootMargin
threshold: 0, //IntersectionObserverのthreshold
loop: false, //アニメーション繰り返し設定
responsive: null, //レスポンシブ設定
/*
//レスポンシブ設定
responsive: [{
breakpoint: 1024, //ブレイクポイント max-width
rootMargin: '0px',
threshold: 0,
loop: false
}, {
breakpoint: 500,
rootMargin: '0px',
threshold: 0,
loop: false
}]
*/
targetAreaClass : '.js-anime-box', //ターゲットを内包するエリアclass
targetClass: '.js-anime-elm', //ターゲットclass
animeSetClass: 'is-anime', //アニメーション実行時に追加するclass
}
//設定をマージ
const options = Object.assign({}, defaultOptions, setOptions);
//ターゲット要素取得
let targets = document.querySelectorAll(options.targetAreaClass);
//要素が存在するかチェック
if(targets.length === 0) {
return false;
}
//各設定を取得
const targetClass = options.targetClass;
const animeSetClass = options.animeSetClass;
options.loopFlg = options.loop;
//observer初期設定
let observer;
setObserver(options);
//breakpoint切り替え監視
let breakpoint = getBreakpoint(options);
//breakpointが設定されていたら処理する
if(breakpoint !== null) {
let nowBreakpoint = breakpoint;
window.addEventListener('resize', function() {
//ブレイクポイントが切り替わったタイミンでobserverをリセット・再定義
breakpoint = getBreakpoint(options);
if(nowBreakpoint !== breakpoint) {
nowBreakpoint = breakpoint;
//observerリセット
targets.forEach(function(target){
observer.unobserve(target);
});
//observer再定義
setObserver(options);
}
});
}
//observer定義関数
function setObserver(options) {
//ブレイクポイントに応じたオプションを取得
const getOptions = getOptionsFunc(options);
options.loopFlg = getOptions[1];
//observer設定
observer = new IntersectionObserver(setAnimeClass, getOptions[0]);
targets.forEach(function(target){
observer.observe(target);
});
}
//アニメーション処理
function setAnimeClass(entries, observer) {
//ループフラグ取得
const loopFlg = options.loopFlg;
entries.forEach(entry => {
//ターゲット要素取得
const target = entry.target;
//ターゲット要素の子要素取得
const children = target.querySelectorAll(targetClass);
//要素が画面に入ると処理
if(entry.isIntersecting) {
//子要素がある場合は子要素にclassを追加
if(children.length !== 0) {
children.forEach(function(child){
child.classList.add(animeSetClass);
});
//子要素がない場合はターゲット自身にclassを追加
} else {
target.classList.add(animeSetClass);
}
//ループなしの場合
if(!loopFlg) {
//要素の監視を解除
observer.unobserve(target);
}
//ループありの場合
} else if(loopFlg) {
//子要素がある場合は子要素に設定されたclassを削除
if(children.length !== 0) {
children.forEach(function(child){
child.classList.remove(animeSetClass);
});
//子要素がない場合はターゲット自身のclassを削除
} else {
target.classList.remove(animeSetClass);
}
}
});
}
//ブレイクポイントごとの設定取得関数
function getBreakpoint(options) {
let responsive = options.responsive;
//ブレイクポイントなしの場合は処理しない
if(!responsive) {
return responsive;
}
//ウィンドウの幅取得
const winWidth = window.innerWidth;
//ウィンドウの幅とブレイクポイントを比較・取得
let reverse = responsive.concat().reverse();
let breakpoint = 'full';
for (let i = 0; i < reverse.length; i++) {
if(winWidth <= reverse[i].breakpoint) {
breakpoint = reverse[i].breakpoint;
break;
}
}
return breakpoint;
}
//オプション取得関数
function getOptionsFunc(options) {
//ベース設定取得
let root = options.root;
let rootMargin = options.rootMargin;
let threshold = options.threshold;
let loop = options.loop;
//レスポンシブ切り替え
const responsive = options.responsive;
//ブレイクポイントありの場合は設定を取得
if(responsive) {
//ウィンドウの幅取得
const winWidth = window.innerWidth;
//ウィンドウの幅とブレイクポイントを比較・各設定取得
for (let i = 0; i < responsive.length; i++) {
if(winWidth <= responsive[i].breakpoint) {
root = responsive[i].root;
rootMargin = responsive[i].rootMargin;
threshold = responsive[i].threshold;
loop = responsive[i].loop;
}
}
}
//IntersectionObserverのオプションとループフラグを返す
return [
{
root: root,
rootMargin: rootMargin,
threshold: threshold
},
loop
];
}
}
Also see: Tab Triggers