首页 > 解决方案 > 使用 css 淡出和在部分文本中的文本动画

问题描述

我正在尝试为一些文本设置动画,但在获得我想要的结果时遇到了一些问题。我有一首诗(每个句子在自己的 div 中),我希望淡出,然后在悬停时淡入,保持可见一段时间,然后再次淡出。有点像寻找文本并向用户提示悬停位置的游戏。

这是我到目前为止的代码:

.poem {
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}

.hide {
opacity: 0;
transition: all ease 15s;
}

.hide:hover {
border: 1px solid black;
opacity: 1;
}

.hint {
-webkit-animation: fadeinout 30s linear forwards;
animation: fadeinout 5s linear forwards;
}

@-webkit-keyframes fadeinout {
0%,100% { opacity: 0; }
50% { opacity: 1; }
}

@keyframes fadeinout {
0%,100% { opacity: 0; }
50% { opacity: 1; }
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/css?family=Cardo" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<title>Opacity 1</title>
</head>
<body>
<div id="wrapper">
    <div class="hint poem hide">I have emotions</div>
    <div class="poem hide">that are like newspapers that</div>
    <div class="poem hide">read themselves.</div>
    <div class="poem hide">I go for days at a time</div>
    <div class="poem hide">trapped in the want ads.</div>
    <div class="poem hide">I feel as if I am an ad</div>
    <div class="poem hide">for the sale of a haunted house:</div>
    <div class="poem hide">18 rooms</div>
    <div class="poem hide">$37,000</div>
    <div class="poem hide">I'm yours</div>
    <div class="poem">ghosts and all.</div>
    <div class="poem hide">Richard Brautigan</div>
</div>
</body>
</html>

到目前为止,我只用 css 尝试过,但我也欢迎任何 javascript 建议!

任何帮助将不胜感激!

提前致谢 !

标签: javascriptcssanimationtext

解决方案


请检查这个working example

$(window).ready(()=>{
  // fade in the hint text
  $('.poem-hint').toggleClass('hide');
  // fade out the hint text after 3 seconds (3000ms)
  setTimeout(()=>{
     $('.poem-hint').toggleClass('hide');
  }, 3000);  
  
  $('.poem').hover((e)=>{
    $(e.target).toggleClass('hide');
  })
});
.poem {
    transition: opacity 1500ms ease-in-out;
    opacity: 1;
}

.poem:hover {
  border: solid thin black;
}

.hide {
   opacity: 0;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="container text-center p-4">
    <div class="poem poem-hint hide">I have emotions</div>
    <div class="poem hide">that are like newspapers that</div>
    <div class="poem hide">read themselves.</div>
    <div class="poem hide">I go for days at a time</div>
    <div class="poem hide">trapped in the want ads.</div>
    <div class="poem hide">I feel as if I am an ad</div>
    <div class="poem hide">for the sale of a haunted house:</div>
    <div class="poem hide">18 rooms</div>
    <div class="poem hide">$37,000</div>
    <div class="poem hide">I'm yours</div>
    <div class="poem">ghosts and all.</div>
    <div class="poem hide">Richard Brautigan</div>
</div>

如果您希望文本更长时间可见,只需更改1500ms为满足您要求的数字即可:)


推荐阅读