Function for mouse near an element in jQuery

后端 未结 1 1950

I want to track and show a tooltip when the mouse is near a table head element. It works with the mouseenter event, but I want show the tooltip before mou

1条回答
  •  一生所求
    2021-01-04 09:19

    This works. The first parameter can be any jQuery object. The second parameter is the nearness to the object, in this case 20px.

    Demo: http://jsfiddle.net/ThinkingStiff/Lpg8x/

    Script:

    $( 'body' ).mousemove( function( event ) {
    
        if( isNear( $( 'thead' ), 20, event ) ) {
            //show your tooltip here
        } else {
            //hide it here
        };
    
    });           
    
    function isNear( element, distance, event ) {
    
        var left = element.offset().left - distance,
            top = element.offset().top - distance,
            right = left + element.width() + 2*distance,
            bottom = top + element.height() + 2*distance,
            x = event.pageX,
            y = event.pageY;
    
        return ( x > left && x < right && y > top && y < bottom );
    
    };
    

    0 讨论(0)
提交回复
热议问题