Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Really odd Safari bug with @Keyframes on my devices

Hello everyone, I hope your Saturday night is going great! I've ran into a really really weird bug in Safari, I'll tell you the story about it here:

A few months back, I got an assigment from my web developer teacher to make a site with modern and advanced web technologies, and I deicided to make a re-designed landing page for the code editor 'Coda'. It's been going great so far, but a few days ago I implented a @Keyframe animation for an SVG file. It works great in Coda on my Mac, but not in Safari? I first thought that I made a mistake somewhere, but I've added both @Keyframes with and without the -webkit- prefix, and then I saw that it didn't work on any of my mobile devices either.. I'll show my code here:

@-webkit-keyframes color_change {
0% { fill: #41B9D1; }
20% { fill: #9BFF1B; }
40% { fill: #F27E18; }
60% { fill: #FFD700; }
80% { fill: #FF0066; }
100% { fill: #41B9D1; }
}

@keyframes color_change {
0% { fill: #41B9D1; }
20% { fill: #9BFF1B; }
40% { fill: #F27E18; }
60% { fill: #FFD700; }
80% { fill: #FF0066; }
100% { fill: #41B9D1; }
}

@-webkit-keyframes color_change_2 {
0% { fill: #127093; }
20% { fill: #FF0066; }
40% { fill: #FFD700; }
60% { fill: #F27E18; }
80% { fill: #9BFF1B; }
100% { fill: #127093; }
}

@keyframes color_change_2 {
0% { fill: #127093; }
20% { fill: #FF0066; }
40% { fill: #FFD700; }
60% { fill: #F27E18; }
80% { fill: #9BFF1B; }
100% { fill: #127093; }
}

#Shape2 { /* SVG path I'm changing the fill on */
-webkit-animation: color_change 10s infinite alternate;
animation: color_change 10s infinite alternate;
}

#Shape3 { /* the other SVG path I'm changing the fill on */
-webkit-animation: color_change_2 10s infinite alternate;
animation: color_change_2 10s infinite alternate;
}

And now to the weird part, the animation works fine on ANY other devices except for MY ones.. And that includes an iPhone 6 and a brand new iPad Pro 9.7" running iOS 9.3.1, aswell as a rMBP running OS X 10.11.4 with Safari 9.1. I've tested the site on 4 other iPhones(4S, 5C, 2 6S) and all of them work fine.. Although, the 5C ran the animation the first time and now refuses to play it at just like mine(although mine never displayed it at all). On my Mac I can see that when I mark the SVG with the color animation on the site, it's flickering A LOT.. Here's the link to the animation itself(This one works fine on all devices and doesn't cause any issues): https://jsfiddle.net/hrygw4vn

I've ensured that the animation works separately and @Keyframes works fine on all devices, and yet only MY personal ones has this problem on THIS site... I've tested with and without private mode enabled, aswell as rebooting, clearing cache etc. It's also worth to mention that it works fine in 3rd party browsers such as Puffin and iCab, and also when I open it with the Safari View Controller in different apps(Skype, Tweebot)...

Does anyone have a clue on what's going on?

Thanks!

1 Answer

If you are on Safari 5.1 or Lower there is only partial support for SVG animation, it is very buggy in Safari.

One culprit that tends to mess up SVG animation in Safari is Mobile Zoom.

<meta name="viewport" content="width=device-width, initial-scale=1">

This will set the initial scale to stop safari zooming on page load.

and

<meta name="viewport" content="initial-scale=1, maximum-scale=1">

Will disable zooming all together (which means it will stop users from zooming.

This might help you discover the problem with the blinking of your animation, why it is not loading at all on your devices I do not know, what version of Safari and which IOS device are you testing on that isn't working?

Safari and SVG animations are a nightmare, Safari in general in my opinion is starting to become a bit of a nightmare, I get a new obscure bug with SVG's everytime I test a site on Safari.

Other fixes I have found have worked with previous IOS/Safari SVG problems:

A. Set Positioning relative or absolute on your SVG and it's parent. (I found sometimes SVG animation render off screen when using Safari and this fixed the problem.

B. Declare content type in your html for your SVG

<svg id="shape3" xmlns="http://www.w3.org/2000/svg" version="1.1">

C. In some cases I just gave up and added the SVG to the DOM with Javascript for Safari, this isn't a very graceful fallback though as you will also need a fallback for lack of javascript.

Hi Ashley, thanks for getting back!

As described above, I run the latest version of Safari on all my devices(9.0 iOS, 9.1 OS X). I also have the meta tag for the viewport as Bootstrap requires it.

A. Intresting! I'll definitely try this, it actually sounds like it'll work!

B. If A fails I'll try this aswell.

C. I actually do the same with this neat script

If you'd like to have a look on my code, I've uploaded it to this workspace snapshot

Thanks again for the reply, your first suggestion got my hopes up!

Hi again,

No nothing worked... Have I missed something? If you look at my code(The SVG is in the navbar), where exactly should I put the position: relative? I tried putting it for the classes .navbar-brand and .svg which cover the SVG and the a link, and it didn't help.. Any clue?

Thanks again, I really appreciate it!