简体   繁体   English

如何在导航上创建平滑过渡

[英]How to create smooth transition on nav

I have a working navigation, but I want the transition between switching dropdown menus to be smoother. 我的导航正常,但我希望切换下拉菜单之间的过渡更加顺畅。 Right now, there is an uncomfortable 'blinking' when you hover between the various main menu links. 现在,当您在各个主菜单链接之间悬停时,会出现不舒服的“闪烁”。 I know it is caused because of the transition time, but I'm not sure how to fix it. 我知道这是由于过渡时间引起的,但我不确定如何解决。 I want the transition to be seamless, as if only the dropdown items themselves are changing (not the background). 我希望过渡是无缝的,好像只有下拉菜单项本身在变化(而不是背景)。

Below is my code, and here is a link to a CodePen ( https://codepen.io/zp12345/pen/mQzvXr ). 下面是我的代码,这是指向CodePen( https://codepen.io/zp12345/pen/mQzvXr )的链接。 Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

HTML HTML

<div class="nav-links">
  <ul class="nav-primary" id="nav-primary">
    <li class="nav-item-top">
      <a href="#link">
        <span class="nav-item-label">Item One</span>
      </a>
      <ul class="nav-dropdown">
        <li class="nav-dropdown-item">
          <a href="#link">
            <h5>1.1</h5>
          </a>
        </li>
        <li class="nav-dropdown-item">
          <a href="#link">
            <h5>1.2</h5>
          </a>
        </li>
      </ul>
    </li>
    <li class="nav-item-top">
      <a href="#link">
        <span class="nav-item-label">Item Two</span>
      </a>
      <ul class="nav-dropdown">
        <li class="nav-dropdown-item">
          <a href="#link">
            <h5>2.1</h5>
          </a>
        </li>
        <li class="nav-dropdown-item">
          <a href="#link">
            <h5>2.2</h5>
          </a>
        </li>
      </ul>
    </li>
    <li class="nav-item-top">
      <a href="#link">
        <span class="nav-item-label">Item Three</span>
      </a>
      <ul class="nav-dropdown">
        <li class="nav-dropdown-item">
          <a href="#link">
            <h5>3.1</h5>
          </a>
        </li>
        <li class="nav-dropdown-item">
          <a href="#link">
            <h5>3.2</h5>
          </a>
        </li>
      </ul>
    </li>
    <li class="nav-item-top">
      <a href="#link">
        <span class="nav-item-label">Item Four</span>
      </a>
      <ul class="nav-dropdown">
        <li class="nav-dropdown-item">
          <a href="#link">
            <h5>4.1</h5>
          </a>
        </li>
      </ul>
    </li>
  </ul>
</div>

SCSS SCSS

.nav-primary {
  display: flex;
  flex-grow: 1; 
  justify-content: center;
  list-style-type: none;
  padding-left: 0;
}
.nav-item-top .nav-item-label {
  color: #383838;
  font-size: 18px;
  padding: 0 24px;
  cursor: pointer;
}
.nav-item-top {
  &:hover {
    .nav-item-label {
      color: #319644;
    }
    .nav-dropdown {
      visibility: visible;
      opacity: 1;
      padding: 16px 0;
    }
  }
}
.nav-dropdown {
  width: 100%;
  left: 0;
  position: fixed;
  top: 60px;
  transition: .2s;
  opacity: 0;
  z-index: 3;
  padding: 0;
  background-color: #133751;
  color: #133751;
  display: flex;
  align-items: center;
  justify-content: center;
  visibility: hidden;
  list-style-type: none;
  .nav-dropdown-item {
    transition: .2s;
    padding: 12px 24px;
    text-align: center;
    color: #fff;
    cursor: pointer!important;
  }
  h5 {
    color: #fff;
    margin: 0;
    text-transform: none;
    font-size: 16px;
  }
}
.nav-dropdown-item {
  a {
    transition: all 0.2s;
    text-decoration: none;
  }
}

In your styles you can adjust the .nav-dropdown transition property to a higher time to make it seem more smooth. 您可以在样式中将.nav-dropdown transition属性调整为更长的时间,以使其看起来更加平滑。

.nav-dropdown {
  transition: 2s;
}

As mentioned by @mike-tung, it is about timing of the transition. 如@ mike-tung所述,这与过渡的时机有关。 You could use an opacity effect on nav-dropdown with low transition timing. 您可以在过渡时间较短的导航下拉列表上使用不透明效果。 It would run smoothly. 它将运行平稳。

.nav-hover:hover {
  .nav-dropdown {
    transition: 0.3s ease;
    opacity: 1;
  }
}

.nav-dropdown {
  transition: 0.2s ease;
  opacity: 0;
  ... 
}

And I see an issue that when you hover out the nav bar, the dropdown is immediately hidden, making it virtually impossible for user to access it. 我看到一个问题,当您将鼠标悬停在导航栏上时,该下拉菜单立即被隐藏,从而使用户几乎无法访问它。

Thus, I'd suggest you to reduce the top attribute ov nav-dropdown, so it stays right below the NAV menu, and then set the background color only to your .nav-dropdown-item. 因此,我建议您减少top属性ov nav-dropdown,使其保持在NAV菜单的正下方,然后仅将背景色设置为.nav-dropdown-item。

.nav-dropdown-item {
    width: 100%;
    background-color: #133751;
... 
}

And giving a padding top to .nav-dropdown: 并给.nav-dropdown填充顶部:

padding: 20px 0 0; // I let the 0s to not mess with your original padding: 0;

The results would be something like this: https://codepen.io/annabranco/pen/QJYWvm?editors=1100 结果将是这样的: https : //codepen.io/annabranco/pen/QJYWvm?editors=1100

Hope it helps! 希望能帮助到你! :) :)

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

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