Javascript for image-aware scrolling

iljitsch

Ars Tribunus Angusticlavius
9,637
Subscriptor++
Hi all,

There are huge problems in the world, and tiny ones. I couldn't fix a huge one today, but with a couple dozen lines of Javascript I managed to fix a small one: when you scroll a web page, you end up with an image that's only partially in view. Then you scroll again, and then the part that was previously visible scrolls off the page, and you only see the part that wasn't visible before. So you if you want to see the whole image, you need to do some precision scrolling rather than simply bang the spacebar.

Including the following code on a page fixes this:

Code:
<script language="javascript">

lastkeywasnav = 1;
window.addEventListener('keydown', scrolltoimg);

function scrolltoimg()
{
  key = event.code;
  if (lastkeywasnav && key == "Space")
    { 
      for (i = 0; i < document.images.length; i++)
        { 
          if (document.images[i].getBoundingClientRect().top < window.innerHeight &&
              document.images[i].getBoundingClientRect().bottom > window.innerHeight)
            { 
              document.images[i].scrollIntoView({ behavior: "smooth", block: "center" });
              event.preventDefault();
              break;
            }
        }
    }
  lastkeywasnav = (key == "Space" || key == "PageUp" || key == "PageDown" ||
                   key == "ArrowLeft" || key == "ArrowRight" ||
                   key == "ArroUp" || key == "ArroDown");
}
</script>

When the user hits the spacebar, the script looks if there's an image where the top is in view, but the bottom of the image is not in view. If it finds such an image, it scrolls that to the middle of the window. If not, the normal spacebar action happens (which should be scrolling by a page length).

Limitation: the scroll speed. On Chrome, it's slower than normal. On Safari, it's faster.

If you want to see this in action, have a look at my photos of the SHA2017 event. Pagedown will scroll an entire page, often slicing photos in half, while space will use the smart no-image-slicing scroll technique.

Details:

The script intercepts all keyboard presses. It only does something if the key pressed is space, and then only if the previous key was a navigation key. This is some simple protection against the spacebar not working in a text entry field. But you probably don't want to use this on a page with routine text entry, though. The script cycles over all images and sees if the top part is in view but the bottom part isn't. If that's the case, it scrolls the image into view and cancels the key event's default action, otherwise there would be additional scrolling.
 

Cranioclast

Ars Legatus Legionis
11,026
Subscriptor++
FYI, this can basically be done with just CSS in modern browsers.

Code:
.container {
  scroll-snap-type: y mandatory;
}

.child {
  scroll-snap-align: center;
}

Arrow keys and space bar should both just advance you to the next child element and center it vertically inside the container.

https://developer.mozilla.org/en-US/doc ... -snap-type
 

iljitsch

Ars Tribunus Angusticlavius
9,637
Subscriptor++
Oh, that's interesting, I didn't know about CSS scroll-snap.

However, it doesn't do what I want consistently: see here. So it still often leaves photos cut in half by "the fold". Interestingly, it works with different ways to initiate scrolling, including flicking a touch screen. With the mouse scroll wheel it works really strange: after scrolling a bit and waiting a fraction of a second, the browser decides I didn't scroll properly so it scrolls some more, or even scrolls back a bit.

Compare to the version with my Javascript code.