window.addEventListener('load', handler, false);
window.addEventListener('scroll', handler, false);
let exOne = document.getElementById('example1');
let exTwo = document.getElementById('example2');
let exThree = document.getElementById('example3');
function handler(event) {
function getOffset(el) {
const rect = el.getBoundingClientRect();
return {
left: rect.left + window.scrollX,
top: rect.top + window.scrollY
};
}
exTwo.style.top = getOffset(exOne).top + "px";
exTwo.style.left = getOffset(exOne).left + "px";
exTwo.style.height = exOne.offsetHeight + "px";
exTwo.style.width = exOne.offsetWidth + "px";
exTwo.style.display = "block";
exThree.style.top = getOffset(exOne).top + "px";
exThree.style.left = getOffset(exOne).left + "px";
exThree.style.height = exOne.offsetHeight / 1.75 + "px";
exThree.style.width = exOne.offsetWidth + "px";
exThree.style.display = "block";
}
#example1 {
background-color: transparent;
padding: 10px;
}
#example1 p,
#example1 h1 {
z-index: 10;
position: relative;
}
#example2 {
background-color: transparent;
background: linear-gradient(to top, #0FF 51%, transparent 25%), linear-gradient(to left, #0FF 51%, transparent 25%);
position: absolute;
z-index: 1;
/* display none to remove any glitches shown before load
add back to block in JS after load*/
display: none;
}
#example3 {
background-color: transparent;
background: linear-gradient(to bottom, #eceddc 51%, transparent 25%), linear-gradient(to right, #eceddc 51%, transparent 25%);
position: absolute;
z-index: 1;
/* display none to remove any glitches shown before load
add back to block in JS after load*/
display: none;
}
Scroll down
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="example1">
<div id="example2"></div>
<div id="example3"></div>
<h1>Lorem Ipsum Dolor</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
</div>