简体   繁体   English

如何使用 CSS 和 Javascript 在我的菜单导航栏中添加 animation

[英]How to add animation in my menu navbar usng CSS and Javascript

I want to add animation in to the height from 0% to 45% when clicked我想在单击时将 animation 添加到 0% 到 45% 的高度

HTML: HTML:

<header id="header">
  <div class="contact">
    <ul>
      <li><i class="fas fa-phone"> (914) 296-0044</i></li>
      <li>Address</li>
    </ul>
  </div>
  <div class="header-navigation">
    <ul class="links">
      <li><a href="#">HOME</a></li>
      <li><a href="#">PAINTING</a></li>
      <li><a href="#">POWER WASHING</a></li>
      <li><a href="#">LIGHT FIXTURE REPLACEMNET</a></li>
      <li><a href="#">PLUMBING FIXTURE REPLACEMENT</a></li>
    </ul>
    <i class="fa fa-bars linkShow"></i>
  </div>
</header>

CSS: CSS:

.linkShow {
    display: block;
    position: absolute;
    right: 50%;
    top: 15%;
  }

  .links {
    display: none;
    width: 100%;
    height: 0%;
    text-align: center;
    background: var(--secondaryColor);
    position: relative;
    margin-top: 250px;
    transition: all 0.7s linear;
  }

  .links a {
    display: block;
    padding: 0 140px;
    transition: 0.5s ease-in-out;
  }
  .links a:hover {
    background: var(--mainColor);
    color: black;
  }
  .links li {
    width: 100%;
  }

Javascript: Javascript:

const links = document.querySelector(".links");
const button = document.querySelector(".linkShow");

button.addEventListener("click", function () {
  if (links.style.display === "block") {
    links.style.display = "none";
  } else {
    links.style.display = "block";
  }
});

i tried adding new class and put a height to 45% to show and put a classlist add and remove to the javascript but it didn't work我尝试添加新的 class 并将高度设置为 45% 以显示并将类列表添加和删除到 javascript 但它不起作用

I also tried the transition height.7s linear but it didn't work我也尝试了过渡 height.7s 线性,但没有用

You can done this using CSS.您可以使用 CSS 完成此操作。

Head over to this link - https://css-tricks.com/using-css-transitions-auto-dimensions/前往此链接 - https://css-tricks.com/using-css-transitions-auto-dimensions/

Also check this out - How can I transition height: 0;还要检查一下 - 我如何转换高度:0; to height: auto; 到高度:自动; using CSS? 使用 CSS?

Hope this answers your question.希望这能回答你的问题。

Try to implement using setInterval() function.尝试使用 setInterval() function 来实现。 It will take two parameters i) function name ii) time in milliseconds Then create that function in js code and set the position of your element and at last when element reaches to final position use clearInterval() function to stop further animation of element It will take two parameters i) function name ii) time in milliseconds Then create that function in js code and set the position of your element and at last when element reaches to final position use clearInterval() function to stop further animation of element

Add this code snippet to your js code below the links.style.display=="block";将此代码段添加到您的 js 代码中links.style.display=="block";下方

links.style.transition="3s";

Transitions always add an animation effect to elements onhover and onclick过渡总是为元素 onhover 和 onclick 添加 animation 效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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