Div mouseenter and mouseleave

I have two Divs (CSS is TailwindCss)

$(document).on("mouseover", "#imagebox", function() {
  $("#PictureHoverPanel").removeClass("invisible");

})

$(document).on("mouseout", "#imagebox", function() {
  $("#PictureHoverPanel").addClass("invisible");

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.0.3/tailwind.min.css" integrity="sha512-wl80ucxCRpLkfaCnbM88y4AxnutbGk327762eM9E/rRTvY/ZGAHWMZrYUq66VQBYMIYDFpDdJAOGSLyIPHZ2IQ==" crossorigin="anonymous" />


<div id="PictureHoverPanel" class="invisible absolute flex flex-col justify-center rounded-xl bg-gray-600 opacity-70">
  <div class="flex justify-center">
    <a class="bg-gray-800 px-2 py-1 text-white border border-red-800 font-semibold cursor-pointer">Remove</a>

  </div>

</div>
<img id="imagebox" class="w-48 h-48 rounded-xl md:w-64 md:h-64" src="https://picsum.photos/seed/1/100">

The problem is as I enter #imagebox every mouse move triggers the mouseout event resulting in a really quick blink. How can I make sure that the invisible class will not be added unless the cursor is fully out of imagebox

2 thoughts on “Div mouseenter and mouseleave”

  1. $(document).on("mouseover", "#imagebox", function() {
      $("#PictureHoverPanel").removeClass("invisible");
    
    })
    
    $(document).on("mouseout", "#imagebox", function() {
      $("#PictureHoverPanel").addClass("invisible");
    
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.0.3/tailwind.min.css" integrity="sha512-wl80ucxCRpLkfaCnbM88y4AxnutbGk327762eM9E/rRTvY/ZGAHWMZrYUq66VQBYMIYDFpDdJAOGSLyIPHZ2IQ==" crossorigin="anonymous" />
    
    
    <div id="PictureHoverPanel" class="invisible absolute flex flex-col justify-center rounded-xl bg-gray-600 opacity-70">
      <div class="flex justify-center">
        <a class="bg-gray-800 px-2 py-1 text-white border border-red-800 font-semibold cursor-pointer">Remove</a>
    
      </div>
    
    </div>
    
    <img id="imagebox" class="w-48 h-48 rounded-xl md:w-64 md:h-64" src="/assets/photos/newyork1.jpg">
    Reply
  2. You can add #PictureHoverPanel as well to the event listener.

    Like this:

    $(document).on("mouseover", "#imagebox, #PictureHoverPanel", function() {
      $("#PictureHoverPanel").removeClass("invisible");
    })
    
    $(document).on("mouseover", "#imagebox, #PictureHoverPanel", function() {
      $("#PictureHoverPanel").removeClass("invisible");
    })
    
    $(document).on("mouseout", "#imagebox, #PictureHoverPanel", function() {
      $("#PictureHoverPanel").addClass("invisible");
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.0.3/tailwind.min.css" integrity="sha512-wl80ucxCRpLkfaCnbM88y4AxnutbGk327762eM9E/rRTvY/ZGAHWMZrYUq66VQBYMIYDFpDdJAOGSLyIPHZ2IQ==" crossorigin="anonymous" />
    
    
    <div id="PictureHoverPanel" class="invisible absolute flex flex-col justify-center rounded-xl bg-gray-600 opacity-70">
      <div class="flex justify-center">
        <a class="bg-gray-800 px-2 py-1 text-white border border-red-800 font-semibold cursor-pointer">Remove</a>
    
      </div>
    
    </div>
    <img id="imagebox" class="w-48 h-48 rounded-xl md:w-64 md:h-64" src="https://picsum.photos/seed/1/100">
    Reply

Leave a Comment