我正在试着让我的汉堡菜单发挥作用,但它没有(无论如何都不是我想要的).动画过渡在第一次单击该按钮时起作用,但在再次单击时,它会执行相同的操作.它的动画应该像单击时第一个的反向过渡一样.

let a = document.getElementsByClassName("mobile1")[0];

document.querySelector(".button-one").addEventListener("click", () => {
  a.classList.toggle("mobile2");
})
button {
  background: transparent;
  border: 3px #000 solid;
  border-radius: 0.25rem;
  position: absolute;
  left: 20px;
  top: 20px
}

.button-one {
  align-items: center;
  display: flex;
  height: 21px;
  width: 30px;
}

.mobile1 {
  background-color: black;
  height: 3px;
  width: 100%
}

.mobile1 {
  background-color: currentColor;
  position: absolute;
  left: 0;
  top: 12px;
  transition: top 250ms ease, transform 250ms ease 250ms;
  transform-origin: center;
  width: 100%
}

.mobile2 {
  top: 0;
  width: 100%;
}

.mobile2 {
  transform: rotate(-45deg);
}
<button class="button-one">
  <div class="mobile1"></div>
</button>

任何帮助我们都会很感激.

推荐答案

需要更新mobile1类的transition css属性,并为mobile2类添加transition属性,因为当类更改(用户被单击)时,它应该反向移动.

作为解释;

  • 当用户第一次点击它时,首先移动该行,然后对其进行变换
  • 当用户第二次点击它时,首先变换线条,然后移动它

let a = document.getElementsByClassName("mobile1")[0];

document.querySelector(".button-one").addEventListener("click", () => {
    a.classList.toggle("mobile2");
})
    button{
        background: transparent;
        border:3px #000 solid;
        border-radius: 0.25rem;
        position:absolute;
        left: 20px;
        top:20px
    }
    
    .button-one{
        align-items: center;
        display: flex;
        height: 21px;
        width: 30px;
    }
    
    .mobile1 {
        background-color: black;
        height: 3px;
        width: 100%
    }
    
    
    .mobile1{
        background-color: currentColor;
        position:absolute;
        left:0;
        top:12px;
        transition: transform 250ms ease, top 250ms ease 250ms; **
        transform-origin: center;
        width:100%
    }
    
    .mobile2 {
        top:0;
        width:100%;
    }
    
    .mobile2 {
        transform:rotate(-45deg);
        transition: top 250ms ease, transform 250ms ease 250ms; **
    }
<button class="button-one">
                <div class="mobile1">
                </div>
</button>

Javascript相关问答推荐

在JavaScript中逐一播放随机生成的音频文件数组

除了在Angular 16中使用快照之外,什么是可行且更灵活的替代方案?

在NextJS中使用计时器循环逐个打开手风琴项目?

React对话框模式在用户单击预期按钮之前出现

从PWA中的内部存储读取文件

如何解决CORS政策的问题

如何解决chrome—extension代码中的错误,它会实时覆盖google—meet的面部图像?'

如何找出摆线表面上y与x相交的地方?

当运行d3示例代码时,没有显示任何内容

如何从调整大小/zoom 的SVG路径定义新的d属性?""

Chart.js 4.4.2,当悬停在一个数据点上时,如何在工具提示中拥有多个数据点/标签?

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

如何将多维数组插入到另一个多维数组中?

不能将空字符串传递给cy.containes()

确保函数签名中的类型安全:匹配值

如何在Angular拖放组件中同步数组?

<;img>;标记无法呈现图像

如何修复错误&语法错误:不能在纯react 项目中JEST引发的模块&之外使用导入语句?

如何使用puppeteer操作所有选项

bootstrap S JS赢得了REACT中的函数/加载