mirror of
https://github.com/frainfreeze/the-monospace-web-pandoc
synced 2025-12-30 02:59:50 -08:00
Fix alignment js logic
This commit is contained in:
13
index.css
13
index.css
@@ -178,10 +178,8 @@ img, video {
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
padding-bottom: calc(
|
padding-bottom: calc(
|
||||||
round(
|
round(
|
||||||
up,
|
nearest,
|
||||||
var(--line-height) - (
|
round(up, var(--real-height), var(--line-height)) - var(--real-height),
|
||||||
var(--real-height) - round(down, var(--real-height), var(--line-height))
|
|
||||||
),
|
|
||||||
1px
|
1px
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -226,6 +224,11 @@ li {
|
|||||||
line-height: 0;
|
line-height: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
::-webkit-scrollbar {
|
||||||
|
height: var(--line-height);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
.tree, .tree ul {
|
.tree, .tree ul {
|
||||||
position: relative;
|
position: relative;
|
||||||
padding-left: 0;
|
padding-left: 0;
|
||||||
@@ -282,6 +285,6 @@ li {
|
|||||||
/*display: none;*/
|
/*display: none;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
body :not(.grid) {
|
.off-grid {
|
||||||
background: rgba(255, 0, 0, 0.1);
|
background: rgba(255, 0, 0, 0.1);
|
||||||
}
|
}
|
||||||
|
|||||||
65
index.js
65
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.
|
// Set the ratio variable on each media.
|
||||||
document.querySelectorAll("img, video").forEach(media => {
|
function setRatios() {
|
||||||
|
const medias = document.querySelectorAll("img, video");
|
||||||
|
for (media of medias) {
|
||||||
function onLoaded() {
|
function onLoaded() {
|
||||||
var ratio = 1;
|
var ratio = defaultRatio;
|
||||||
switch (media.tagName) {
|
switch (media.tagName) {
|
||||||
case "IMG":
|
case "IMG":
|
||||||
ratio = media.naturalWidth / media.naturalHeight;
|
ratio = media.naturalWidth / media.naturalHeight;
|
||||||
break;
|
break;
|
||||||
case "VIDEO":
|
case "VIDEO":
|
||||||
|
if (media.videoWidth != 0 && media.videoHeight != 0) {
|
||||||
ratio = media.videoWidth / media.videoHeight;
|
ratio = media.videoWidth / media.videoHeight;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
console.log("Setting ratio", ratio, "for element", media);
|
||||||
media.style.setProperty("--ratio", ratio);
|
media.style.setProperty("--ratio", ratio);
|
||||||
}
|
}
|
||||||
switch (media.tagName) {
|
switch (media.tagName) {
|
||||||
case "IMG":
|
case "IMG":
|
||||||
if (media.loaded) {
|
if (!media.loaded) {
|
||||||
onLoaded();
|
media.style.setProperty("--ratio", defaultRatio); // default while loading
|
||||||
} else {
|
firstEvent(media, "load").then(onLoaded);
|
||||||
media.addEventListener("load", onLoaded);
|
console.log("Image loaded", media);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "VIDEO":
|
case "VIDEO":
|
||||||
if (media.readystate == 4) {
|
if (media.readyState != 4) {
|
||||||
onLoaded();
|
media.style.setProperty("--ratio", defaultRatio); // default while loading
|
||||||
} else {
|
firstEvent(media, "loadeddata").then(onLoaded);
|
||||||
media.style.setProperty("--ratio", 16/9); // default while loading
|
console.log("Video ready", media);
|
||||||
media.addEventListener("loadedmetadata", onLoaded);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
onLoaded();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user