tkss44
tkss44

Reputation: 33

How can I get jQuery to not display tool-tip after I stop hovering div?

I'm trying to have the tool tip disappear as soon I'm not hovering the div. However when I hover the next div, the tool tip from the previous div displays again. Is there any way that I can stop the tool tip from displaying as soon I leave the hover? I've tried to create a function where my tool-top disappears when I hover over it, but it didn't work. I've posted the code below to demonstrate my issue. Thank you very much for any help.

$(".photos").hover(
  function(){
    $(".tooltip",this).fadeIn();
  },
  function(){
    $(".tooltip",this).fadeOut();
  }
);
*{
  margin:0;
  padding:0;
  box-sizing:border-box;
}

.container{
  width:80%;
  margin:auto;
  text-align:center;
}

.photos{
  width:200px;
  height:200px;
  background-color:red;
  float:left;
  margin:20px 0 0 40px;
  position:relative;
}

.tooltip{
  content:"";
  position:absolute;
  width:200px;
  height:100%;
  background-color:gray;
  color:black;
  z-index:1;
  left:103%;
  display:none;
}

.tooltip:after{
  content:"";
  position:absolute;
  color:black;
  border-width:5px;
  border-style:solid;
  top:45%;
  right:100%;
  border-color:transparent rgba(0,0,0,.6) transparent transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
</div>

Upvotes: 1

Views: 52

Answers (2)

VXp
VXp

Reputation: 12058

You are missing the .stop() method:

$(".photos").hover(
  function(){
    $(".tooltip",this).stop().fadeIn();
  },
  function(){
    $(".tooltip",this).stop().fadeOut();
  }
);
*{
  margin:0;
  padding:0;
  box-sizing:border-box;
}

.container{
  width:80%;
  margin:auto;
  text-align:center;
}

.photos{
  width:200px;
  height:200px;
  background-color:red;
  float:left;
  margin:20px 0 0 40px;
  position:relative;
}

.tooltip{
  content:"";
  position:absolute;
  width:200px;
  height:100%;
  background-color:gray;
  color:black;
  z-index:1;
  left:103%;
  display:none;
}

.tooltip:after{
  content:"";
  position:absolute;
  color:black;
  border-width:5px;
  border-style:solid;
  top:45%;
  right:100%;
  border-color:transparent rgba(0,0,0,.6) transparent transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="container">
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
</div>

Upvotes: 2

Rick Hitchcock
Rick Hitchcock

Reputation: 35670

Add this style:

.tooltip {
  pointer-events: none;
}

This will prevent the mouse from interacting with the tooltip, which otherwise would re-trigger the hover event on its parent div.

Snippet:

$(".photos").hover(
  function() {
    $('.tooltip', this).fadeIn();
  },
  function() {
    $('.tooltip', this).fadeOut();
  }
);
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  width: 80%;
  margin: auto;
  text-align: center;
}

.photos {
  width: 200px;
  height: 200px;
  background-color: red;
  float: left;
  margin: 20px 0 0 40px;
  position: relative;
}

.tooltip {
  content: "";
  position: absolute;
  width: 200px;
  height: 100%;
  background-color: gray;
  color: black;
  z-index: 1;
  pointer-events: none;
  left: 103%;
  display: none;
}

.tooltip:after {
  content: "";
  position: absolute;
  color: black;
  border-width: 5px;
  border-style: solid;
  top: 45%;
  right: 100%;
  border-color: transparent rgba(0, 0, 0, .6) transparent transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>
  <div class="photos"><span class="tooltip">Hello There</span></div>

</div>

Upvotes: 2

Related Questions