Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <body>
    <div class="child"></div>
    <div id="margin">
        <div></div>
    </div>
    <!-- https://github.com/w3c/IntersectionObserver/tree/master/polyfill -->
    <script src="main.js"></script>
</body>
              
            
!

CSS

              
                
.child {
  position: absolute;
  top: 150vh;
  background-color: red;
  width: 100px;
  height: 100px;
}
.child.inview {
	background-color: green;
  }

.child:nth-child(2) {
  left: 110px;
}

body {
  width: 100%;
  height: 300vh;
  background-color: #8f8f8f;
}

#margin {
  padding: 10% 0;
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}

#margin > div {
  background-color: rgba(255, 255, 255, 0.5);
  width: 100%;
  height: 100%;
}
              
            
!

JS

              
                const child = document.querySelector('.child');

//動きの詳細を決める
const cb = function(entries, observer){
	entries.forEach(entry => {
		//渡ってきた要素が画面内に入ってきた場合
		if(entry.isIntersecting) {
			entry.target.classList.add('inview');
			//監視解除について記述もできる
			//observer.unobserve(entry.target);
		} else {
			//画面外へ出た場合
			entry.target.classList.remove('inview');
		}

	});
}
//オプション
const option = {
	//詳細な交差設定
	root: null,
	//要素の交差のオプションをmarginでつける(0もpxがいる)
	rootMargin: "-40% 0px -50% 0px",
	//進行方向の交差のオプション(進行方向と要素の先頭が0,後ろが1
	threhold: 0
}
//ioはインスタンスされたオブジェクト。コールバック関数を渡されている。
const io = new IntersectionObserver(cb, option);
//監視したい対象のDOM
io.observe(child);
              
            
!
999px

Console