javascript - How to check if cursor is on a div - Stack Overflow

On clicking on a link I show a pop up. Here is my pop up code<div class='' id="custom

On clicking on a link I show a pop up. Here is my pop up code

<div class='' id="custom-popover">
    <div class="arrow"></div>
    <h3 class="popover-title">Popover left</h3>
    <div class="popover-content" id="details-container">
    </div>
  </div>
</div>

I also add some html code using jquery when pop up show. Now I want to check if cursor is on this pop over or if user is hovering over this pop up and then I show an alert message otherwise hide the popup. How can I do this? I tried something like this

var isHovered = $('#custom-popover').is(":hover");
if (isHovered){
alert("msg"); 
} else {
 $('#custom-popover').hide();
 }

But this is not working. How can I do this?

On clicking on a link I show a pop up. Here is my pop up code

<div class='' id="custom-popover">
    <div class="arrow"></div>
    <h3 class="popover-title">Popover left</h3>
    <div class="popover-content" id="details-container">
    </div>
  </div>
</div>

I also add some html code using jquery when pop up show. Now I want to check if cursor is on this pop over or if user is hovering over this pop up and then I show an alert message otherwise hide the popup. How can I do this? I tried something like this

var isHovered = $('#custom-popover').is(":hover");
if (isHovered){
alert("msg"); 
} else {
 $('#custom-popover').hide();
 }

But this is not working. How can I do this?

Share Improve this question asked Mar 13, 2014 at 4:47 asdfkjasdfjkasdfkjasdfjk 3,90418 gold badges66 silver badges107 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

use global variable:

var cursorOnDiv = false;

$(document).on({
        mouseenter:function(){ cursorOnDiv = true; },
        mouseleave:function(){ cursorOnDiv = false; },
    }, 
   '#yourDivId'
);

and check him

Try it:

$("#custom-popover").hover(
   function() {
       $("#custom-popover").show();
   },
   function() {
       $("#custom-popover").hide();
});

Some thing like this,

$( "#custom-popover" ).mouseover(function() {
     $('#custom-popover').hide();
});

the function is() as per doc is often useful inside callbacks, such as event handlers. This function runs only once as it is not a callback. So in order to detect hover events use call backs.

You can have even like this if you want to have enter and leave both,

$( "custom-popover" )
.mouseenter(function() {

})
.mouseleave(function() {

})

This is what you ask:

$("#custom-popover").hover(
   function() {
       alert("Hovering now");
       $("#custom-popover").show();
   },
   function() {
       $("#custom-popover").hide();
});

You stored a value whether #custom-popover is being hovered or not at the time of execution, you did not bind an eventListener (hover) for the cause.

Try this:

if($('#custom-popover:hover'))
{
       alert("msg");
}
else 
{
      $('#custom-popover').hide();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347802a4426899.html

相关推荐

  • javascript - How to check if cursor is on a div - Stack Overflow

    On clicking on a link I show a pop up. Here is my pop up code<div class='' id="custom

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信