Monday, December 25, 2023
HomeEmail MarketingRestart CSS Animations in Electronic mail

Restart CSS Animations in Electronic mail


Start and Pause CSS Animation

restarting CSS techniques

This text exhibits you a method to restart CSS animations with a single click on or faucet utilizing the :energetic pseudo-class.

There are two sorts of CSS animations, those who solely run as soon as, and people than run constantly. Some CSS animations that run in loops could also be distracting to the recipient of your electronic mail so that you may solely need to run an animation as soon as. Nonetheless, it could be helpful if the recipient got an choice to replay the animation on demand.

Though the CSS animation-play-state property helps paused and working, it there is no such thing as a property that makes the animation “restart”. On the net, restarting animations generally contain the usage of Javascript. Sadly we don’t have such a luxurious with electronic mail.

Fortunately there’s a easy approach to restart CSS animations that works in electronic mail purchasers.

My code snippets use the -webkit prefix. It is because for the most half animations solely work in Webkit primarily based purchasers.

Utilizing the :energetic pseudo-class

The important thing to restarting a CSS animation is to set the animation-name of an animation to ‘none’ after which setting it again to the unique animation.

As you may see this requires two steps. One to change it to a distinct animation and one to set it again. You want a mechanism that may toggle these states in a single motion. Though checkboxes can be utilized to toggle between two states, for those who used a checkbox you’d must faucet twice.

We have now two pseudo-classes that match our wants: :hover and :energetic. Each of those selectors solely function both whereas the cursor is over a component or when the consumer is clicking on a component. Since our purpose is to restart an animation when the consumer clicks on a “restart” button, the :energetic pseudo-class is most popular.

Right here’s the code to play an animation:

<model>
.field{
  -webkit-animation: launch 2s; 
}
@-webkit-keyframes launch{
  from {
    remodel:translateX(0px);
  }
  to{
    remodel:translateX(250px);
  }
}
</model>
<a href="#r" id="replay">replay animation</a>
<div class="anim-container">
<div class="field"></div>
</div>

In our instance, we set the animation property to none when a consumer is clicking on the “replay” button. When the consumer releases the button, the :energetic selector turns into inactive and the animation reverts again to the unique animation, thus restarting the animation.

#replay:energetic + .anim-container .field{
  -webkit-animation-name:none!vital;
}

Replaying CSS animations

View code instance

 

Assist for :energetic in electronic mail purchasers

Curiously :energetic has a large assist amongst Webkit primarily based electronic mail purchasers together with iOS.

The peculiar factor about iOS is that the :energetic selector solely fires briefly when a component is tapped. Not like on a browser, the place the :energetic selector stays energetic so long as the factor in clicked, in iOS tapping and holding a button won’t extend the :energetic selector. That’s why for the longest time, there was little or no worth in utilizing the :energetic selector in interactive electronic mail.

Nonetheless, for the aim of restarting animations, this temporary firing of the selector is ideal!

Outlook for iOS quirk

Outlook on iOS has a quirk the place clicking on a hyperlink with a named anchor will trigger Outlook to launch the online browser. One methodology to handle the Outlook on iOS quirk is to rapidly transfer the factor away when the :energetic selector is fired stopping the precise hyperlink from firing and opening the browser.

This may be completed through the use of the CSS translate remodel to maneuver the factor 1000px to the left when the :energetic selector is fired. Word CSS transforms can solely be utilized to dam degree parts.

#replay:energetic{
  remodel:translateX(-1000px);
}

Alternatively, you need to use a picture because the set off as an alternative. The default iOS Mail consumer will solely hearth the :hover or :energetic selectors when utilized to hyperlinks, photos or labels.

Association of parts

As you’ve most likely seen, this method requires that the replay set off is positioned earlier than the animated factor within the code, for the reason that sibling selector can solely goal parts which can be forward and never behind itself.

What if you wish to place the set off after the animated factor? The best approach to obtain that’s to both use absolute positioning or CSS transforms to visually place your set off after the animated factor on the e-mail. (Have in mind the Samsung Android consumer doesn’t assist absolute positioning).

Replaying CSS animations


View code instance

Beginning animations manually

Typically you don’t need an animation to robotically begin. This may be completed through the use of the :checked state utilizing a label that targets a radio factor. Clicking the label the primary time will activate the :checked state of the radio factor inflicting the animation to run as soon as. By concentrating on the label utilizing the :energetic selector, clicking the label once more will trigger the animation state to change and swap again, and thus restarting the animation.

<model>
#cbox:checked + .anim-container .field{
  -webkit-animation: launch 2s; 
}
#replay:energetic + .field{
  -webkit-animation-name: none!vital;
}
</model>

<enter kind="radio" id="cbox">
<div class="anim-container">
  <label for="cbox" id="replay">play animation</label>
  <div class="field"></div>
</div>

Playing CSS animations

View code instance

Remember the fact that electronic mail purchasers similar to Outlook on iOS and Mac (and sure electronic mail service suppliers like MailChimp) don’t assist type parts in electronic mail, so that you received’t be capable of apply this trick.

Be sure to conceal the checkboxes in your electronic mail. Right here’s an article that goes into the small print of hiding type parts.

Examples

CSS animation Example

View electronic mail instance

A cool use of a replay button is to permit the recipient to replay an intro animation in an electronic mail. Right here’s an electronic mail that performs fireworks with a recipient opens the e-mail. The replay button permits the recipient to benefit from the fireworks as many instances with out having to reopen the e-mail.

One other use of a replay button is to play animations which may be embedded additional down the e-mail content material (under the fold), by having a replay button recipients will be capable of begin the animation when they’re able to view the animation.

Fallbacks

When working with interactivity and animations in electronic mail, it is advisable make sure the expertise on much less succesful purchasers are usually not negatively impacted, so you should definitely plan for these purchasers as effectively. This text goes into how one can show fallback content material for purchasers that don’t assist these options.

Testing on the multitudes of electronic mail purchasers is the place Electronic mail on Acid can prevent a ton of time. Recover from 70 previews in only one minute by testing with Electronic mail on Acid.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments