JSFiddle - React, Tailwind, and code Playground
HTML
<div id="container">
<div id="viewport">
</div>
<img id="image" src="https://www.fag.edu.br/novo/img/mapa_do_campus/mapa.png" />
</div>
CSS
#image {
cursor: move;
margin-left: -50px;
margin-top: -50px;
}
#viewport {
cursor: move;
position: absolute;
top: 50px;
left: 50px;
width: 400px;
height: 150px;
border: solid 2px blue;
}
JavaScript
$(document).ready(function(){
var isDragging,
top = 0, left = 0,
curX, curY;
$("#image").mousedown(function (e) {
e.preventDefault();
});
$("#container").mousedown(function (e) {
isDragging = true;
curX = e.pageX;
curY = e.pageY;
left = Number($("#image").css("margin-left").
toString().replace("px", ""));
top = Number($("#image").css("margin-top").
toString().replace("px", ""));
});
// end dragging
$(document).mouseup(function () {
if (isDragging){
// reset
isDragging = false;
top = 0;
left = 0;
}
});
$("#container").mousemove(function(e){
if (!isDragging) {
return;
}
var dx = e.pageX - curX;
var dy = e.pageY - curY;
left += dx;
top += dy;
// set the position
$("#image").css("margin-left", left + "px").
css("margin-top", top + "px");
curX = e.pageX;
curY = e.pageY;
});
});