Reputation: 87
I'm trying to animate some text and I'm having some problems getting the result that I want. I have a poem (each sentence in its own div) that I wish to fade out, then fade in on hover, remain visible for a while, and then fade out again. Sort of like a game of finding the text and giving the user a hint on where to hover.
Here is the code that I have so far:
.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>
So far I've only tried it with css but I would welcome any javascript suggestions too !
Any help would be extremely appreciated !
Thanks in advance !
Upvotes: 0
Views: 90
Reputation: 716
Please checkout this 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>
If you want the text to stay visible for a longer time, just change 1500ms
to a number that satisfies your requirements :)
Upvotes: 2
Reputation: 1063
Instead of forwards I just ran it one time so it didn't stop on the last frame. Should work:
.hint {
-webkit-animation: fadeinout 5s 1 linear ;
animation: fadeinout 5s 1 linear ;
}
Working example: https://jsfiddle.net/nsf25rz0/2/
Upvotes: 2