Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                @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%;
}
              
            
!

JS

              
                //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
		];
	}
}
              
            
!
999px

Console