Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle jquery - issue

I have this demo

However the mouse over when dragged to left or right stops the toogle. The hover() event didn't solve the problem.

Any idea ?

div.fileinputs {
    position: relative;
    display: none;
}

#show {
    width: 200px;
    height: 40px;
    background-color: red;
    z-index: -2px;
    position: absolute;
}

<div id="show"></div>

<div class="fileinputs">Visible Panel Div</div>

$('#show').mouseover(function() {
    $('.fileinputs').toggle();
});
like image 858
Daniel Avatar asked Feb 23 '26 15:02

Daniel


1 Answers

Given that you want to simply show the element on mouseover and then hide it on mouseout, you should also use mouseout() to define the desired behavior you want when the mouse is removed:

$("#show")
    .mouseover(function(){
        $(".fileinputs").toggle();
    })
    .mouseout(function(){
        $(".fileinputs").toggle();
    });

Example. (It's choppy because fileinputs is a separate element, and it's not counting hovering over that as hovering over the show div).

But you should use hover, just to make it easier:

$("#show").hover(function(){
    $(".fileinputs").show();
}, function(){
    $(".fileinputs").hide();
});

Example. (Choppy for the same reason as above).

Since your desired behavior is definite, we'll just use show() for when the mouse is over it and hide() when it is removed.

By the way, it is preferred that you bind events using delegate() (for older versions of jQuery) or on() (for jQuery 1.7+):

$(document).on("mouseover mouseout", "#show", function(){
    $(".fileinputs").toggle();
});

Example.

Though, you really should just use CSS for this. You can place fileinputs inside of show and use a child selector:

#show:hover > .fileinputs {
    display: block;
}

Example. This one doesn't flicker because the element is inside the one that's getting the hover declarations attached to it, and CSS considers it as though you are still hovering over the parent element (because you technically are, as the target of the hover is within the boundaries of the parent [it would still work if it was outside the boundaries because the element is still nested]).

like image 52
Purag Avatar answered Feb 25 '26 03:02

Purag