JavaScript :
- () /
- /
: getBoundingClientRect
elem.getBoundingClientRect() elem :
topY-,leftX-,rightX-,bottomY-.
:
:
:
-
style.position.left/top elemelem.getBoundingClientRect().top- Chrome
getBoundingClientRectwidthheight:height=bottom-top,width=right-left
CSSposition:fixed
CSS right bottom
JavaScript
elementFromPoint(x, y)
document.elementFromPoint(x, y) (x, y)
:
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)
/null
:
let elem = document.elementFromPoint(x, y);
// elem = null
elem.style.background = ''; // !
position:fixed
CSS left/top ( right/bottom) position:fixed
getBoundingClientRect
createMessageUnder(elem, html) elem :
let elem = document.getElementById("coords-show-mark");
function createMessageUnder(elem, html) {
//
let message = document.createElement('div');
// CSS
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;
}
// :
//
let message = createMessageUnder(elem, 'Hello, world!');
document.body.append(message);
setTimeout(() => message.remove(), 5000);
:
id=coords-show-mark ,
CSS
:
: position:fixed
position:absolute
CSS position:fixed position:absolute
position:absolute top/left
(clientX,clientY) (pageX,pageY)
:
(clientX,clientY) (pageX,pageY)
:
-
"From today's featured article"clientY0 clientX-
pageXpageY
:
pageY=clientY+pageX=clientX+
getCoords(elem) elem.getBoundingClientRect() :
//
function getCoords(elem) {
let box = elem.getBoundingClientRect();
return {
top: box.top + pageYOffset,
left: box.left + pageXOffset
};
}
:
-
elem.getBoundingClientRect() -
elem.getBoundingClientRect()+
position:fixed position:absolute
CSS position absolute fixed
<code><pre>10(plnkr, JSBin, codepen)