我里面有一个 div 元素 我有一些链接。我希望 div 的颜色在鼠标悬停时发生变化,而不是整个宽度的长度,而只是 div 的 25%。
当我使用悬停属性时,它会改变整个 div 的颜色。 还请建议我应该放置哪个标签我的悬停属性 - 跨度标签或标签。
如何在颜色变化过程中获得平滑的过渡动画?
.c-label {
display: inline-block;
text-align: left;
}
.cloud-label {
display: inline-block;
float: left;
margin: 5px 0;
opacity: 1;
width: 50%;
line-height: 1.2;
font-size: 120%;
text-align: left;
}
.cloud-label a {
transition: all 0.5s;
background: #000;
border-radius: 3px;
color: #fff;
display: block;
font-size: 14px;
padding: 7px 20px;
position: relative;
margin-left: 20px;
text-decoration: none;
transition: color .15s linear;
-webkit-transition: color .15s linear;
-moz-transition: color .15s linear;
}
.cloud-label a::before {
content: "";
display: block;
border-style: solid;
border-color: transparent #138D89 transparent transparent;
border-width: 15.2px;
width: 0px;
left: 0px;
position: absolute;
margin-left: -29px;
margin-top: -15px;
top: 50%;
}
.cloud-label a::after {
content: "";
display: block;
position: absolute;
width: 8px;
height: 8px;
background-color: #fff;
border-radius: 50%;
top: 50%;
margin-top: -4px;
left: -3px;
}
.cloud-label :hover {
background-color: #138D89;
}
<div class="c-label">
<span class="cloud-label">
<a href="#" rel="nofollow">Gadgets</a>
</span>
</div>
最佳答案
not to full length of width but only 25% of the div
您可以为此使用linear-gradient
。
Also please suggest which tag I should put my hover properties- span tag or a tag
原则上这个不需要额外的标签
How can I get a smooth transition animation during the colour change?
悬停时,需要将属性background-position
从一侧改变到另一侧(例如,从左到右)。
结果
.link {
position: relative;
display: inline-block;
margin-left: 10px;
padding: 5px 10px;
color: #fff;
font-weight: 700;
text-decoration: none;
background: linear-gradient(to left, #138D89 25%, #000 25%) left / 135% 100% no-repeat;
transition: 0.3s;
}
.link:hover {
background: linear-gradient(to left, #138D89 25%, #000 25%) right / 135% 100% no-repeat;
}
.link::before {
content: "";
position: absolute;
top: 0;
right: 100%;
border-right: 15px solid #138D89;
border-top: 13px solid transparent;
border-bottom: 15px solid transparent;
}
.link::after {
content: "";
position: absolute;
top: 50%;
left: 0;
transform: translate(-50%, -50%);
width: 10px;
height: 10px;
box-sizing: border-box;
background: #fff;
border-radius: 50%;
}
<a class="link" href="">Lorem ipsum dolor sit amet.</a>
CodePen 上的代码相同
关于css 悬停颜色从右边变化到 div 的 25%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58147775/