<div class="container">
<div class="tool draggable" id="tool"><div class="oeil oeil-gauche"></div><div class="oeil oeil-droite"></div></div>
<img class="image" src="https://velvetcocoon.com/sandbox/background_anaglyphe_photo.png">
</div>
</div>
body {
background: #fff;
}
.container {
display: flex;
justify-content: center;
align-items: center;
}
.image {
height: 50%;
width: 50%;
position: relative;
}
.tool {
width: 20rem;
height: 10rem;
position: absolute;
left: 20px;
z-index: 10;
border: 2px solid black;
mix-blend-mode: multiply;
}
.oeil {
height: 10rem;
width: 10rem;
border-radius: 50%;
z-index: 10;
display: inline-block;
}
.oeil-gauche {
background: red;
}
.oeil-droite {
background: blue;
}
let isDragging = false;
document.addEventListener('mousedown', function(event) {
let dragElement = event.target.closest('.draggable');
if (!dragElement) return;
event.preventDefault();
dragElement.ondragstart = function() {
return false;
};
let coords, shiftX, shiftY;
startDrag(dragElement, event.clientX, event.clientY);
function onMouseUp(event) {
finishDrag();
};
function onMouseMove(event) {
moveAt(event.clientX, event.clientY);
}
function startDrag(element, clientX, clientY) {
if(isDragging) {
return;
}
isDragging = true;
document.addEventListener('mousemove', onMouseMove);
element.addEventListener('mouseup', onMouseUp);
shiftX = clientX - element.getBoundingClientRect().left;
shiftY = clientY - element.getBoundingClientRect().top;
element.style.position = 'fixed';
moveAt(clientX, clientY);
};
function finishDrag() {
if(!isDragging) {
return;
}
isDragging = false;
dragElement.style.top = parseInt(dragElement.style.top) + window.pageYOffset + 'px';
dragElement.style.position = 'absolute';
document.removeEventListener('mousemove', onMouseMove);
dragElement.removeEventListener('mouseup', onMouseUp);
}
function moveAt(clientX, clientY) {
let newX = clientX - shiftX;
let newY = clientY - shiftY;
let newBottom = newY + dragElement.offsetHeight;
if (newBottom > document.documentElement.clientHeight) {
let docBottom = document.documentElement.getBoundingClientRect().bottom;
let scrollY = Math.min(docBottom - newBottom, 10);
if (scrollY < 0) scrollY = 0;
window.scrollBy(0, scrollY);
newY = Math.min(newY, document.documentElement.clientHeight - dragElement.offsetHeight);
}
if (newY < 0) {
let scrollY = Math.min(-newY, 10);
if (scrollY < 0) scrollY = 0;
window.scrollBy(0, -scrollY);
newY = Math.max(newY, 0);
}
if (newX < 0) newX = 0;
if (newX > document.documentElement.clientWidth - dragElement.offsetWidth) {
newX = document.documentElement.clientWidth - dragElement.offsetWidth;
}
dragElement.style.left = newX + 'px';
dragElement.style.top = newY + 'px';
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.