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

175
Views
How do I make the positional values of my onMouseMove function relate to the local div, rather than the entire visible area?

I have created a class which renders a div to which I have connected some functions that get executed whenever the user starts clicking and dragging within the view (onMouseMove event). I have used the following code to do so:

class MouseMover extends React.Component {
  state = {
    x: 0,
    y: 0,
    open: false
  };

  handleMouseMove = e => {
    if (!this.state.open) {
      return
    }
    this.setState({
      x: e.clientX,
      y: e.clientY
    });
  };

  started = e => {
    this.setState({
      open: true
    });
  };

  ended = e => {
    this.setState({
      open: false
    });
  };

  render() {
    return (
      <div onPointerDownCapture={this.started} onPointerLeave={this.ended} onPointerUpCapture={this.ended} onMouseMove={this.handleMouseMove} className='gesture-view'>
        {this.state.x || this.state.y
          ? "The mouse is at x: " + this.state.x + ", y: " + this.state.y
          : "Move the mouse over this box"}
      </div>
    );
  }
}

The question I have relates to the x- and y-values (the position of the mouse as it drags). The positional values are relative to the entire visible screen. This means that when the user clicks and drags to the left end of the div, the x value will be 60, because I have a marginal of 5vw to the left. I would like to have the positional values relate to the div that the events are connected to. When the user clicks and drags to the left part of the div, the x value should move towards zero, not 60.

I am not sure that this is necessary information, but it might be worth to know that my App function, which I render into the DOM looks like this:

function App() {
  return (
    <div className="App">
      <MouseMover />
    </div>
  );
}

It is quite uncomplicated but I need to fix the issue before I proceed, since this is just a prototype.

about 4 years ago · Juan Pablo Isaza
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!