Is it possible to use jQuery .on and hover?

前端 未结 10 779
失恋的感觉
失恋的感觉 2020-11-22 11:03

I have a

    that is populated with javascript after the initial page load. I\'m currently using .bind with mouseover and
相关标签:
10条回答
  • 2020-11-22 11:43

    If you need it to have as a condition in an other event, I solved it this way:

    $('.classname').hover(
         function(){$(this).data('hover',true);},
         function(){$(this).data('hover',false);}
    );
    

    Then in another event, you can easily use it:

     if ($(this).data('hover')){
          //...
     }
    

    (I see some using is(':hover') to solve this. But this is not (yet) a valid jQuery selector and does not work in all compatible browsers)

    0 讨论(0)
  • 2020-11-22 11:43

    The jQuery plugin hoverIntent http://cherne.net/brian/resources/jquery.hoverIntent.html goes much further than the naive approaches listed here. While they certainly work, they might not necessarily behave how users expect.

    The strongest reason to use hoverIntent is the timeout feature. It allows you to do things like prevent a menu from closing because a user drags their mouse slightly too far to the right or left before they click the item they want. It also provides capabilities for not activating hover events in a barrage and waits for focused hovering.

    Usage example:

    var config = {    
     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
     interval: 200, // number = milliseconds for onMouseOver polling interval    
     over: makeTall, // function = onMouseOver callback (REQUIRED)    
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out: makeShort // function = onMouseOut callback (REQUIRED)
    };
    $("#demo3 li").hoverIntent( config )
    

    Further explaination of this can be found on https://stackoverflow.com/a/1089381/37055

    0 讨论(0)
  • 2020-11-22 11:50

    You can you use .on() with hover by doing what the Additional Notes section says:

    Although strongly discouraged for new code, you may see the pseudo-event-name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.

    That would be to do the following:

    $("#foo").on("hover", function(e) {
    
        if (e.type === "mouseenter") { console.log("enter"); }
        else if (e.type === "mouseleave") { console.log("leave"); }
    
    });
    

    EDIT (note for jQuery 1.8+ users):

    Deprecated in jQuery 1.8, removed in 1.9: The name "hover" used as a shorthand for the string "mouseenter mouseleave". It attaches a single event handler for those two events, and the handler must examine event.type to determine whether the event is mouseenter or mouseleave. Do not confuse the "hover" pseudo-event-name with the .hover() method, which accepts one or two functions.

    0 讨论(0)
  • 2020-11-22 11:50
    $("#MyTableData").on({
    
     mouseenter: function(){
    
        //stuff to do on mouse enter
        $(this).css({'color':'red'});
    
    },
    mouseleave: function () {
        //stuff to do on mouse leave
        $(this).css({'color':'blue'});
    
    }},'tr');
    
    0 讨论(0)
提交回复
热议问题