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>position: sticky; <br>Stacking elements within a specific area Vanilla JS</h1>

<section class="c-lead">
  <div class="c-lead__sticky-body js-stacking__area">
    <p class="c-lead__sticky js-stacking__item">
      吾輩は猫である。名前はまだ無い。<br>
      どこで生れたかとんと見当がつかぬ。
    </p>
    <div style="height: 50vh;"></div>

    <p class="c-lead__sticky js-stacking__item">
      何でも薄暗いじめじめした所でニャーニャー泣いていた事だけは記憶している。<br>
      吾輩はここで始めて人間というものを見た。
    </p>
    <div style="height: 50vh;"></div>

    <p class="c-lead__sticky js-stacking__item">
      しかもあとで聞くとそれは書生という人間中で<br>
      一番獰悪な種族であったそうだ。<br>
      この書生というのは時々我々を捕えて煮て食うという話である。
    </p>
    <div style="height: 50vh;"></div>
  </div>
</section>
              
            
!

CSS

              
                .js-stacking__item {
  position: sticky;
  top: 4em;
}



/* テストページ設定 */
body {
  margin: 0;
  padding: 100px 0 200vh;
}
h1 {
  text-align: center;
  margin-bottom: 250px;
  font-size: 30px;
}
.c-lead {
  margin-bottom: 50vh;
  background-color: #f0f0f0;
}
.c-lead__sticky-body {
  max-width: 960px;
  padding: 160px 50px 80px;
  margin: 0 auto;
  line-height: 2;
  font-size: 20px;
}
.c-lead__sticky {
  margin: 0;
}
              
            
!

JS

              
                //position: sticky;で指定エリア内に要素を追従させ積み重ねる関数
const areaStacking = () => {
	//エリアチェック
	const areas = document.querySelectorAll('.js-stacking__area');
	if(areas.length === 0) {
		return;
	}

	//追従チェック関数
	const checkPosition = targets => {
		//ブロックごとの高さを取得
		const height = [];
		targets.forEach(target => {
			target.style.paddingTop = '';
			target.style.paddingBottom = '';
			height.push(target.offsetHeight);
		});
		const heightLen = height.length;

		//高さを設定
		targets.forEach((target, index) => {
			//ブロックのtop位置設定
			let topHeight = 0;
			if(index === 1) {
				topHeight = height[0];

			} else if(index > 1) {
				topHeight = height.slice(0,index);
				topHeight = topHeight.reduce((a, x) => a + x);
			}
			target.style.paddingTop = topHeight + 'px';

			//ブロックのbottom位置設定
			let bottomHeight = 0;
			if(index !== heightLen - 1) {
				bottomHeight = height.slice(index+1,heightLen);
				bottomHeight = bottomHeight.reduce((a, x) => a + x);
				target.style.paddingBottom = bottomHeight + 'px';
			}
		});
	}

	areas.forEach(area => {
		//エリア内に追従要素が存在する場合のみ処理する
		const targets = area.querySelectorAll('.js-stacking__item');
		if(targets.length > 0) {
			checkPosition(targets);

			/*
				レスポンシブ対応
			*/
			//表示エリアリサイズ監視 ResizeObserver
			const resizeObserver = new ResizeObserver(() => {
				checkPosition(targets);
			});
			//リサイズ監視開始
			resizeObserver.observe(area);
		}
	});

};
areaStacking();
              
            
!
999px

Console