html - 仅使用 CSS 的导航栏动画/过渡

标签 html css css-transitions css-animations navigationbar

所以,我正在尝试为我的网站制作一个漂亮的导航菜单栏动画/过渡,以创建一个光滑的外观效果。我的目标是仅使用 CSS,而不添加任何只会使事情复杂化的 javascript。 This link has the kind of animation我在找。只是给你们一个想法。 (只需浏览 W3 Schools 即可查看我正在寻找的动画)。

这里的想法是将 topnavbackground-color 从一个页面更改为一个平滑的过渡页面。

这是我目前拥有的 CSS 样式。我喜欢我当前的样式,但我只想添加从一个页面到另一个页面的过渡,以便 background-color 以某种方式充当 slider

ul.topnav li a:hover:not(.active) {
  background-color: #4d4dff;
  transition: 0.5s;
}

ul.topnav li a.active {
  background-color: #00cc99;
  color: black;
  /* I would like to add in some sort of animations / transition here. */
}
<ul class="topnav">
  <label for="toggle">&#9776;</label>
  <input type="checkbox" id="toggle">
  <div class="menu">
    <li><a href="#" class="active">Home</a>
      <!--Background when active-->
      <li><a href="#">About</a></li>
      <li><a href="#">Portfolio</a></li>
      <!--If I click on this page, it would transition the background COLOR to this page-->
      <li><a href="#">Gallery</a></li>
      <li><a href="#">Contact Me</a></li>
  </div>
</ul>

编辑对 Muljayan 的回应的评论

在此处查看此示例 https://youtu.be/oCUCWnbre0k?t=1014并遵循此代码。

HTML

* {
  margin: 0;
  padding: 0;
}

.navigation {
  margin: 200px 20px;
  background: #383838;
  color: #FFF;
  overflow: hidden;
  height: 50px;
  width: 1000px;
  box-shadow: 0 2px 10px 2px rgba(0, 0, 0, 0.2);
  -webkit-border-radius: 6px;
  -moz-border-radius: 6px;
  border-radius: 6px;
}

.navigation li {
  width: 100px;
  border-bottom: 1px solid #666;
  border-right: 2px ridge #585858;
  float: left;
  list-style-type: none;
  font-family: 'ambleregular';
  font-weight: normal;
  font-size: 15px;
  line-height: 28px;
  padding: 10px 10px 10px 10px;
  -webkit-transition: all .9s;
  -moz-transition: all .9s;
  -o-transition: all .9s;
  -ms-transition: all .9s;
  transition: all .9s;
}

.navigation li:hover {
  background: #FE980F;
  border-bottom: 1px solid #FFF;
}

.navigation li a {
  text-decoration: none;
  color: #FFF;
}

.navigation li a:hover {
  color: #333;
}


/* Search box    */

.search_box {
  float: right;
  border: 1px solid #3C3C3C;
  background: #FFF;
  border-radius: 0.3em;
  -webkit-border-radius: 0.3em;
  -moz-border-radius: 0.3em;
  -o-border-radius: 0.3em;
  position: absolute;
  margin-top: 8px;
  margin-right: 15px;
  width: 228px;
  left: 765px;
  top: 204px;
}

.search_box form input[type="text"] {
  border: none;
  outline: none;
  background: none;
  font-size: 12px;
  color: #acacac;
  width: 75%;
  padding: 5px;
}


/* Final STEP  */

.search_box form input[type="submit"] {
  border: none;
  cursor: pointer;
  background: url(images/search.png) no-repeat 0px 7px;
  position: absolute;
  right: 0;
  width: 20px;
  height: 25px;
}
<body bgcolor="#faf7fd">
  <ul class="navigation">
    <li><a href="#">Home</a></li>
    <li><a href="#">Products</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>

  <div class="search_box">
    <form>
      <input type="text" value="Search" onfocus="this.value = ''; {this.value='Search' ;}">
      <input type="submit " value=" ">
    </form>
  </div>
</body>

最佳答案

我在 li 上使用了过渡,在 a 标签上使用了动画来实现您需要的效果。在没有 javascript 的每个页面上,您必须手动更改事件页面的类。 示例:在关于页面上,about 标签将具有 lnk 类,所有其他 a 标签将具有 aLnk,这是为了突出显示当前页面。

.menu {
  display: flex;
  align-items: center;
  width: 100%;
  background-color: #333333;
  height: 50px;
  float: left;
}

ul {
  width: 100%;
  float: left;
  color: #ffffff;
}

li {
  width: 100px;
  height: 34px;
  float: left;
  list-style: none;
  background-color: transparent;
  transition: ease-in-out .9s;
  text-align: center;
  padding-top: 10px;
}

li:hover {
  background-color: orange;
  transition: ease-in-out .9s;
}

.aLnk {
  width: 100%;
  height: 100%;
  float: left;
  text-decoration: none;
  color: #ffffff;
  transition: ease-in-out .9s;
}

.aLnk:hover {
  color: yellow;
  transition: ease-in-out .9s;
  animation-name: changeColor;
  animation-duration: .9s;
  animation-iteration-count: infinate;
}

@keyframes changeColor {
  10% {
    color: #000000;
  }
  100% {
    color: yellow;
  }
}

.active {
  background-color: orange;
}

.lnk {
  color: yellow;
  text-decoration: none;
}

.lnk:hover {
  color: yellow;
}
<div class="menu">
  <ul class="topnav">
    <li class="active"><a class="lnk" href="index.html">Home</a></li>
    <li><a class="aLnk" href="about.html">About</a></li>
    <li><a class="aLnk" href="portfolio.html">Portfolio</a></li>

  </ul>
</div>

关于html - 仅使用 CSS 的导航栏动画/过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54339144/

相关文章:

html - 在第三个子菜单后重置 css 属性

CSS 链接问题

css - 在 Wordpress 主题中禁用默认移动菜单 View

javascript - 通过 CSS3 过渡立即淡出和淡入

javascript - 为什么这个 "checked"不起作用?

javascript - 为什么导航栏的宽度不是 100%? (没有 Bootstrap )

html - 不透明度 1 - 元素消失。 HTML CSS

html - css动态图标布局无需刷新,类似amazon网站

css - 如何修复 CSS3 中工具提示的过渡?

css - 从 svg 路径计算 css 缓动函数