implemented simple grid api
parent
81a60ecb09
commit
93bb0d461d
@ -0,0 +1,3 @@
|
|||||||
|
function loadTSTarget(name) {
|
||||||
|
require(`../../util/${name}`);
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
.grid3{
|
||||||
|
border: 1px solid red;
|
||||||
|
display: inline-block;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.grid-item{
|
||||||
|
position: absolute;
|
||||||
|
z-index: 3;
|
||||||
|
}
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
function arrangeGrids() {
|
||||||
|
let $ = require('jquery');
|
||||||
|
let grids = $(".grid3");
|
||||||
|
for (let i = 0, l = grids.length; i < l; i++) {
|
||||||
|
let grid = grids[i];
|
||||||
|
arrangeGrid(grid, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function arrangeGrid(grid: HTMLElement, width: number) {
|
||||||
|
let children = grid.children;
|
||||||
|
for (let i = 0, l = children.length; i < l; i++) {
|
||||||
|
if (0 === i) {
|
||||||
|
placeZero(children[i] as HTMLElement);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let sibling = children[i - 1];
|
||||||
|
let child = children[i];
|
||||||
|
|
||||||
|
if (0 === (i % width)) {
|
||||||
|
let rowBegin = children[i - width];
|
||||||
|
placeUnder(rowBegin as HTMLElement, child as HTMLElement, 15);
|
||||||
|
} else {
|
||||||
|
placeRight(sibling as HTMLElement, child as HTMLElement, 15);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function placeZero(anchor: HTMLElement) {
|
||||||
|
let posX = anchor.offsetLeft;
|
||||||
|
let posY = anchor.offsetTop;
|
||||||
|
anchor.style.left = `${posX}px`
|
||||||
|
anchor.style.top = `${posY}px`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function placeRight(anchor: HTMLElement, satellite: HTMLElement, spacing: number) {
|
||||||
|
let posX = anchor.offsetLeft;
|
||||||
|
let posY = anchor.offsetTop;
|
||||||
|
let finalX = posX + anchor.clientWidth + spacing;
|
||||||
|
let finalY = posY;
|
||||||
|
|
||||||
|
satellite.style.left = `${finalX}px`
|
||||||
|
satellite.style.top = `${finalY}px`;
|
||||||
|
|
||||||
|
let childX = finalX + satellite.clientWidth - satellite.parentElement.offsetLeft;
|
||||||
|
if (childX > satellite.parentElement.clientWidth) {
|
||||||
|
console.log(`setting parent width to ${childX}`)
|
||||||
|
satellite.parentElement.style.width = `${childX}px`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function placeUnder(anchor: HTMLElement, satellite: HTMLElement, spacing: number) {
|
||||||
|
let posX = anchor.offsetLeft;
|
||||||
|
let posY = anchor.offsetTop;
|
||||||
|
|
||||||
|
let finalX = posX;
|
||||||
|
let finalY = posY + anchor.clientHeight + spacing;
|
||||||
|
|
||||||
|
satellite.style.left = `${finalX}px`
|
||||||
|
satellite.style.top = `${finalY}px`;
|
||||||
|
|
||||||
|
let childY = finalY + satellite.clientHeight - satellite.parentElement.offsetTop;
|
||||||
|
if (childY > satellite.parentElement.clientHeight) {
|
||||||
|
console.log(`setting parent height to ${childY}`)
|
||||||
|
satellite.parentElement.style.height = `${childY}px`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
arrangeGrids();
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
export module HTMLUtil {
|
||||||
|
export function getElementXinParent(elem: HTMLElement, parent: HTMLElement): number {
|
||||||
|
let styleLeft = elem.style.left;
|
||||||
|
if (0 === styleLeft.length) {
|
||||||
|
return parent.offsetLeft;
|
||||||
|
} else {
|
||||||
|
return parent.offsetLeft+parseInt(styleLeft);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getElementYInParent(elem: HTMLElement, parent: HTMLElement): number {
|
||||||
|
let styleTop = elem.style.top;
|
||||||
|
if (0 === styleTop.length) {
|
||||||
|
return parent.offsetTop;
|
||||||
|
} else {
|
||||||
|
return parent.offsetTop + parseInt(styleTop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue