Skip to content

Instantly share code, notes, and snippets.

@VacantThinker
Created April 30, 2023 06:21
Show Gist options
  • Select an option

  • Save VacantThinker/ca10ee69af7e5e9ecdc3aa17921638ec to your computer and use it in GitHub Desktop.

Select an option

Save VacantThinker/ca10ee69af7e5e9ecdc3aa17921638ec to your computer and use it in GitHub Desktop.
document.querySelector('head')
.insertAdjacentHTML('beforeend',
`<style>
#highlighter-top, #highlighter-bottom {
background: red;
height:1px;
position: fixed;
transition:all 300ms ease;
}
#highlighter-left, #highlighter-right {
background: red;
width:1px;
position: fixed;
transition:all 300ms ease;
}
</style>`,
);
document.querySelector('body')
.insertAdjacentHTML('beforeend',
`<div id="highlighter">
<div id="highlighter-top"></div>
<div id="highlighter-left"></div>
<div id="highlighter-right"></div>
<div id="highlighter-bottom"></div>
</div>`,
);
/**
*
* @param obj {Object}
*/
function applyHighlighterCss(obj) {
let highLightElements = {
top: document.querySelector('#highlighter-top'),
left: document.querySelector('#highlighter-left'),
right: document.querySelector('#highlighter-right'),
bottom: document.querySelector('#highlighter-bottom'),
};
Object.keys(obj).forEach((key_) => {
let csses = obj[key_];
let ele = highLightElements[key_];
Object.keys(csses).forEach(cssKey => {
let cssVal = csses[cssKey];
ele.style[cssKey] = String(`${cssVal}px`);
});
});
}
let listener = (event) => {
if (
event.target.id.indexOf('selector') !== -1
) {
return;
}
const $target = document.elementFromPoint(event.x, event.y);
$target.addEventListener('click', (ev) => {
document.querySelector('body')
.removeEventListener('mousemove', listener);
// todo code is here
});
const targetOffset = $target.getBoundingClientRect();
const targetWidth = targetOffset.width;
const targetHeight = targetOffset.height;
applyHighlighterCss({
top: {
left: targetOffset.left,
top: (targetOffset.top - 1),
width: (targetWidth + 1),
},
right: {
left: (targetOffset.right),
top: targetOffset.top,
height: (targetHeight + 1),
},
bottom: {
left: targetOffset.left,
top: (targetOffset.bottom),
width: (targetWidth + 1),
},
left: {
left: targetOffset.left,
top: (targetOffset.top - 1),
height: (targetHeight + 1),
},
});
$target.style.color = 'green';
// $target.style.backgroundColor = 'grey';
};
document.querySelector('body')
.addEventListener('mousemove', listener);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment