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

              
                <div id="root">
  <p>scroll to see the target</p>
  <div class="buffer"></div>
  <div id="target"></div>
  <div class="buffer"></div>
</div>

<div id="output">position: <pre></pre></div>

<div id="buttons">
  <button id="targetSize">toggle target size</button>
  <button id="rootSize">toggle root size</button>
</div>
              
            
!

CSS

              
                html {
  box-sizing: border-box;
  font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
}
*, *:before, *:after {
  box-sizing: inherit;
}
html,
body {
  background-color: #1d1e22;
  margin: 0;
  padding: 0;
}

#root {
  background-color: #FFF;
  border: 3px solid rebeccapurple;
  height: 400px;
  margin: 20px auto;
  overflow: scroll;
  transition: 0.25s;
  width: 400px;
}
#root.large {
  height: 600px;
}
#root p {
  text-align: center;
}

#target {
  background-color: rebeccapurple;
  background-image: radial-gradient(ellipse at center, #ffffff 0%,#ffffff 14%,#663399 15%,#663399 34%,#ffffff 35%,#ffffff 50%,#663399 51%,#663399 99%);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 300px 300px;
  height: 500px;
  margin: auto;
  pointer-events: none;
  transition: 0.25s;
  width: 300px;
}
#target.small {
  background-size: 200px;
  height: 200px;
}

#output {
  color: #FFF;
  margin: 20px auto;
  width: 400px;
}
#output pre {
  display: inline;
  font-size: 16px;
  padding: 0 10px;
}

#buttons {
  display: flex;
  justify-content: space-around;
  margin: 20px auto;
  position: relative;
  width: 400px;
  z-index: 2;
}
#buttons button {
  background-color: #FFF;
  border: 3px solid rebeccapurple;
  color: rebeccapurple;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  padding: 0.5rem 1rem;
  transition: 0.25s 0.25s;
}

.buffer {
  height: 600px;
}
              
            
!

JS

              
                const root = document.querySelector('#root');
const rootSize = document.querySelector('#rootSize');
const target = document.querySelector('#target');
const targetSize = document.querySelector('#targetSize');
const output = document.querySelector('#output pre');
const io_options = {
  root: root,
  rootMargin: '0px',
  threshold: [...Array(100).keys()].map(x => x / 100)
};
let io_observer;

function io_callback (entries) {
  const ratio = entries[0].intersectionRatio;
  const boundingRect = entries[0].boundingClientRect;
  const intersectionRect = entries[0].intersectionRect;

  if (ratio === 0) {
    output.innerText = 'outside';
  } else if (ratio < 1) {
    if (boundingRect.top < intersectionRect.top) {
      output.innerText = 'on the top';
    } else {
      output.innerText = 'on the bottom';
    }
  } else {
    output.innerText = 'inside';
  }
}

rootSize.addEventListener('click', function () {
  root.classList.toggle('large');
});

targetSize.addEventListener('click', function () {
  target.classList.toggle('small');
});

io_observer = new IntersectionObserver(io_callback, io_options);
io_observer.observe(target);
              
            
!
999px

Console