Javascript 显示带有动画的隐藏 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14335501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:25:27  来源:igfitidea点击:

Show hide div with animation

javascriptanimationhtmlshow-hide

提问by user1632298

I made this script, which opens a div with the right class and close the others.

我制作了这个脚本,它用正确的类打开一个 div 并关闭其他类。

function showhide(id) {
    if (document.getElementById) {
        var divid = document.getElementById(id);
        var divs = document.getElementsByClassName("hideable");
        for (var i = 0; i < divs.length; i = i + 1) {
            divs[i].style.display = "none";
        }
        divid.style.display = "block";
    }
    return false;
}

Is it possible to make some animation, like fadout, easeout instead of just showing it by display options?

是否可以制作一些动画,例如淡出、淡出,而不是仅通过显示选项显示?

采纳答案by yogi

You could try this

你可以试试这个

function showhide(id) {
  if (document.getElementById) {
    var divid = document.getElementById(id);
    var divs = document.getElementsByClassName("hideable");
    for (var i = 0; i < divs.length; i = i + 1) {
      $(divs[i]).fadeOut("slow");
    }
    $(divid).fadeIn("slow");
  }
  return false;
}

Have a look at this fiddle "http://jsfiddle.net/9jtd3/"

看看这个小提琴“ http://jsfiddle.net/9jtd3/

There are many more techniques provided by Jquery library, You should have a look at that too.

Jquery 库提供了更多技术,您也应该看看。

回答by Arun Kasyakar

You can use slideDown() and slidUp()of jQuery

你可以使用slideDown() and slidUp()jQuery

$( document.body ).click(function () {
  if ( $( "div:first" ).is( ":hidden" ) ) {
    $( "div" ).slideDown( "slow" );
  } else {
    $( "div" ).slideUp("slow");
  }
});

回答by imVJ

Thiswill surely solve your problem.

肯定会解决您的问题。

You can use .fadeOut() directly if you have included jQuery library in your script.

如果您在脚本中包含了 jQuery 库,则可以直接使用 .fadeOut() 。

回答by Saju

If You are using Jquery then another way to do this is

如果您使用的是 Jquery,那么另一种方法是

function showhide(id) {
  $(".hideable".fadeOut("slow");
  $("#" + id).fadeIn("slow");
}

Assuming "hideable" as className in your group of divs

假设您的 div 组中的“可隐藏”为 className

Good luck.

祝你好运。

回答by codedude

This is way easier with only CSS.

仅使用 CSS 就更容易了。

You make a class

你做一堂课

div {
 display:block;
 transition: all .2s ease-out;
}

.hidden {
 display:none;
}

And with javascript, you apply or remove the class hidden when you want to. jQuery animation lib is wayyyy far from good to be used. It's clunky, and ressource eating for your user. CSS works with your GPU instead, allowing a more fluid animation.

使用 javascript,您可以在需要时应用或删除隐藏的类。jQuery 动画库远不能很好地使用。它很笨重,并且为您的用户消耗资源。CSS 与您的 GPU 一起工作,允许更流畅的动画。

回答by Felipe Fonseca

You can do that using a Library like jQueryor something.

您可以使用jQuery 之类的库来做到这一点。

You can sure make it using plain javascript, but there's no point doing that since jQuery is an amazing library.

您肯定可以使用普通的 javascript 来实现它,但是这样做没有意义,因为 jQuery 是一个了不起的库。

See some examples of showand hide

查看显示隐藏的一些示例