平滑切换 Javascript 功能

发布于 2025-01-10 05:41:52 字数 534 浏览 5 评论 0原文

好吧,我想做的是点击时切换菜单,但它不平滑,感觉很难,我是 JS 新手,但我对 CSS 和 HTML 足够了解,所以有没有办法平滑这个切换功能?

未单击的菜单:

menu unclicked

单击的菜单:

const toggleButton = document.getElementsByClassName("nav__toggle-button")[0];
const navbarLinks = document.getElementsByClassName("nav__links")[0];

toggleButton.addEventListener("click", () => {
    console.log("clicked");
    navbarLinks.classList.toggle("active");
    toggleButton.classList.toggle("open");
});

Well what I want to do is to toggle a menu when is clicked but it's not smooth and it feels tough, I'm a newbie in JS but I do know CSS and HTML well enough, so is there a way to smooth this toggle function?

menu unclicked:

menu unclicked

menu clicked:

menu clicked

const toggleButton = document.getElementsByClassName("nav__toggle-button")[0];
const navbarLinks = document.getElementsByClassName("nav__links")[0];

toggleButton.addEventListener("click", () => {
    console.log("clicked");
    navbarLinks.classList.toggle("active");
    toggleButton.classList.toggle("open");
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦毁影碎の 2025-01-17 05:41:52

如果你想用CSS解决这个问题,你可以使用transitions属性对两个div进行“动画”处理: https ://www.w3schools.com/css/css3_transitions.asp

关闭状态:

div {
  opacity: 0;
  transition: opacity 1s;
}

打开状态:

div.active {
  opacity: 1;
  transition: opacity 1s;
}

两个未成年人:

  1. 不要使用 BEM 类来触发事件侦听器,而是使用适当的类(js-click 或其他东西..)

  2. 前两行的小重构:

const [toggleButton] = document.querySelectorAll(".nav__toggle-button")
const [navbarLinks] = document.querySelectorAll(".nav__links")

If you want to solve this with CSS you can 'animate' the two divs with the transitions property: https://www.w3schools.com/css/css3_transitions.asp

close state:

div {
  opacity: 0;
  transition: opacity 1s;
}

open state:

div.active {
  opacity: 1;
  transition: opacity 1s;
}

Two minors:

  1. don't use BEM classes to trigger an event listener, use instead a proper class (js-click or something..)

  2. a small refactor for your first two lines:

const [toggleButton] = document.querySelectorAll(".nav__toggle-button")
const [navbarLinks] = document.querySelectorAll(".nav__links")
Bonjour°[大白 2025-01-17 05:41:52

您可以通过 CSS 将过渡和变换属性应用于元素。
例如,如果您使用下拉菜单并控制幻灯片和不透明度:

transform: translateY(-10px);
transition: opacity 150ms ease-in-out, transform 150ms ease-in-out;

您可以查看:
https://developer.mozilla.org/en-US/docs/网页/CSS/过渡

You can apply transition and transform properties to the element through CSS.
For example, if you are using a drop down menu and controlling the slide and the opacity:

transform: translateY(-10px);
transition: opacity 150ms ease-in-out, transform 150ms ease-in-out;

You could check out:
https://developer.mozilla.org/en-US/docs/Web/CSS/transition

漫雪独思 2025-01-17 05:41:52

您所需要的只是一个可以切换的过渡和变换属性。 Transform CSS 属性用于处理 DOM 元素的尺寸、方向等。添加过渡会增加变换属性如果更改则逐渐变化的效果。

const closeButton = document.getElementById("close")

closeButton.addEventListener("click", () => {
  const menu = document.getElementById("nav-links")

  menu.classList.toggle("closed-list");
})
ol {
  width: 100%;
  list-style-type: none;
  background: gray;
  transition: all 0.4s ease-in-out;
}

.closed-list {
  transform: scaleY(0);
  transform-origin: top;
}

li {
  text-align: center;
  padding: 12px 0px;
  color: white;
  font-weight: 700;
  font-size: 18px;
}

#close-container {
  text-align: right;
}
<div>
  <div id="close-container">
    <button id="close">
      open/close
    </button>
  </div>
  <ol id="nav-links">
    <li>Test 1</li>
    <li>Test 2</li>
    <li>Test 3</li>
    <li>Test 4</li>
  </ol>
</div>

All you need is a transition and transform property that you can toggle. Transform CSS property is used for handling dimensions, orientation etc of a DOM element. Adding transition adds an effect where the transform properties if changed, change gradually.

const closeButton = document.getElementById("close")

closeButton.addEventListener("click", () => {
  const menu = document.getElementById("nav-links")

  menu.classList.toggle("closed-list");
})
ol {
  width: 100%;
  list-style-type: none;
  background: gray;
  transition: all 0.4s ease-in-out;
}

.closed-list {
  transform: scaleY(0);
  transform-origin: top;
}

li {
  text-align: center;
  padding: 12px 0px;
  color: white;
  font-weight: 700;
  font-size: 18px;
}

#close-container {
  text-align: right;
}
<div>
  <div id="close-container">
    <button id="close">
      open/close
    </button>
  </div>
  <ol id="nav-links">
    <li>Test 1</li>
    <li>Test 2</li>
    <li>Test 3</li>
    <li>Test 4</li>
  </ol>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文