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
Move a text div by jquery

i have a div that contains some text and i want to be able to move that div when i click and hold my left mouse button and when my pointer cursor location is changed the div location is changes too. my js code:

$(() => {
    $(document).on("mousemove", "#div", (e) => {
        const x = e.pageX
        const y = e.pageY
            $("#div").css("top", y - 50)
            $("#div").css("left", x - 100)
            console.log(e.pageY)
            console.log(e.pageX)
    })
})

my html code:

<div id="div">
        <span>Hello World!</span>
        <span>Look at Me!</span>
        <span>I'm Moving!</span>
    </div>

my problem is that i don't know to use which event. i want click and mousemove events together. what should i do now?

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

0

Consider the following example, based on the following Demo.

$(function() {
  $("#draggable").draggable({
    drag: function(event, ui) {
      console.log(event.pageX, event.pageY);
    }
  });
});
#draggable {
  width: 150px;
  height: 150px;
  padding: 0.5em;
}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.0/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.0/jquery-ui.js"></script>
<div id="draggable" class="ui-widget-content">
  <span>Hello World!</span>
  <span>Look at Me!</span>
  <span>I'm Moving!</span>
</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!