我在我的 React 元素中使用 react-router-dom 节点包来实现路由。
设置路由器链接后,我使用以下自定义CSS默认隐藏链接下划线:
let styles = theme => ({
TextLink: {
position: 'relative',
color: 'white',
textDecoration: 'none',
'&:hover':{
color: 'white',
},
});
使用它我能够隐藏。
我的目标是制作一个链接,在悬停时显示下划线并具有过渡效果(链接下划线从中心向两端增长)。
修改后的 CSS 或代码示例以及任何额外的节点包会很有帮助。
最佳答案
下面的例子是用纯css完成的。
基本上reactjs中的链接是a
标签,因此你可以使用下面的css
@import url("https://fonts.googleapis.com/css?family=Montserrat:500");
body {
font-family: 'Montserrat', sans-serif;
}
ol,
ul {
list-style: none;
}
li {
display: inline-block;
padding: 20px 0 20px;
}
a {
text-decoration: none;
position: relative;
display: block;
padding: 16px 0;
margin: 0 12px;
font-size: 1.2rem;
text-transform: uppercase;
transition: color 0.1s, background-color 0.1s;
color: #000;
}
a:hover {
color: #4dd0e1;
}
a:focus, a:active {
color: #00bcd4;
}
a::before {
content: '';
display: block;
position: absolute;
top: 100%;
height: 3px;
width: 100%;
background-color: #00bcd4;
-webkit-transform-origin: center top;
transform-origin: center top;
-webkit-transform: scale(0, 1);
transform: scale(0, 1);
transition: color 0.1s, -webkit-transform 0.2s ease-out;
transition: color 0.1s, transform 0.2s ease-out;
transition: color 0.1s, transform 0.2s ease-out, -webkit-transform 0.2s ease-out;
}
a:active::before {
background-color: #00bcd4;
}
a:hover::before,
a:focus::before {
-webkit-transform-origin: center top;
transform-origin: center top;
-webkit-transform: scale(1, 1);
transform: scale(1, 1);
}
<nav>
<ul>
<li class=""><a href="#">home</a></li>
<li class=""><a href="#">career</a></li>
<li class=""><a href="#">projects</a></li>
<li class=""><a href="#">about us</a></li>
<li class=""><a href="#">contact us</a></li>
</ul>
</nav>
关于javascript - 如何在悬停 react 路由器链接时实现显示下划线的过渡效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51719399/