Onmousemove event with jquery

Asked

Viewed 90 times

1

I’m trying to recreate this tooltip http://jsfiddle.net/HJf8q/ with jQuery.

$(document).ready(function(){
  $(".tooltip").mousemove(function(event) {
    $(".text").style.top = (event.pageY + 20) + "px";
    $(".text").style.left = (event.pageX + 20) + "px";
  });
});
.tooltip {
  position: relative;
}

.tooltip span {
  display:none;
}

.tooltip:hover span {
  display:block;
  position:fixed;
  overflow:hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tooltip">
  <span class="text">Texto</span>
  <p>Passe o mouse aqui.</p>
</div>

However, Text does not follow mouse movement. Could someone tell me how to do this?

  • 1

    jQuery is returning all found elements, so it is an object, change $(".text").style for $(".text")[0].style, if you have more than one element you will have to traverse this object.

  • 1

    See working in jsfiddle.net

  • That’s right! Thank you very much!

  • 1

    @Noobsaibot Put as answer.

2 answers

1


Like quoted by Anderson, it would be necessary a response that is flexible to the number of tooltips that you have on the page.

Based on that link, created this example that will apply the effect to all tooltips present in the document.

How are you wearing jquery, I felt free to use the method css() to make the changes.

$(document).ready(function(){
  $('.tooltip').each(function(){
    $(this).mousemove(function(ev){
      var x = ev.clientX,
          y = ev.clientY
      $(".text", this).css("top", (y + 20) + "px"); 
      $(".text", this).css("left", (x + 20) + "px") ;
    })
  }); 
});
.tooltip {
  position: relative;
}

.tooltip span {
  display:none;
}

.tooltip:hover span {
  display:block;
  position:fixed;
  overflow:hidden;
}
<div class="tooltip">
  <span class="text">Texto</span>
  <p>Passe o mouse aqui.</p>
</div>

<div class="tooltip">
  <span class="text">Outro texto</span>
  <p>Outro tooltip</p>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

How you are trying to find an element by class indicates that you want the first element found in case [0] the first position.

From a look at the example I made below, I hope I’ve helped...

$(document).ready(function(){
  $(".tooltip").mousemove(function(event) {
    $(".text")[0].style.top = (event.pageY + 20) + "px";
    $(".text")[0].style.left = (event.pageX + 20) + "px";
  });
});
.tooltip {
  position: relative;
}

.tooltip span {
  display:none;
}

.tooltip:hover span {
  display:block;
  position:fixed;
  overflow:hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tooltip">
  <span class="text">Texto</span>
  <p>Passe o mouse aqui.</p>
</div>

  • 2

    And if there are several tooltips on the page, how would it look?

  • If there were many tooltips it would be better to make a selection by ID, in my view, but there may be some easier way to create several different ID’s and select one by one, I’ll do a search and try to inform you better...

  • You are already selecting by the element in .tooltip in the event, then just search for the .text within it. $(this).children('.text').style.top, for example.

  • Ah right Thanks... is that I am also running after studies with jQuery has many tricks and things that I do not know yet and I doubt, but thank you.

  • Take the test and add in your answer :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.