Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda