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

241
Visualizações
JS: How to make a Div follow your cursors height

I am having problems trying to make this div have the same height on the screen as the cursor. Currently the div stays stationary. Here is what I have now:

  <body>
    <div id="container">

        <div id="mouseHover"></div>

    </div>

    <script>

        let box = document.getElementById('mouseHover');

        var mousex = 0;
        var mousey = 0;

        // Listens for where the mouse is hovering
        function mouse(){
        mousex = event.clientX;
        mousey = event.clientY;

        box.style.y = mousey;

        }

        document.addEventListener('mousemove', mouse);

    </script>
  </body>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

//Make the DIV draggagle:
dragElement(document.getElementById("mouseHover"));

function dragElement(elmnt) {
  var pos1 = 0,
    pos2 = 0,
    pos3 = 0,
    pos4 = 0;

  elmnt.onmousedown = dragMouseDown;

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
#container {
  width: 1700px;
  height: 100vh;
}

#mouseHover {
  width: 150px;
  height: 150px;
  border: 2px solid red;
}

.draggable {
  cursor: move;
  position: absolute;
  user-select: none;
}
<div id="container">

  <div id="mouseHover" class="draggable"></div>

</div>

I tried to reproduce your issue and made some changes to it. Below is the code. Hope that's how you wanted it to work.

Full working code at fiddle - https://jsfiddle.net/jkp15cyo/1/

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