简体   繁体   中英

Hide a div with opacity animate function

I am trying to hide a div with opacity animate function. Basically, I want the div to be hidden on click. But I want it to fadeout. Below is the code I have for it. can anyone help?

$("#div1").click(function() {
  $(this).animate({ opacity: "0" }, 1000);
  $("div").hide();
});

also, is it better to use fadeOut function instead of animate opacity?

fadeOut() is simpler because it will hide it for you automatically when it is done so you can save that code and it automatically waits for the animation to be done before hiding the element (something your current code was not doing).

$("#div1").click(function() {
  $(this).fadeOut(1000);
});

Try this JSFIDDLE

$("#div1").click(function() {
    $(this).animate({ opacity: "0" }, 1000, function(){
        $(this).hide();
    });

});

Also you can use .fadeout(1000) . to get same behavior.

You can use .fadeOut() API for this,

$("#div1").click(function() {

   $(this).fadOut(1000);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM