I'm trying to zoom children of a container to make it fit the container's height. The container is a flexbox item and may share the available space with other elements, i.e. a heading.
My problem is that I don't know how to examine the visible height of a flex item, minus the height of siblings:
I have created a codepen example here:
In the end, all cards should have the same height (so the first two cards' contents need to shrink even more).
I played around with scrollHeight
, clientHeight
, offsetHeight
, getBoundingClientRect().height
, but didn't succeed.
UPDATE:
Thanks to @Bergi, i found the solution. I have to manually calculate the max. height that is available to the .content
element like so:
const contentHeight = parseFloat(sectionStyles.height) - content.offsetTop;
I somehow assumed that JavaScript would offer an automatically calculated property for this. I updated the CodePen, now it works very well.
I'm trying to zoom children of a container to make it fit the container's height. The container is a flexbox item and may share the available space with other elements, i.e. a heading.
My problem is that I don't know how to examine the visible height of a flex item, minus the height of siblings:
I have created a codepen example here:
https://codepen.io/jmuheim/pen/GgRBMbZ
In the end, all cards should have the same height (so the first two cards' contents need to shrink even more).
I played around with scrollHeight
, clientHeight
, offsetHeight
, getBoundingClientRect().height
, but didn't succeed.
UPDATE:
Thanks to @Bergi, i found the solution. I have to manually calculate the max. height that is available to the .content
element like so:
const contentHeight = parseFloat(sectionStyles.height) - content.offsetTop;
I somehow assumed that JavaScript would offer an automatically calculated property for this. I updated the CodePen, now it works very well.
Share Improve this question edited Mar 26 at 10:15 Joshua Muheim asked Mar 25 at 19:44 Joshua MuheimJoshua Muheim 13.3k9 gold badges86 silver badges165 bronze badges 5 |1 Answer
Reset to default -1These modifications in the javascript file make the contents shrink inside the container.
Section's padding and border widths need to be considered to calculate the available height for the .content element within each section.
Subtract the height of the h1 element from the available height, along with its top and bottom margins.
The element.style.zoom is reset to 1, before each zoom adjustment to prevent zoom effects from previous adjustments.
scrollHeight checks the actual content height, including any content that overflows the visible area as opposed to clientHeight for determining if the content exceeds the available space.
function adjustZoom() { const slides = document.querySelector(".slides"); const sections = slides.querySelectorAll("section"); sections.forEach((section) => { const content = section.querySelector(".content"); const elements = content.children; const sectionStyles = getComputedStyle(section); const sectionPaddingTop = parseFloat(sectionStyles.paddingTop) || 0; const sectionPaddingBottom = parseFloat(sectionStyles.paddingBottom) || 0; const sectionBorderTop = parseFloat(sectionStyles.borderTopWidth) || 0; const sectionBorderBottom = parseFloat(sectionStyles.borderBottomWidth) || 0; let availableHeight = section.clientHeight - sectionPaddingTop - sectionPaddingBottom - sectionBorderTop - sectionBorderBottom; const heading = section.querySelector("h1"); if (heading) { const headingStyles = getComputedStyle(heading); const headingMarginTop = parseFloat(headingStyles.marginTop) || 0; const headingMarginBottom = parseFloat(headingStyles.marginBottom) || 0; availableHeight -= heading.offsetHeight + headingMarginTop + headingMarginBottom; } let zoomLevel = 1; for (let element of elements) { element.style.zoom = 1; } while (content.scrollHeight > availableHeight) { zoomLevel -= 0.05; for (let element of elements) { element.style.zoom = zoomLevel; } } }); } window.onload = adjustZoom; window.onresize = adjustZoom;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744172915a4561620.html
zoom
is a strange way to solve this issue. Take a look atmin-content
. – Spectric Commented Mar 25 at 22:35slides.clientHeight - content.offsetTop
? – Bergi Commented Mar 26 at 0:27