Hey there! I have a heading (h1) and a button on my webpage. I've created a cool animation using CSS that I want to apply to the h1 when the button is clicked using JQuery.
The idea is that when the button is clicked, the animation should add more content to the h1 element. The problem is, currently the animation starts as soon as the HTML loads, but I want it to start only when the button is clicked.
Since I'm still learning these skills, I find complex answers confusing. Could someone please guide me through this?
Below is the snippet of HTML code:
<div class="container-fluid center" id="mainPage">
<div id="heading" class="row">
<h1 id="wishH1">NAME!</h1>
</div>
</div>
<div class="container-fluid center" id="gift">
<button type="button" id="giftButton">
<img id="giftImg" src="gift.png">
</button>
</div>
And here's how the CSS looks like:
@keyframes wish {
25% {
content: : "hi ";
}
50% {
content: : "hello ";
}
75% {
content: "hola ";
}
}
#wishH1::before {
content: "hey there ";
animation: wish 20s linear infinite;
}
Additionally, this is the JQuery script I've tried:
I've made multiple attempts with different comments in the code block below but none seem to work. They either don't trigger the animation or start automatically without waiting for the button click.
$('#giftButton').click(function() {
$("#gift").fadeOut(1500);
// Various attempts to start the animation after clicking the button
/*$("#wishH1").css("animation",wish);
$("#wishH1").css("animation-duration",6s);
$("#wishH1").css("animation-timing-function",linear);
$("#wishH1").css("animation-iteration-count",infinite);*/
setTimeout(function() {
$("#gift").fadeIn(1500);
}, 20000);
});
This is how the h1 changes during the animation:
hey there NAME!
hi NAME!
hello NAME!
hola NAME!
If anyone can help me understand what adjustments need to be made in the JQuery code or if any changes are required in the CSS for the desired output, I would greatly appreciate it. Thank you!
Also, apologies if my question isn't clear enough, it's my first time asking one. Thank you for your patience.