![](/img/trans.png)
[英]check for ('div')mouseenter on ('a')mouseleave
[英]Div mouseenter and mouseleave
我有兩個 Div(CSS 是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">
問題是當我輸入#imagebox
,每次鼠標移動都會觸發mouseout
事件,從而導致快速閃爍。 如何確保不添加invisible
的 class 除非 cursor 完全超出imagebox
$(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">
您也可以將#PictureHoverPanel
添加到事件偵聽器中。
像這樣:
$(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">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.