开发者

Javascript - Issue with mouse events and floating element

开发者 https://www.devze.com 2023-03-01 02:56 出处:网络
I\'ve got a tricky problem with a script I\'m writing using jQuery. I have a table which will have a toolbar appear on any row if hovered over (except the heading row).This is working great, and this

I've got a tricky problem with a script I'm writing using jQuery.

I have a table which will have a toolbar appear on any row if hovered over (except the heading row). This is working great, and this is the code for it:

$cont.delegate('tr:not(:eq(0))','mouseover mouseout', function(e){
    var $this = $(this);
    var pos = $this.position();
    if(e.type == 'mouseout') {
        $actionToolbar.hide();
    } else {
        $actionToolbar.css({
            'top'  : pos.top  + $this.height()/2 - $actionToolbar.height()/2,
            'left' : pos.left + $this.width()    - $actionToolbar.width()
        }).show();
    }
});

The problem arises when I hover over the action toolbar. The row's mouseout event fires, and the toolbar hides (then enters an infinite loop of showing/hiding). This is because the toolbar is absolutely positioned and not a child of the row.

Here's the deal:

  • I do not want it to be a child of the row, because that means I'd have to remove and append to the DOM for every mouse event -- and this is not as efficient as simply updating the eleme开发者_开发问答nt's CSS.
  • I would also like to avoid timers to solve this if possible.

Thanks in advance!


Having little patience, I've gone and found my own answer:

I've removed the mouseout event listener and moved to using jQuery's data() method to keep track of the previously hovered row. Then, I add a mouseleave event listener to the container.

It works perfectly:

$cont.delegate('tr:not(:eq(0))', 'mouseover', function(e) {
    var $this = $(this);
    $($cont.data('COLStorage.row')).removeClass(opts.hoverClass);
    $this.addClass(opts.hoverClass);
    var pos = $this.position();
    $actionToolbar.css({
        'top': pos.top + $this.height() / 2 - $actionToolbar.height() / 2,
        'left': pos.left + $this.width() - $actionToolbar.width()
    }).show();
    $cont.data('COLStorage.row', $this);
}).mouseleave(function(e) {
    $actionToolbar.fadeOut();
});


Try mouseenter and mouseleave instead of mouseover and mouseout.


Put a marker css selector on your action toolbar and check for it using jquery. Then don't apply if your event's target has that marker selector.

0

精彩评论

暂无评论...
验证码 换一张
取 消