Skip to content

Instantly share code, notes, and snippets.

@tobiasahlin
Created August 6, 2014 12:43
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tobiasahlin/7a421fb9306a4f518aab to your computer and use it in GitHub Desktop.
Save tobiasahlin/7a421fb9306a4f518aab to your computer and use it in GitHub Desktop.
Sass multiple transitions mixin
// Usage: @include transition(width, height 0.3s ease-in-out);
// Output: -webkit-transition(width 0.2s, height 0.3s ease-in-out);
// transition(width 0.2s, height 0.3s ease-in-out);
//
// Pass in any number of transitions
@mixin transition($transitions...) {
$unfoldedTransitions: ();
@each $transition in $transitions {
$unfoldedTransitions: append($unfoldedTransitions, unfoldTransition($transition), comma);
}
-webkit-transition: $unfoldedTransitions;
transition: $unfoldedTransitions;
}
@function unfoldTransition ($transition) {
// Default values
$property: all;
$duration: .2s;
$easing: null; // Browser default is ease, which is what we want
$delay: null; // Browser default is 0, which is what we want
$defaultProperties: ($property, $duration, $easing, $delay);
// Grab transition properties if they exist
$unfoldedTransition: ();
@for $i from 1 through length($defaultProperties) {
$p: null;
@if $i <= length($transition) {
$p: nth($transition, $i)
} @else {
$p: nth($defaultProperties, $i)
}
$unfoldedTransition: append($unfoldedTransition, $p);
}
@return $unfoldedTransition;
}
@farskid
Copy link

farskid commented Jun 8, 2016

This is exactly what I wanted for months. The last search with full of disappointment led me here. Thanks a lot for the cleverness.

@dimitrieh
Copy link

Awesome stuff!

@donalathuman
Copy link

Works perfectly, thank you!

@aglaganov
Copy link

Great! Thank you!

@tdhartwick
Copy link

This is perfect! Thanks!!

@sgromkov
Copy link

sgromkov commented Mar 6, 2020

Great job! Thanks!

@orangebokov
Copy link

many thanks

@Klemart3D
Copy link

Klemart3D commented Dec 30, 2021

I get Error: () isn't a valid CSS value. when I'm calling @include transition();.
Is there a way to returns defaults values transition(all .2s) when calling the mixin without argument?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment