Css3 transitions and animations + graceful degradation with jQuery

69
Graceful degradation with jQuery CSS 3 Transitions and Animations

Transcript of Css3 transitions and animations + graceful degradation with jQuery

Page 1: Css3 transitions and animations + graceful degradation with jQuery

Graceful degradation with jQuery

CSS 3Transitions and Animations

Page 2: Css3 transitions and animations + graceful degradation with jQuery

About me

• Andrea Verlicchi

• Gruppo Euris - Bologna

• Senior Front End Developer & Team Leader in Yoox Group

• www.andreaverlicchi.eu verlok

Page 3: Css3 transitions and animations + graceful degradation with jQuery

NEW IN CSS 3

Page 4: Css3 transitions and animations + graceful degradation with jQuery

NEW IN CSS 3

• New styles

• Transforms

• Transitions

• Animations

Page 5: Css3 transitions and animations + graceful degradation with jQuery

New styles

• border-radius

• box-shadow

• text-shadow

• rgba / hsla

• gradients

Page 6: Css3 transitions and animations + graceful degradation with jQuery

Transforms

• scale

• rotate

• skew

• translate 2D

• translate 3D

Page 7: Css3 transitions and animations + graceful degradation with jQuery

Transitions

Change between states gradually in time

Page 8: Css3 transitions and animations + graceful degradation with jQuery

Animations

Automatic transition through states (keyframes) defined in a time line

Page 9: Css3 transitions and animations + graceful degradation with jQuery

Any doctype

CSS 3 works on any version of x/html HTML 5, HTML 4, XHTML 1, etc.

Page 10: Css3 transitions and animations + graceful degradation with jQuery

BROWSERS

Page 11: Css3 transitions and animations + graceful degradation with jQuery

BROWSER SUPPORT

10+ All All All 12+

IE 9: Basic IE 8, 7, 6: No

Page 12: Css3 transitions and animations + graceful degradation with jQuery

Browser stats (Oct 2012)

CHROME

INTERNET EXPLORER

FIREFOX

SAFARI

OPERA

OTHER

Page 13: Css3 transitions and animations + graceful degradation with jQuery

Browser versions (Oct 12)

IE 9IE 8

IE 7

IE 6

Page 14: Css3 transitions and animations + graceful degradation with jQuery

Mobile browsers (Oct 12)

ANDROID

IPHONEOPERA

NOKIAUC BROWSER

NETFRONTBLACKBERRY

IPODDOLFIN

+SAFARI

Page 15: Css3 transitions and animations + graceful degradation with jQuery

Windows 8 + IE 10 Release

Page 16: Css3 transitions and animations + graceful degradation with jQuery

BROWSERS ARE READY FOR CSS 3

Page 17: Css3 transitions and animations + graceful degradation with jQuery

CSS 3 TRANSITIONS

vsJAVASCRIPT

ANIMATIONS

Page 18: Css3 transitions and animations + graceful degradation with jQuery

Low effort

Page 19: Css3 transitions and animations + graceful degradation with jQuery

BETTER RESULTS

Best performanceGPU acceleration

No queue

Consistent layoutBrowser-triggered

animations

Users don’t have to wait

CSS-driven final state

Asynchronous Zoom, MQ switch

They run on a separate thread

Expecially on mobile devices

Page 20: Css3 transitions and animations + graceful degradation with jQuery

Development time(and maintenance)

Page 21: Css3 transitions and animations + graceful degradation with jQuery

IE 8 + 9 will be around for a while.Do we have to write twice the code

to do the same thing?

Page 22: Css3 transitions and animations + graceful degradation with jQuery

NO

Page 23: Css3 transitions and animations + graceful degradation with jQuery

Progressive enhancement

Do you really want to / need to replicate EVERY effect you CAN create using CSS

transitions?

Page 24: Css3 transitions and animations + graceful degradation with jQuery

Low redundancy

If you really need to replicate an effect, the code redundancy is VERY LOW

Page 25: Css3 transitions and animations + graceful degradation with jQuery

HOW DO WE DO IT?

Page 26: Css3 transitions and animations + graceful degradation with jQuery

A transition is about animating the change of value of one or more element

properties in a given time.

Page 27: Css3 transitions and animations + graceful degradation with jQuery

SO WE NEED TO...

• Consider an element in the DOM• Define what element properties to

transition• Define in how much time the

properties will change• Define a final state for the element

properties

Page 28: Css3 transitions and animations + graceful degradation with jQuery

Using a CSS selector.I.g.: All the links in the page

a {}

CONSIDER AN ELEMENT IN THE DOM

Page 29: Css3 transitions and animations + graceful degradation with jQuery

Using the “transition-duration” property

a { transition-duration: 1s;}

DEFINE IN HOW MUCH TIME THE PROPERTIES CHANGE

Page 31: Css3 transitions and animations + graceful degradation with jQuery

3 possible ways:

• Using an auto-applied pseudo class

• Using a class

• Using Javascript

DEFINE A FINAL STATE FOR THE ELEMENT PROPERTIES

Page 32: Css3 transitions and animations + graceful degradation with jQuery

FINAL STATE WITH A PSEUDO-CLASS

a:hover { color: red;}

The link color will transition to red on mouseover state

Page 33: Css3 transitions and animations + graceful degradation with jQuery

FINAL STATE WITH A CLASS

a.selected { color: black;}

The link color will transition to black when the selected class is applied to it

Page 34: Css3 transitions and animations + graceful degradation with jQuery

FINAL STATE USING JAVASCRIPT

$(‘a’).css(‘color’, ‘blue’);

The link color will transition to blue when this line of code is executed

Page 35: Css3 transitions and animations + graceful degradation with jQuery

DEMO

Page 36: Css3 transitions and animations + graceful degradation with jQuery

WHAT ABOUT OLD IE VERSIONS?

Page 37: Css3 transitions and animations + graceful degradation with jQuery

GRACEFUL DEGRADATION

DESIGN FOR MODERN BROWSERS

but make your site work perfectly for OLDER VERSIONS that are still out there

(maybe with less effects)

Page 38: Css3 transitions and animations + graceful degradation with jQuery

GRACEFUL DEGRADATIONin TRANSITIONS

In which cases should we implement a fallback, javascript based

transition?

Page 39: Css3 transitions and animations + graceful degradation with jQuery

WE SHOULD NOTWhen the transitions are merely for

“coolness”, or they may negatively affect site load time or runtime performance

Page 40: Css3 transitions and animations + graceful degradation with jQuery

WE SHOULDWhen transitions are useful for the site

comprehension, usability, to attract users, etc.

Page 41: Css3 transitions and animations + graceful degradation with jQuery

DETECTING BROWSER FEATURES

Page 42: Css3 transitions and animations + graceful degradation with jQuery

USE MODERNIZR!

• Go to http://modernizr.com/

• Download development version of Modernizr

• Copy it in your site folder

• Link it in your site header

Page 43: Css3 transitions and animations + graceful degradation with jQuery

WHAT MODERNIZR DOES:

For each result:

• It creates a boolean property of a global object called Modernizr

• It adds a css class to the html element of the page

Page 44: Css3 transitions and animations + graceful degradation with jQuery

WHAT MODERNIZR DOES:

Example:

• Modernizr.csstransitions (true/false)

• <html class=“csstransitions”>note: the negative result no-csstransitions

Page 45: Css3 transitions and animations + graceful degradation with jQuery

WHAT YOU CAN DO:

• JS: Create alternative lines of Javascript code to manage CSS transitions / Javascript transitions

• CSS: Manage exceptions depending on the browser support to CSS transitions

Page 46: Css3 transitions and animations + graceful degradation with jQuery

EXAMPLE OF JS FALLBACK

var newLeft = SOME_VALUE;

// Set the new left if browser can handle css transitions// else animate it the left propertyif (Modernizr.csstransitions) { this.bannerContainer.css({ left: newLeft });} else { this.bannerContainer.animate({ left: newLeft }, 750);}

Page 47: Css3 transitions and animations + graceful degradation with jQuery

EXAMPLE OF CSS FALLBACK

#someBox { background-color: rgba(255, 255, 255, 0.5);}

html.no-rgba #someBox { background-image: url(‘../img/white_50_percent.png’);}

Page 48: Css3 transitions and animations + graceful degradation with jQuery

DEMO

Page 49: Css3 transitions and animations + graceful degradation with jQuery

WHAT ABOUT ANIMATIONS?

Page 50: Css3 transitions and animations + graceful degradation with jQuery

WHAT ARE ANIMATIONS

Animations allow us to define states in time (keyframes) and transition through them

Page 51: Css3 transitions and animations + graceful degradation with jQuery

CSS ANIMATIONS

• Name an animation

• Define a set of keyframes

• Define the CSS properties for each frame

• Apply the animation in time

= Automatic transition bewteen keyframes

Page 52: Css3 transitions and animations + graceful degradation with jQuery

DECLARATION

@keyframes bounceThenFly { 0% { background-position: -13px 42px } 10% { background-position: -7px 30px } 20% { background-position: -13px 42px } 30% { background-position: -7px 30px } 40% { background-position: -13px 42px } 50% { background-position: 40px -60px } 50.01% { background-position: -35px 120px } 98% { background-position: -13px 42px }}

Defining the animation “bounceThenFly”

Page 53: Css3 transitions and animations + graceful degradation with jQuery

DECLARATION

@keyframes anotherAnimation { // ...

50% { background-color: red; color: white; text-shadow: 0 0 10px black; }

// ...}

(each keyframe can be more complex)

Page 54: Css3 transitions and animations + graceful degradation with jQuery

USAGE

#rocket {animation-name: bounceThenFly;animation-duration: 5s;animation-iteration-count: infinite;

}

The #rocket element will use the animation “bounceThenFly” during a time of 5 seconds,

and repeat it infinitely

Page 55: Css3 transitions and animations + graceful degradation with jQuery

USAGE

#rocket {animation: bounceThenFly 5s infinite;

}

Less characters = happier coders & lighter CSS

Page 56: Css3 transitions and animations + graceful degradation with jQuery

#rocket {animation: bounceThenFly 5s infinite;

}

USAGE

animation-name animation-duration animation-iteration-count

Less characters = happier coders & lighter CSS

Page 57: Css3 transitions and animations + graceful degradation with jQuery

DEMO

Page 58: Css3 transitions and animations + graceful degradation with jQuery

INTO THE WILD

Page 59: Css3 transitions and animations + graceful degradation with jQuery

VENDOR PREFIXES

For the CSS rules that are still to be defined as a standard, we need to use vendor-prefixes

Page 60: Css3 transitions and animations + graceful degradation with jQuery

-webkit- -moz- -ms- -o-

MARCH 2012

For the CSS rules that are still to be defined as a standard, we need to use vendor-prefixes

VENDOR PREFIXES

Page 61: Css3 transitions and animations + graceful degradation with jQuery

-webkit- -o-

OCTOBER 2012

For the CSS rules that are still to be defined as a standard, we need to use vendor-prefixes

VENDOR PREFIXES

Page 62: Css3 transitions and animations + graceful degradation with jQuery

-webkit-

____ 2013 ?

For the CSS rules that are still to be defined as a standard, we need to use vendor-prefixes

VENDOR PREFIXES

Page 63: Css3 transitions and animations + graceful degradation with jQuery

Option A

transition: color 500ms ease-in-out;

-webkit-transition: color 500ms ease-in-out;-o-transition: color 500ms ease-in-out;transition: color 500ms ease-in-out;

Manually duplicate the code From:

To:

Page 64: Css3 transitions and animations + graceful degradation with jQuery

Option B

Client-side: PrefixFreehttp://leaverou.github.com/prefixfree/

Server-side: Prefixerhttp://cssprefixer.appspot.com/

Use a script to do it automatically

Page 65: Css3 transitions and animations + graceful degradation with jQuery

RESUME

Page 66: Css3 transitions and animations + graceful degradation with jQuery

TRANSITIONS

• FOR LITTLE GRAPHIC TRANSITIONSwithout detect and fallback

• FOR RELEVANT GRAPHIC TRANSITIONSwith Javascript detect and fallback

• FOR EVERY TRANSITION on MOBILEno real need of detect and fallback

Page 67: Css3 transitions and animations + graceful degradation with jQuery

ANIMATIONS

• TO CREATE ANIMATED ELEMENTS IN SITESi.g. banners, call-to-action buttons, logos, graphical background elements

• SEE:http://2012.fromthefront.it

Page 68: Css3 transitions and animations + graceful degradation with jQuery

• They make a more pleasant user experience for users with modern browsers

• They are optimized for mobile devices

Page 69: Css3 transitions and animations + graceful degradation with jQuery

@verlokwww.andreaverlicchi.eu