如何设置动画,使div出现和消失时向下扩展和向上收缩?我想也许css3动画,但当我添加过渡动画到我的CSS它没有做任何事情.

    function SearchToggle(){
        var off=document.getElementById('SearchContainer');
        if (off.style.display == "none") {
            off.style.display = "block";
        } else {
            off.style.display = "none";
        }        
    }
#SearchContainer {
        border: none;
        outline: none;
        display: block;
        background-color: silver;
        color: #FFF;
        margin: 0 0 0 0;
        float: left;
        width: 400px;
        min-width: 400px;
        max-width: 400px;
        height: 40px;
        min-height: 40px;
        max-height: 40px;
    }
 <menu name="WindowMenu" id="WindowMenu" class="WindowMenu">
        <span name="WindowIcon-Span" id="WindowIcon-Span" class="WindowIcon-Span">
            <img src="ROOT.ASSETS/IMAGES/ICONS/ICON GROUP 1/FAVICON1.64.png" name="WindowIcon-PNG" id="WindowIcon-PNG" class="WindowIcon-PNG" />
        </span>

        <span name="WindowTitle" id="WindowTitle" class="WindowTitle"> PROTON CORE </span>

        <div name="WindowControl-Wrapper" id="WindowControl-Wrapper" class="WindowControlWrapper">
           <i name="SearchIcon" id="SearchIcon" class="fa-solid fa-magnifying-glass" onclick="SearchToggle()"></i>

           <input type="button" value="" name="WindowControl-1" id="WindowControl-1" class="WindowControl-1" onclick="window.close(true)" />
           <input type="button" value="" name="WindowControl-2" id="WindowControl-2" class="WindowControl-2" />
        </div>
    </menu>

    <form name="SearchContainer" id="SearchContainer" class="SearchContainer">

    </form>

我试着用CSS制作高度转换的动画,但在将其更改为高度后就停止了.

function SearchToggle(){
    var off=document.getElementById('SearchContainer');
    if (off.style.height == "0px") {
        off.style.height = "40px";
    } else {
        off.style.display = "0px";
    }        
}

#SearchContainer {
    border: none;
    outline: none;
    background-color: silver;
    color: #FFF;
    margin: 0 0 0 0;
    float: left;
    width: 400px;
    min-width: 400px;
    max-width: 400px;
    height: 0px;
    //min-height: 40px;
    //max-height: 40px;

    transition: height 2s;
}

推荐答案

好的,我想你想要的是:

var current = "up";
     function SearchToggle(){
         if(current == "up"){
            document.getElementById("WindowMenu").classList.remove("WindowMenuUp")
            document.getElementById("WindowMenu").classList.add("WindowMenuDown")
            current = "down";
         }
        else {
            document.getElementById("WindowMenu").classList.remove("WindowMenuDown")
            document.getElementById("WindowMenu").classList.add("WindowMenuUp")
            current = "up";
        }
    }
.WindowMenuDown {
    height: 40px;
    transition: 1s;
  }
    .WindowMenuUp {
    height: 0;
    transition: 1s;
  }
  #WindowControl-Wrapper {
     overflow: auto;
  }
<menu name="WindowMenu" id="WindowMenu" class="">
        <span name="WindowIcon-Span" id="WindowIcon-Span" class="WindowIcon-Span">
            <img src="ROOT.ASSETS/IMAGES/ICONS/ICON GROUP 1/FAVICON1.64.png" name="WindowIcon-PNG" id="WindowIcon-PNG" class="WindowIcon-PNG" />
        </span>

        <span name="WindowTitle" id="WindowTitle" class="WindowTitle"> PROTON CORE </span>

        <div name="WindowControl-Wrapper" id="WindowControl-Wrapper" class="WindowControlWrapper">
           <i name="SearchIcon" id="SearchIcon" class="fa-solid fa-magnifying-glass"></i>

           <input type="button" value="" name="WindowControl-1" id="WindowControl-1" class="WindowControl-1" onclick="window.close(true)" />
           <input type="button" value="" name="WindowControl-2" id="WindowControl-2" class="WindowControl-2" />
        </div>
    </menu>

    <form name="SearchContainer" id="SearchContainer" class="SearchContainer">
    <input type="button" value="Button"  onclick="SearchToggle()">
    </form>

如果您决定按要求使用JQuery,下面是一个片段:

#WindowControl-Wrapper {
  display: none;
  overflow: auto;
}
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>

<body>
  <menu name="WindowMenu" id="WindowMenu" class="">
        <span name="WindowIcon-Span" id="WindowIcon-Span" class="WindowIcon-Span">
            <img src="ROOT.ASSETS/IMAGES/ICONS/ICON GROUP 1/FAVICON1.64.png" name="WindowIcon-PNG" id="WindowIcon-PNG" class="WindowIcon-PNG" />
        </span>

        <span name="WindowTitle" id="WindowTitle" class="WindowTitle"> PROTON CORE </span>

        <div name="WindowControl-Wrapper" id="WindowControl-Wrapper" class="WindowMenuUp">

           <input type="button" value="" name="WindowControl-1" id="WindowControl-1" class="WindowControl-1" onclick="window.close(true)" />
           <input type="button" value="" name="WindowControl-2" id="WindowControl-2" class="WindowControl-2" />
        </div>
    </menu>

    <form name="SearchContainer" id="SearchContainer" class="SearchContainer">
    <input type="button" value="Button" id="button">
    </form>
  <script type="text/javascript">
  <!--
    $(document).ready(function(){
        var current = "up";
        $('#button').on('click',function() {

            if(current == "up"){
                $('#WindowControl-Wrapper').show(500).slideDown(500);
                current = "down"
            }
            else{
                $('#WindowControl-Wrapper').hide(500).slideUp(500);
                current = "down"
            }

        });
    })
  //-->
  </script>
 </body>

Javascript相关问答推荐

更新Reduxstore 中的状态变量,导致整个应用程序出现不必要的重新渲染

我的YouTube视频没有以html形式显示,以获取免费加密信号

如何通过继承contentitable属性的元素捕捉keydown事件?

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

使用下表中所示的值初始化一个二维数组

使用JavaScript重新排序行

Next.js(react)使用moment或不使用日期和时间格式

在Vite React库中添加子模块路径

如何将Map字符串,布尔值转换为{key:string;value:bo布尔值;}[]?<>

在forEach循环中获取目标而不是父对象的属性

如何将未排序的元素追加到数组的末尾?

在css中放置所需 colored颜色 以填充图像的透明区域

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

有效路径同时显示有效路径组件和不存在的路径组件

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

WebSocketException:远程方在未完成关闭握手的情况下关闭了WebSocket连接.&#三十九岁;

FileReader()不能处理Firefox和GiB文件

如何在Firebase中读取对象的特定字段?

递归地将JSON对象的内容上移一级

react :图表负片区域不同 colored颜色