首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我将鼠标悬停在我的文本上时,它会改变颜色,而颜色保持不变,我该怎么做呢?

当我将鼠标悬停在我的文本上时,它会改变颜色,而颜色保持不变,我该怎么做呢?
EN

Stack Overflow用户
提问于 2021-10-24 08:16:35
回答 4查看 83关注 0票数 0

我刚接触web开发,似乎不知道如何在悬停时更改文本的颜色,并使其保持不变。目前,我有以下内容:

代码语言:javascript
运行
复制
.main{
    color: white;
    font-size: 20px;
    }

.main:hover{
    animation: textchange 1s cubic-bezier(0.165, 0.84, 0.44, 1);
}
@keyframes textchange{
    from{
        color: white;
    }
    to {
        color:black;
    }
}
代码语言:javascript
运行
复制
<p class=main>
  Lorem ipsum dolor sit amet
</p>

我理解为什么取消悬停后颜色不会保持黑色,但我仍然不知道如何处理这个问题。如果没有javascript,我甚至不确定这是否可能。

EN

Stack Overflow用户

发布于 2021-10-24 08:55:31

您应该使用animation-fill-mode: forwards;

代码语言:javascript
运行
复制
function enter() {
  const main = document.getElementById('main-with-javascript');
  main.style.animation = 'textchange 4s cubic-bezier(0.165, 0.84, 0.44, 1)';
  main.style.animationFillMode = 'forwards';
}

function leave() {
  const main = document.getElementById('main-with-javascript');
  main.style.animation = 'textchangeReverse 4s cubic-bezier(0.165, 0.84, 0.44, 1)';
}
代码语言:javascript
运行
复制
#main-with-javascript {
  color: #dddddd;
  font-size: 20px;
}

.main {
  color: #dddddd;
  font-size: 20px;
  animation: textchangeReverse 4s cubic-bezier(0.165, 0.84, 0.44, 1);
  animation-fill-mode: forwards;
}

.main:hover {
  animation: textchange 4s cubic-bezier(0.165, 0.84, 0.44, 1);
  /*Let the element retain the style values from the last keyframe when the animation ends*/
  animation-fill-mode: forwards;
}

@keyframes textchange {
  from {
    color: #dddddd;
  }
  to {
    color: black;
  }
}

@keyframes textchangeReverse {
  from {
    color: black;
  }
  to {
    color: #dddddd;
  }
}
代码语言:javascript
运行
复制
<h2>with css</h2>
<p class='main'>
  Lorem ipsum dolor sit amet
</p>
<hr/>
<h2>with css and javasctipt</h2>
<p id='main-with-javascript' onmouseenter='enter()' onmouseleave='leave()'>
  Lorem ipsum dolor sit amet
</p>

票数 2
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69694972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档