JavaScript
-
position:fixed/-
clientX/clientY
-
- document root
position:absolute/-
pageX/pageY
-
pageYclientY
getBoundingClientRect
elem.getBoundingClientRect() elem DOMRect
DOMRect
x/yX/Ywidth/heightwidth/height
derived
top/bottom/ Yleft/right/ X
y/top/bottom
elem.getBoundingClientRect()
x/y width/height derived
left = xtop = yright = x + widthbottom = y + height
-
10.5style.left/top -
elemelem.getBoundingClientRect().top
x/y top/left (x,y) (width,height)
width/height directed
width/height
width height width=-200height=-100
left/top x/y
elem.getBoundingClientRect() width/height width/height
x/yIE x/y
polyfill DomRect.prototype getter top/left width/height x/y elem.getBoundingClientRect()
window CSS position:fixed
CSS right bottom
JavaScript
elementFromPoint(x, y)
document.elementFromPoint(x, y) (x, y) the most nested
let elem = document.elementFromPoint(x, y);
let centerX = document.documentElement.clientWidth / 2;
let centerY = document.documentElement.clientHeight / 2;
let elem = document.elementFromPoint(centerX, centerY);
elem.style.background = "red";
alert(elem.tagName);
elementFromPoint null document.elementFromPoint(x,y) (x,y)
width/height null
let elem = document.elementFromPoint(x, y);
// elem = null
elem.style.background = ''; // Error!
fixed
getBoundingClientRect CSS position left/top right/bottom
createMessageUnder(elem, html) elem
let elem = document.getElementById("coords-show-mark");
function createMessageUnder(elem, html) {
// message
let message = document.createElement('div');
// CSS class
message.style.cssText = "position:fixed; color: red";
// "px"
let coords = elem.getBoundingClientRect();
message.style.left = coords.left + "px";
message.style.top = coords.bottom + "px";
message.innerHTML = html;
return message;
}
//
// message 5
let message = createMessageUnder(elem, 'Hello, world!');
document.body.append(message);
setTimeout(() => message.remove(), 5000);
Button with id=coords-show-mark, the message will appear under it
CSS
message position:fixed
document position:absolute
CSS position:fixed position:absolute
position:absolute top/left
pageY=clientY+pageX=clientX+
getCoords(elem) elem.getBoundingClientRect()
//
function getCoords(elem) {
let box = elem.getBoundingClientRect();
return {
top: box.top + window.pageYOffset,
right: box.right + window.pageXOffset,
bottom: box.bottom + window.pageYOffset,
left: box.left + window.pageXOffset
};
}
position:absolute
createMessageUnder
function createMessageUnder(elem, html) {
let message = document.createElement('div');
message.style.cssText = "position:absolute; color: red";
let coords = getCoords(elem);
message.style.left = coords.left + "px";
message.style.top = coords.bottom + "px";
message.innerHTML = html;
return message;
}
-
elem.getBoundingClientRect() -
elem.getBoundingClientRect()
position:fixed position:absolute
CSS position absolute fixed
<code><pre>10 plnkrJSBincodepen