Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
How to click and drag a large view like in google maps?

Suppose I have a large view with scroll in both x and y direction and instead of scrolling with mouse scroll, I need to drag and move to navigate through the entire view like in google maps

<div id="wrapper">
  <div id="dragable_content">
    <img src="https://geology.com/world/world-map.gif" style="height:100rem;"/>
  </div>
</div>

how do I achieve this? preferably using only pure js

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thanks to the solution provided by @grisuu in the comments

I was able to whip up this:

jsfiddle

var _startX = 0;
var _startY = 0;
var _offsetX = 0;           
var _offsetY = 0;
var _dragElement;
document.onmousedown = OnMouseDown;
document.onmouseup = OnMouseUp;

function OnMouseDown(event){
  document.onmousemove = OnMouseMove;
    _startX = event.clientX;
  _startY = event.clientY;
  _offsetX = document.getElementById('div1').offsetLeft;
  _offsetY = document.getElementById('div1').offsetTop;
  _dragElement = document.getElementById('div1');

}

function OnMouseMove(event){
    _dragElement.style.left = (_offsetX + event.clientX - _startX) + 'px';
  _dragElement.style.top = (_offsetY + event.clientY - _startY) + 'px';
}

function OnMouseUp(event){
  document.onmousemove = null;
  _dragElement=null;
}
html{ 
  background-color:green;
  overflow: hidden;
}


.div1{position:absolute; height:500px; width: 500px; z-index:1;}
<div class="div1" id="div1">

    <div style="height:50px;width:50px;background-color:red;"></div>
    <div style="position:absolute;top:75px;left:100px;height:50px;width:50px;background-color:blue;"></div>

  </div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!