Mixin ombre materiche - Trucchi CSS

Sommario

Il Material Design è stato dappertutto ultimamente. Una parte consiste nell'impilare strati uno sopra l'altro come veri fogli di carta. Per ottenere un tale effetto nei CSS, dobbiamo usare la box-shadowproprietà.

Per evitare di dover scrivere manualmente le ombre ogni volta, possiamo creare un po 'di Sass mixin per questo. L'unica cosa che questo mixin farà è emettere una box-shadowdichiarazione basata sul dato $depth(da 0 a 5). Le ombre verranno effettivamente calcolate da due funzioni: bottom-shadowe top-shadow.

/// Gives a card depth effect. /// @param (Number) $depth - depth level (between 1 and 5) /// @link http://www.google.com/design/spec/layout/layout-principles.html#layout-principles-dimensionality Google Design /// @requires (function) top-shadow /// @requires (function) bottom-shadow @mixin card($depth) ( @if $depth 5 ( @warn "Invalid $depth `#($depth)` for mixin `card`."; ) @else ( box-shadow: bottom-shadow($depth), top-shadow($depth); ) )

Non dimentichiamo le nostre due funzioni:

/// Computes a top-shadow for a card effect. /// @param (Number) $depth - depth level /// @return (List) @function top-shadow($depth) ( $primary-offset: nth(1.5 3 10 14 19, $depth) * 1px; $blur: nth(1.5 3 10 14 19, $depth) * 4px; $color: rgba(black, nth(.12 .16 .19 .25 .30, $depth)); @return 0 $primary-offset $blur $color; ) /// Computes a bottom-shadow for a card effect. /// @param (Number) $depth - depth level /// @return (List) @function bottom-shadow($depth) ( $primary-offset: nth(1.5 3 6 10 15, $depth) * 1px; $blur: nth(1 3 3 5 6, $depth) * 4px; $color: rgba(black, nth(.24 .23 .23 .22 .22, $depth)); @return 0 $primary-offset $blur $color; )

Guarda l'ombra Pen Material di Hugo Giraudel (@HugoGiraudel) su CodePen.

Articoli interessanti...