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

173
Views
Change element's position next to the cursor

Ok so I have this:

x = `${e.clientX+window.scrollX}px`
y = `${e.clientY+window.scrollY}px`

And it works, but the problem is that the element can extend outside of the page and it creates a scrollbar.
So how can I make it position the div in some way that it wouldn't do that? Is it possible?

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

0

I tried to solve this through JS. Here is my code:

var pointerX = -1;
var pointerY = -1;
document.onmousemove = function(event) {
    pointerX = event.pageX;
    pointerY = event.pageY;
}
setInterval(pointerCheck, 1000);
function pointerCheck() {
    console.log('Cursor at: '+pointerX+', '+pointerY);
}
about 4 years ago · Juan Pablo Isaza Report

0

You can take the viewport width / height and the element width / height into account to calculate the x/y coordinates of your element. See the pseudo-code attached to get the idea. Might need a tweak here and there :)

const { clientHeight, clientWidth } = document.documentElement
const { offsetHeight, offsetWidth } = element
const { clientY, clientX } = event
const { scrollY, scrollX } = window

let x = clientX + scrollX

// check if the box would leave the viewport on the right side
if (x + offsetWidth > clientWidth) {
    // move the element to the left
    x -= clientWidth - (x + offsetWidth)
}

let y = clientY + scrollY

// check if the box would leave the viewport on the bottom
if (y + offsetHeight > clientHeight) {
    // move the element to the top
    y -= clientHeight - (y + offsetHeight)
}
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!