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:
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.
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.