3

I am having trouble trying to stop and start animation whenever I hover over the element or on click. This is my HTML element:

<div class='marquee'>
   <h1>Some Text</h1>
</div>

CSS:

.marquee {
    width: 80%;
    white-space: nowrap;
    overflow: hidden;
    box-sizing: border-box;
}

.marquee h1 {
    display: inline-block;
    padding-left: 100%;
    animation: marquee 15s linear infinite;
}

@keyframes marquee {
    0%   { transform: translate(0, 0); }
    100% { transform: translate(-100%, 0); }
}

Right now, the animation just goes on forever by default. How can I have it so that whenever I hover over or click on the h1 or the div, the animation will pause and resume accordingly. I'm assuming to pause/restart the animation on click, I would need a JavaScript function. Any help is appreciated.

4
  • 2
    Does this answer your question? How to pause and resume CSS3 animation using JavaScript? Commented Apr 3, 2020 at 23:54
  • @OmriAttiya I'm not using webkit so I don't think that'll solve it Commented Apr 4, 2020 at 0:21
  • @Mr.ZZ I'm not using a <marquee> tag since it became obsolete Commented Apr 4, 2020 at 0:21
  • @RedApple you don't need the webkit prefixes anymore, but that's still the same, JS style.animationPlayState and CSS animation-play-state. Commented Apr 4, 2020 at 2:28

2 Answers 2

6

You can use animation-play-state rule:

.marquee {
    width: 80%;
    white-space: nowrap;
    overflow: hidden;
    box-sizing: border-box;
}

.marquee h1 {
    display: inline-block;
    padding-left: 100%;
    animation: marquee 15s linear infinite;
}

.marquee h1:hover {
    animation-play-state: paused;
}

@keyframes marquee {
    0%   { transform: translate(0, 0); }
    100% { transform: translate(-100%, 0); }
}
<div class='marquee'>
   <h1>Some Text</h1>
</div>

Or with JS:

function stopAnimation(e){
   if (e.target.style.animationPlayState == ""){
     e.target.style.animationPlayState = "paused";
   } else {
     e.target.style.animationPlayState = "";
   }
}
.marquee {
    width: 80%;
    white-space: nowrap;
    overflow: hidden;
    box-sizing: border-box;
}

.marquee h1 {
    display: inline-block;
    padding-left: 100%;
    animation: marquee 15s linear infinite;
}


@keyframes marquee {
    0%   { transform: translate(0, 0); }
    100% { transform: translate(-100%, 0); }
}
<div class='marquee'>
   <h1 onclick="stopAnimation(event)">Some Text</h1>
</div>

Sign up to request clarification or add additional context in comments.

10 Comments

That worked for hover! Thank you! I was wondering how to do this for onClick as well.
I update my answer
Hi, So I'm using React so the div and the h1 are returned by a React component so I can't use "this" for the "stopAnimation(this)" for the onClick since that "this" would point to the whole React Component rather than just the h1.
It works now. I removed "event" from onClick={this.stopAnimation}. Thank you!
Hi, I just posted a new question for that. Please check it out.
|
1

I would suggest using jquery to change the animation property.

$('.marquee').mouseenter(() => { $(this).css("animation: marquee 15s linear infinite"); } );

$('.marquee').mouseleave(() => { $(this).css("animation: none"); } );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.