Changing the css of a div with Jquery mouseover

Asked

Viewed 593 times

1

I need that if the mouse is not on top of a div, after 4 seconds it disappears, changing the css. How to do this with Javascript/Jquery?

  • But it should appear if the cursor is on it again?

  • No! @Renan. No need

1 answer

3

CSS:

.hide{
    display: none;
);

HTML:

<div id="my-div" style="background-color:#ff0000" >Minha Div</button>

Javascript:

function hideDiv(){
    $("#my-div").addClass('hide');
}

t = setTimeout(hideDiv,4000);

$("#my-div").mouseout(function(event){
    t = setTimeout(hideDiv,4000);
});

$("#my-div").mouseover(function(){

    if (t!=null) {
        clearTimeout(t);
    }
});
  • Looks like it’s not working, buddy :( https://jsfiddle.net/u8Lj7m7L/

  • Of course, this code uses Jquery, Since you put the tag on the question, I understood that you would include the library. https://jsfiddle.net/7fadunso/

  • Sorry buddy, just forgot to include in the fiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.