简体   繁体   English

在 hover 上将 FontAwesome 图标更改为文本

[英]Change FontAwesome icon to text on hover

How do I replace an icon with text on hover?如何在 hover 上用文本替换图标?

I am writing this site in React, however I have altered it so that it appears correctly in JSFiddle.我正在用 React 编写这个站点,但是我已经对其进行了更改,以便它在 JSFiddle 中正确显示。 When I hover over one of the bubble's, I would like the text from the title="..." of the a tag to show up.当我在其中一个气泡上 hover 时,我希望显示a标签的title="..."中的文本。

For example, when I hover over the GitHub bubble, I would like for "GitHub" to replace the icon that is in the bubble.例如,当我在 GitHub 气泡上 hover 时,我想用“GitHub”替换气泡中的图标。

Here's a JSFiddle这是一个JSFiddle

I have tried adding a <p>GitHub</p> within the a tag and adding css to make it appear on hover.我尝试在a标签中添加<p>GitHub</p>并添加 css 以使其出现在 hover 上。 However, I could not get it to work.但是,我无法让它工作。 Not sure if that was the right approach or if I was setting the css properties incorrectly.不确定这是否是正确的方法,或者我是否错误地设置了 css 属性。

While a pure css solution would require you to just add a single span element inside each anchor tag like:虽然纯 css 解决方案需要您在每个锚标记内添加一个跨度元素,例如:

<span class="icon_title">Github</span>

with the following css:使用以下 css:

.bubble .icon_title{
  display:none;
}
.bubble:hover .icon_title{
  display:initial;
}
.bubble:hover .icon{
  display:none;
}

But if you would realy like to use the title attribute for each anchor tag you could also go for this javascript aproach:但是,如果您真的想为每个锚标记使用 title 属性,您也可以使用 go 来实现此 javascript 方法:

var bubbles = document.querySelectorAll('.bubble a'); // Get all anchor tags inside bubbles in an array
for(const bubble of bubbles){ // Loop through each bubble
  let newSpan = document.createElement('span'); //create a bew span element
  newSpan.innerHTML = bubble.title; // Add the title of each anchor tag to the span element
  newSpan.classList.add('icon_title') // Give your new span element a class
  bubble.appendChild(newSpan); // Add the new span element to your bubble
} 

*Note that you still need to add the above css, but creating each span element inside the HTML is no longer needed as this is done by JS, with the appropriate title. *请注意,您仍然需要添加上述 css,但不再需要在 HTML 内创建每个跨度元素,因为这是由 JS 完成的,并带有适当的标题。

Working snippet (font awesome icons not loading properly, but they should in your code):工作片段(字体真棒图标未正确加载,但它们应该在您的代码中):

 var bubbles = document.querySelectorAll('.bubble a'); // Get all anchor tags inside bubbles in an array for(const bubble of bubbles){ // Loop through each bubble let newSpan = document.createElement('span'); //create a bew span element newSpan.innerHTML = bubble.title; // Add the title of each anchor tag to the span element newSpan.classList.add('icon_title') // Give your new span element a class bubble.appendChild(newSpan); // Add the new span element to your bubble }
 .App-header { background-color: #282c34; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(48px + 2vmin); color: white; }.bubble { display: inline-block; background-color: transparent; border: 3px solid gray; margin: 15px; border-radius: 50%; } a { display: table-cell; vertical-align: middle; text-align: center; height: 100px; width: 100px; }.bubble.icon_title{ display:none; }.bubble:hover.icon_title{ display:initial; }.bubble:hover.icon{ display:none; }
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <header className="App-header"> <div> <span class="bubble"> <a title="GitHub" href="https://github.com/{username}" target="_blank " rel="noopener noreferrer"> <i class="icon fab fa-github fa-2x"></i> </a> </span> <span class="bubble"> <a title="LinkedIn" href="https://www.linkedin.com/in/{username}/" target="_blank " rel="noopener noreferrer"> <i class="icon fab fa-linkedin fa-2x"></i> </a> </span> <span class="bubble"> <a title="Resume" href="resume.pdf" target="_blank " rel="noopener noreferrer"> <i class="icon fab fa-linkedin fa-2x"></i> </a> </span> <span class="bubble"> <a title="Email" href="mailto:{email}" target="_blank " rel="noopener noreferrer"> <i class="icon fas fa-envelope fa-2x"></i> </a> </span> </div> </header>

*Just if you are in a hurry, check here for a JS fiddle implementing all that is said below. *如果您赶时间,请在此处查看实现以下所有内容的 JS fiddle。

For a purely CSS solution, here is your best bet对于纯粹的 CSS 解决方案,这是您最好的选择

  • Wrap the <i> tag which holds your icon inside a div<i>标记包含在 div 中,该标记将您的图标包含在内
  • Add another <div> which contains a <span> element that holds the text you want添加另一个<div> ,其中包含一个<span>元素,其中包含您想要的文本

The finished HTML for each link should look something like:每个链接的完成 HTML 应该类似于:

<a title="GitHub" href="https://github.com/{username}" target="_blank " rel="noopener noreferrer">
  <div>
    <i class="icon fab fa-github fa-2x"></i>
  </div>
  <div>
    <span>Github</span>
  </div>
</a>

Then in your stylesheet,然后在你的样式表中,

  • Change the color of your <a> tags to a color other than white so the icon and the text in it is visible<a>标记的颜色更改为白色以外的颜色,以便图标和其中的文本可见
  • Set position: relative on your <a> tags<a>标签上设置position: relative

Then add the following style rules to set the positioning of the elements and the behaviour of the link child elements on hover然后在hover上添加如下样式规则来设置元素的定位和链接子元素的行为

a > div {
  position: absolute;
  top: 0; bottom: 0;
  right: 0; left: 0;
  display: flex;
  border-radius: 100%;
}

a > div > .icon,
a > div > span {
  margin: auto;
  transition: all ease-in-out 250ms;
}

a > div > span {
  opacity: 0;
}

a:hover > div > .icon {
  opacity: 0;
}

a:hover > div > span {
  opacity: 1;
}

You can add more functionality to it but this works.您可以为其添加更多功能,但这有效。

Check here for a JS fiddle implementing all that is said above.在此处查看实现上述所有内容的 JS fiddle。

Best of luck:)祝你好运:)

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

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