Html 如何在纯 css 导航栏上获得三角形悬停效果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8841301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:31:24  来源:igfitidea点击:

How do you get a triangle hover effect on a pure css navbar?

htmlcsscss-shapes

提问by Bramble

I would like to have a little triangle underneath the the text that points up when the user hovers over the different tabs. Here is a bit of code I'm working with.

当用户将鼠标悬停在不同的选项卡上时,我希望在文本下方有一个小三角形。这是我正在使用的一些代码。

css navbar

css导航栏

* {
  margin: 0;
  padding: 0;
  list-style: none;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 11px;
  margin: 10px;
}

.tab {
  width: 100%;
  padding: 5px;
  background: #fff;
  color: #000;
  cursor: pointer;
  font-weight: bold;
  border-bottom: 1px solid black;
  position: relative;
}

.tab:hover {
  background: #a0a0a0;
}

.tab:hover span {
  display: block;
}

.tab_child {
  padding: 15px;
  background: #fff;
}

.selected {
  background: #a0a0a0;
}

.contain * {
  float: left;
  width: 100px;
}

span.triangle {
  background-image: url("http://www.inner.org/torah_and_science/mathematics/images/triangle.gif");
  background-repeat: none;
  display: none;
  height: 14px;
  width: 16px;
  position: absolute;
  top: 25px;
  left: 25%;
}
<div class="contain">
  <div id="one" class="tab selected">Link1</div>
  <div id="two" class="tab">Link2</div>
  <div id="three" class="tab">Link3</div>
  <div id="four" class="tab">Link4</div>
  <div id="five" class="tab">Link5</div>
</div>

回答by Web_Designer

I think this is probably what you're looking for:

我想这可能是你要找的:

Fiddle

小提琴

Also, please use semantic markup:

另外,请使用语义标记:

  1. If your using HTML5 wrap your navigation in <nav>tags.
  2. Your links (if they really are going to be links) should be <a>elements.
  3. For a list of links like you have it is advised to use a list (<ul>& <li>).
  1. 如果您使用 HTML5 将导航包装在<nav>标签中。
  2. 您的链接(如果它们真的是链接)应该是<a>元素。
  3. 对于像您这样的链接列表,建议使用列表 ( <ul>& <li>)。

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 11px;
}

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  float: left;
  width: 20%;
}

nav a {
  display: block;
  padding: 5px;
  border-bottom: 1px solid black;
  background: #fff;
  color: #000;
  font-weight: bold;
  position: relative;
}

nav a:hover,
.active {
  background: #bbb;
}

nav a:hover:after {
  content: "";
  display: block;
  border: 12px solid #bbb;
  border-bottom-color: #000;
  position: absolute;
  bottom: 0px;
  left: 50%;
  margin-left: -12px;
}
<nav>
  <ul>
    <li><a href="#" class="active">Link1</a></li>
    <li><a href="#">Link2</a></li>
    <li><a href="#">Link3</a></li>
    <li><a href="#">Link4</a></li>
    <li><a href="#">Link5</a></li>
  </ul>
</nav>

回答by Micah

Hereis a modification to your jsfiddle:

是对您的jsfiddle的修改:

I've added a <span class="arrow"></span>to contain the triangles in the HTML:

我添加了一个<span class="arrow"></span>来包含 HTML 中的三角形:

<div class="tab_container">
    <div id="test1-header" class="accordion_headings header_highlight" >Home<span class="arrow"></span></div>
    <div id="test2-header" class="accordion_headings" >About<span class="arrow"></span></div>
    <div id="test3-header" class="accordion_headings" >Work<span class="arrow"></span></div>
    <div id="test4-header" class="accordion_headings" >Social<span class="arrow"></span></div>
    <div id="test5-header" class="accordion_headings" >Contact<span class="arrow"></span></div>
</div>

Here are the changes made to your menu which reduce the size of the triangle and position them at the bottom center of each menu item when hovered over: CSS:

以下是对您的菜单所做的更改,它们减小了三角形的大小,并将它们悬停在每个菜单项的底部中心:CSS:

/*
.accordion_headings:hover{
    background:#00CCFF;
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid red;
}
*/
.accordion_headings{
    position:relative;
}
.accordion_headings .arrow{
    display:none;
}
.accordion_headings:hover .arrow{
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid red;

    display:block;
    position:absolute;
    bottom:0;
    left:49%;
}

回答by kwelch

Here is a fiddlethat uses an background-image that will display over the hovered menu item. It not pretty but further css should help with that.

这是一个使用背景图像的小提琴,它将显示在悬停的菜单项上。它不漂亮,但进一步的 css 应该对此有所帮助。

UPDATE

更新

Sorry I must have misread that. Here is a working fiddlewith a smaller arrow pointing in the proper direction.

对不起,我一定是误读了。这是一个带有指向正确方向的较小箭头的工作小提琴