How do I check if the mouse is over an element in jQuery?

前端 未结 24 2633
不思量自难忘°
不思量自难忘° 2020-11-22 08:10

Is there a quick & easy way to do this in jQuery that I\'m missing?

I don\'t want to use the mouseover event because I\'m already using it for something else. I

24条回答
  •  星月不相逢
    2020-11-22 09:17

    I took SLaks' idea and wrapped it in a small class.

    function HoverWatcher(selector){
      this.hovering = false;
      var self = this; 
    
      this.isHoveringOver = function() { 
        return self.hovering; 
      } 
    
        $(selector).hover(function() { 
          self.hovering = true; 
        }, function() { 
          self.hovering = false; 
        }) 
    } 
    
    var box1Watcher = new HoverWatcher('#box1');
    var box2Watcher = new HoverWatcher('#box2');
    
    
    
    $('#container').click(function() {
      alert("box1.hover = " + box1Watcher.isHoveringOver() +
            ", box2.hover = " + box2Watcher.isHoveringOver());
    });
    

提交回复
热议问题