在当前页面上填充导航栏链接

时间:2016-09-24 06:12:39

标签: html css

我目前正在尝试创建一个在链接上悬停时填充的导航栏。但是,我还希望在相关页面上填充(或突出显示)链接。任何帮助将不胜感激!

注意:此代码已从开源中检索。我是Web技术的新学习者,并且正在为Web项目工作。



/*NAVIGATION */

body {
  font-family: 'Roboto', sans-serif;
  padding: 0;
  margin: 0;
}
small {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.4);
}
h1 {
  text-align: center;
  padding: 50px 0;
  font-weight: 800;
  margin: 0;
  letter-spacing: -1px;
  color: inherit;
  font-size: 40px;
}
h2 {
  text-align: center;
  font-size: 30px;
  margin: 0;
  font-weight: 300;
  color: inherit;
  padding: 50px;
}
.center {
  text-align: center;
}
section {
  height: 100vh;
}
nav {
  width: 60%;
  margin: 0 auto;
  background: #fff;
  padding: 10px 0;
  box-shadow: 0px 5px 0px #dedede;
}
nav ul {
  list-style: none;
  text-align: center;
}
nav ul li {
  display: inline-block;
}
nav ul li a {
  display: block;
  padding: 15px;
  text-decoration: none;
  color: #aaa;
  font-weight: 800;
  text-transform: uppercase;
  margin: 0 10px;
}
nav ul li a,
nav ul li a:after,
nav ul li a:before {
  transition: all .5s;
}
nav ul li a:hover {
  color: #555;
}
/* stroke */

nav.stroke ul li a,
nav.fill ul li a {
  position: relative;
}
nav.stroke ul li a:after,
nav.fill ul li a:after {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 0%;
  content: '.';
  color: transparent;
  background: #aaa;
  height: 1px;
}
nav.stroke ul li a:hover:after {
  width: 100%;
}
nav.fill ul li a {
  transition: all 2s;
}
nav.fill ul li a:after {
  text-align: left;
  content: '.';
  margin: 0;
  opacity: 0;
}
nav.fill ul li a:hover {
  color: #fff;
  z-index: 1;
}
nav.fill ul li a:hover:after {
  z-index: -10;
  animation: fill 1s forwards;
  -webkit-animation: fill 1s forwards;
  -moz-animation: fill 1s forwards;
  opacity: 1;
}
/* Keyframes */

@-webkit-keyframes fill {
  0% {
    width: 0%;
    height: 1px;
  }
  50% {
    width: 100%;
    height: 1px;
  }
  100% {
    width: 100%;
    height: 100%;
    background: #333;
  }
}

<section style="background: #2ecc71; color: rgba(0, 0, 0, 0.5);">
  <h2>Nav bar test</h2>
  <nav class="fill">
    <ul>
      <li><a href="index.html">Home</a>
      </li>
      <li><a href="#">About</a>
      </li>
      <li><a href="#">Downloads</a>
      </li>
      <li><a href="#">More</a>
      </li>
      <li><a href="#">Contact</a>
      </li>
    </ul>
  </nav>
</section>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

看起来你已经完成了第一个让它在悬停时突出显示的任务。我会在php中这样做,因为这是我最舒服的语言,但是你还必须在你的计算机上运行一个Web服务器,所以希望你从这里的一些Jscript专业人员那里得到更多有用的提示。如果你不得不求助于php,我通常会这样做的一种方法是在页面上设置一个变量,让标题知道你所在的页面,然后是一些if语句,这些语句是coorespond并应用.current-page样式。

我可能做的很长,但我还没有学过Javascript。

if语句在nav ...的{ul}中的if ... <li <?php if ($title == "Home) { echo "class='current-page'"; } ?>><a href="#">Home</a>中看起来像这样,对于ul中的每个元素都有相应的标题,页面正文中的标题声明。

答案 1 :(得分:0)

最佳做法是添加一个&#34; active&#34;类到当前页面按钮。

<li><a href="index.html" class="active">Home</a></li>

并添加&#34; a.active&#34;每一个&#34; a:hover&#34;你已经拥有,所以它有相同的CSS。

nav.fill ul li a:hover, 
nav.fill ul li a.active { /* Here */
  color: #fff;
  z-index: 1;
}
nav.fill ul li a:hover:after, 
nav.fill ul li a.active:after { /* Here */
  z-index: -10;
  animation: fill 1s forwards;
  -webkit-animation: fill 1s forwards;
  -moz-animation: fill 1s forwards;
  opacity: 1;
}

nav.stroke ul li a:hover:after, 
nav.stroke ul li a.active:after { /* Here */
  width: 100%;
}

这是一个jsfiddle现场示例:https://jsfiddle.net/884o5sjs/ (为了更快地获得帮助,请记住下次创建自己的jsfiddle)

你可以添加class =&#34; active&#34;使用php(建议)或甚至jQuery(不建议)动态到当前页面,但因为它看起来像你正在使用&#34; .html&#34;你必须在每个页面上手动添加它。

希望这有帮助。

相关问题