gpt4 book ai didi

javascript - 有没有办法直接访问 CSS 网格中自动放置元素的实际网格坐标?

转载 作者:行者123 更新时间:2023-12-05 00:24:45 25 4
gpt4 key购买 nike

有人问过类似的问题 here但只有当页面上的每个元素都是自动放置并且只占用一列时,答案才有效。
我有一个 12 列的网格布局,其中前三行都被一个跨越所有十二列的元素占据;然后我有三行自动放置的元素,每行占据六列。
虽然我可以根据之前的答案进行数学计算,但认为计算出的元素位置没有存储在 Javascript 可访问的任何地方似乎有点荒谬。
如果我访问计算出的关于网格的 CSS 属性,我只会得到 autospan 6 ,而不是 row-start 的实际计算数或 row-end .真的没有办法访问 grid-row 的计算值吗?和 grid-column ?

最佳答案

我找不到直接检索网格坐标的方法。
这是一个获取网格内元素的网格坐标的函数。它适用于占用超过 1 列或超过 1 行的元素。

function getItemGridCoords(element) {

function digitsAndSpacesOnly(string) {
return string.replace(/[^\d\s.]/g, '');
}

function parseTemplate(gridTemplate) {
// parses a CSS grid-template-[row|column] to Numbers and does cumulative summation
// "100px 100px 100px" → [100,100,100] → [100,200,300]

return digitsAndSpacesOnly(gridTemplate).split(/\s/gmi).map(Number).reduce((array,currentValue) => {
array.push(currentValue + array[array.length-1]);
return array;
},[0]);
}

function findIndexOfClosest(array,target) {
return array.reduce((prev,curr,indexCurr) => {
return (Math.abs(curr - target) < Math.abs(array[prev] - target) ? indexCurr : prev);
});
}

const grid = element.parentElement;
const computedStyles = getComputedStyle(grid);
const getGapsAdder = gap => (n,i) => n + i * gap;
const colsPositions = parseTemplate(computedStyles['grid-template-columns']).map(getGapsAdder(digitsAndSpacesOnly(computedStyles['column-gap'])));
const rowsPositions = parseTemplate(computedStyles['grid-template-rows']).map(getGapsAdder(digitsAndSpacesOnly(computedStyles['row-gap'])));

const bounds = element.getBoundingClientRect();
const gridBounds = grid.getBoundingClientRect();

return {
col : findIndexOfClosest(colsPositions, bounds.left - gridBounds.left),
row : findIndexOfClosest(rowsPositions, bounds.top - gridBounds.top),
};
}





for (let element of document.querySelectorAll('.grid > *')) {
element.innerHTML = JSON.stringify(getItemGridCoords(element));
}
.grid {
font-family: monospace;
display: grid;
grid : auto-flow dense / repeat(12,1fr);
gap: 0.5em;
}

.grid > * {
white-space: nowrap;
background: aliceblue;
min-height: 2.5em;
}

.whole {
grid-column: auto / span 12;
}
.two-thirds {
grid-column: auto / span 8;
}
.half {
grid-column: auto / span 6;
}
.third {
grid-column: auto / span 4;
}

.skyscraper {
grid-row: auto / span 3;
}
<div class='grid'>
<span class='whole'></span>
<span class='half'></span>
<span class='half'></span>
<span class='half'></span>
<span class='half'></span>
<span class='half'></span>
<span class='half'></span>
</div>

<hr>

<div class='grid'>
<span class='third'></span>
<span class='third'></span>
<span class='third'></span>
<span class='half'></span>
<span class='half'></span>
<span class='half'></span>
<span class='half'></span>
</div>

<hr>

<div class='grid'>
<span class='third skyscraper'></span>
<span class='third'></span>
<span class='third'></span>
<span class='two-thirds'></span>
<span class='third'></span>
<span class='third'></span>
<span class='half'></span>
<span class='half'></span>
</div>

关于javascript - 有没有办法直接访问 CSS 网格中自动放置元素的实际网格坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55108800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com