I need the div to appear wherever the cursor is holding down the right mouse button.
In my scenario, I am using the following code:
<div class="d-none" id="item"></div>
#item{
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
background: royalblue;
}
.d-none{
display: none;
}
var myMouseX, myMouseY;
function getXYPosition(e) {
myMouseX = (e || event).clientX;
myMouseY = (e || event).clientY;
getPosition(myMouseX, myMouseY);
function getPosition(x, y) {
console.log('X = ' + x + '; Y = ' + y);
let div = document.querySelector("#item");
if (div.classList.contains('d-none')) {
div.classList.remove('d-none');
} else {
div.classList.add('d-none');
}
divX = x + "px";
divY = y + "px";
div.style.transform = `translate(calc(`+divX+` - 50%) , calc(`+divY+` - 50%))`;
}
}
window.addEventListener('contextmenu', function (e) {
e.preventDefault();
var pressTimer;
pressTimer = setTimeout(function() {
getXYPosition(e);
}, 1000);
window.addEventListener('mouseup', function (){
clearTimeout(pressTimer);
});
});
You can also view my Fiddle
It currently works with a left click by default using window.addEventListener('click')
So now, how do I change it to activate when holding the right click for a few seconds?