【问题标题】:css hover color change from right till 25% of the divcss 悬停颜色从右侧变化到 div 的 25%
【发布时间】:2020-01-28 13:27:40
【问题描述】:

我里面有一个 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>

【问题讨论】:

  • 你想要 25% 在 div 的左边还是右边?

标签: css animation hover


【解决方案1】:

不是全长,而是 div 的 25%

您可以为此使用linear-gradient

另外请建议我应该将悬停属性放在哪个标签 - 跨度标签或标签

原则上,这不需要额外的标签

如何在颜色变化过程中获得平滑的过渡动画?

悬停时,需要将属性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%;
}
&lt;a class="link" href=""&gt;Lorem ipsum dolor sit amet.&lt;/a&gt;

CodePen 上的代码相同

【讨论】:

  • @subhro ,如果答案有帮助,您可以用解决方案标记它:prnt.sc/pc7nus
猜你喜欢
  • 2012-03-05
  • 2018-08-28
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 2014-02-24
  • 1970-01-01
相关资源
最近更新 更多