I would like to implement a grid layout that meets the following requirements, but I don't know how to do it, so I would like to ask you a question.
The layout can be changed depending on the number of items as shown below, but if there are more than 9 items, the order of arrangement of items will be from top to bottom.Is there any way to solve that?
private getKeysStyle(items:string){
if(items.length>=9){
US>return{
marginTop: '8px',
display: 'grid',
gridGap: '6px',
gridAutoFlow: 'column',
gridTemplateColumns: 'repeat(auto-fill, minmax(40px, 1fr))',
gridTemplateRows: 'repeat(4,1fr)',
};
}
US>return{
marginTop: '8px',
display: 'grid',
gridGap: '6px',
gridTemplateColumns: 'repeat(2,1fr)',
}
}
Is it because you specify the grid-auto-flow
property?If you roughly modify it to fit the image, you will see the following:
function*range(a,b){
for(leti=a;i<=b;i++){
yieldi;
}
}
function getKeyStyle(items){
if(items.length>=19){
US>return{
marginTop: '8px',
display: 'grid',
gridGap: '6px',
gridTemplateColumns: 'repeat(5,minmax(40px,1fr))',
};
} else if(items.length>=9){
US>return{
marginTop: '8px',
display: 'grid',
gridGap: '6px',
gridTemplateColumns: 'repeat(3,minmax(40px,1fr))',
};
}
US>return{
marginTop: '8px',
display: 'grid',
gridGap: '6px',
gridTemplateColumns: 'repeat(2,1fr)',
}
}
constelmNum=9;
[...range(1,elmNum)] .forEach(e=>{
const div = document.createElement("div");
const divTxt= document.createTextNode(e);
div.appendChild(divTxt);
document.body.appendChild(div);
});
const divs=[... document.querySelectorAll("body>div")];
const properties = Object.entries (getKeyStyle(divs));
for ([property, value] of properties) {
document.body.style [property] = value;
}
© 2024 OneMinuteCode. All rights reserved.