From 2d83cb9f8f6358a5f6deabdccc93eab8d52a7f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Wickstr=C3=B6m?= Date: Sun, 25 Aug 2024 10:22:49 +0200 Subject: [PATCH] Fix alignment js logic --- index.css | 13 +++++--- index.js | 97 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 77 insertions(+), 33 deletions(-) diff --git a/index.css b/index.css index b9d15ea..7b34a90 100644 --- a/index.css +++ b/index.css @@ -178,10 +178,8 @@ img, video { width: 100%; padding-bottom: calc( round( - up, - var(--line-height) - ( - var(--real-height) - round(down, var(--real-height), var(--line-height)) - ), + nearest, + round(up, var(--real-height), var(--line-height)) - var(--real-height), 1px ) ); @@ -226,6 +224,11 @@ li { line-height: 0; } +::-webkit-scrollbar { + height: var(--line-height); +} + + .tree, .tree ul { position: relative; padding-left: 0; @@ -282,6 +285,6 @@ li { /*display: none;*/ } -body :not(.grid) { +.off-grid { background: rgba(255, 0, 0, 0.1); } diff --git a/index.js b/index.js index f0112ef..b3c4bb1 100644 --- a/index.js +++ b/index.js @@ -1,32 +1,73 @@ +function firstEvent(element, name) { + return new Promise(resolve => { + element.addEventListener(name, resolve); + }); +} + +const defaultRatio = 16/9; + // Set the ratio variable on each media. -document.querySelectorAll("img, video").forEach(media => { - function onLoaded() { - var ratio = 1; - switch (media.tagName) { - case "IMG": - ratio = media.naturalWidth / media.naturalHeight; - break; - case "VIDEO": - ratio = media.videoWidth / media.videoHeight; - break; +function setRatios() { + const medias = document.querySelectorAll("img, video"); + for (media of medias) { + function onLoaded() { + var ratio = defaultRatio; + switch (media.tagName) { + case "IMG": + ratio = media.naturalWidth / media.naturalHeight; + break; + case "VIDEO": + if (media.videoWidth != 0 && media.videoHeight != 0) { + ratio = media.videoWidth / media.videoHeight; + } + break; + } + console.log("Setting ratio", ratio, "for element", media); + media.style.setProperty("--ratio", ratio); } - media.style.setProperty("--ratio", ratio); + switch (media.tagName) { + case "IMG": + if (!media.loaded) { + media.style.setProperty("--ratio", defaultRatio); // default while loading + firstEvent(media, "load").then(onLoaded); + console.log("Image loaded", media); + } + break; + case "VIDEO": + if (media.readyState != 4) { + media.style.setProperty("--ratio", defaultRatio); // default while loading + firstEvent(media, "loadeddata").then(onLoaded); + console.log("Video ready", media); + } + break; + } + onLoaded(); } - switch (media.tagName) { - case "IMG": - if (media.loaded) { - onLoaded(); - } else { - media.addEventListener("load", onLoaded); - } - break; - case "VIDEO": - if (media.readystate == 4) { - onLoaded(); - } else { - media.style.setProperty("--ratio", 16/9); // default while loading - media.addEventListener("loadedmetadata", onLoaded); - } - break; +} + +setRatios(); + +function checkOffsets() { + const targetNode = document.body; + const config = { childList: true, subtree: true }; + + const callback = () => { + const elements = document.querySelectorAll("body *"); + for (const element of elements) { + const offset = element.offsetTop % 10; + if(element.offsetParent == document.body && offset > 0) { + element.classList.add("off-grid"); + console.error("Incorrect vertical offset:", element, element.offsetTop % 20); + } else { + element.classList.remove("off-grid"); + } } -}); + }; + + callback(); + + const observer = new MutationObserver(callback); + observer.observe(targetNode, config); +} + +window.addEventListener("load", checkOffsets);