cookbook home ~ main site ~ links ~ podcast ~ mastodon

Change Content Based On Basic Scroll Position

Details

EXAMPLE

item1
  1. The curtain rose and the show was on. The birch canoe slid on the smooth planks. Glue the sheet to the dark blue background. It's easy to tell the depth of a well.
  2. Rice is often served in round bowls. The box was thrown beside the parked truck. The source of the huge river is the clear spring.
  3. Jazz and swing fans like fast music. The map had an X that meant nothing. Some ads serve to cheat buyers. On the islands the sea breeze is soft and mild. Add salt before you fry the egg. The houses are built of red clay bricks.
  4. A castle built from sand fails to endure. Tack the strip of carpet to the worn floor. Pour the stew from the pot into the plate.
  5. The drip of the rain made a pleasant sound. Smoke poured out of every crack. Much of the story makes good sense.
  6. Drop the ashes on the worn old rug. The desk and both chairs were painted tan. Throw out the used paper cup and plate.




HTML Source

<div id="currentId">item1</div>

<div id="scrollArea">
  <ol>
    <li id="item1">
      The curtain rose and the show was on. The birch canoe slid on the smooth
      planks. Glue the sheet to the dark blue background. It's easy to tell the
      depth of a well.
    </li>
    <li id="item2">
      Rice is often served in round bowls. The box was thrown beside the parked
      truck. The source of the huge river is the clear spring.
    </li>
    <li id="item3">
      Jazz and swing fans like fast music. The map had an X that meant nothing.
      Some ads serve to cheat buyers. On the islands the sea breeze is soft and
      mild. Add salt before you fry the egg. The houses are built of red clay
      bricks.
    </li>
    <li id="item4">
      A castle built from sand fails to endure. Tack the strip of carpet to the
      worn floor. Pour the stew from the pot into the plate.
    </li>
    <li id="item5">
      The drip of the rain made a pleasant sound. Smoke poured out of every
      crack. Much of the story makes good sense.
    </li>
    <li id="item6">
      Drop the ashes on the worn old rug. The desk and both chairs were painted
      tan. Throw out the used paper cup and plate.
    </li>
  </ol>
</div>

CSS Source

:root {
  --area-height: 18rem;
  --top-padding: 1rem;
}

#scrollArea {
  border: 1px solid black;
  height: var(--area-height);
  overflow-y: auto;
  width: 22rem;
  padding-top: var(--top-padding);
}

#scrollArea li {
  padding-bottom: 4rem;
  margin: 0;
}

#scrollArea li:last-child {
  height: calc(var(--area-height) - var(--top-padding));
}

JavaScript Source

function handleIntersect(entries, observer) {
  entries.forEach((entry) => {
    if (
      entry.boundingClientRect.top < entry.rootBounds.top &&
      entry.boundingClientRect.bottom > entry.rootBounds.top
    ) {
      window.currentId.innerHTML = entry.target.id
    } else if (
      entry.boundingClientRect.top < entry.rootBounds.top &&
      entry.boundingClientRect.bottom < entry.rootBounds.top
    ) {
      window.currentId.innerHTML = entry.target.nextElementSibling.id
    }
  })
}

function createObserver() {
  const options = {
    root: window.scrollArea,
    rootMargin: '0px',
    threshold: 0,
  }
  const observer = new IntersectionObserver(handleIntersect, options)
  const els = document.getElementsByTagName('li')
  for (let i = 0; i < els.length; i++) {
    observer.observe(els[i])
  }
}

window.addEventListener('load', createObserver)

JSON Data Source

{
  "note": "Example JSON data file"
}

Config JS Source

// This file can be used to represent
// and data that should be held outside
// of the main script

const c = {
  alfa: 'bravo',
}

Notes