background: #1e5799; /* Old browsers */ background: -moz-linear-gradient(left, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, right top, color-stop(0%,#1e5799), color-stop(50%,#2989d8), color-stop(51%,#207cca), color-stop(100%,#7db9e8)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(left, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(left, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(left, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* IE10+ */ background: linear-gradient(to right, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=1 ); /* IE6-9 */
background: #1e5799; /* Old browsers */ background: -moz-linear-gradient(left, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%); /* FF3.6+ */ background: -webkit-linear-gradient(left, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(left, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(left, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* IE10+ */ background: linear-gradient(to right, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%); /* W3C */
$gradientOptions: rgba(225,225,225,0) 0%, rgba(225,225,225,1) 10%, rgba(225,225,225,1) 90%, rgba(225,225,225,0) 100%; @include linearGradient(#fff, $gradientOptions , left);
background: -*-linear-gradient(left, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%);
#1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%
$browsersPrefix: moz o webkit ms; @mixin linearGradient($oldColor, $gradientList, $direction) { background: $oldColor; $directionRevers:null; @if $direction == left { $directionRevers: right; } @else if $direction == right { $directionRevers: left; } @else if $direction == top { $directionRevers: bottom; } @else if $direction == bottom { $directionRevers: top; } @include buildGradientLine($gradientList); @each $prefix in $browsersPrefix { background:-#{$prefix}-linear-gradient($direction, $resultLine); } background: linear-gradient(to $directionRevers, $resultLine); }
$directionRevers
variable is needed just for the rule according to the W3C standard, where the direction is indicated to which side the gradient goes, unlike browser solutions, which indicate where the direction starts. Oh, yes, in the code only 4 directions are possible, but who wants a more flexible solution, it is easy to increase the code. @each
performs a search on the $browsersPrefix
prefixes previously specified in the variable and displays almost the necessary code.@include buildGradientLine($gradientList)
and the variable $resultLine
. The @include buildGradientLine($gradientList)
line @include buildGradientLine($gradientList)
just forms the “color line”, and the $resultLine
variable contains it: $resultLine:null; @mixin buildGradientLine($gradientList) { $resultLine:null; @for $i from 1 through length($gradientList) { $colors: nth($gradientList, $i); $color: nth($colors, 1); $position: nth($colors, 2); $resultLine: $resultLine $color $position; @if $i != length($gradientList) { $resultLine: $resultLine#{','} } } }
$resultLine:null
, and then the logic is understandable for an ordinary programmer (it should be clear for a student, $resultLine:null
, though anything can happen). By the way, I had a problem with this part of the code. Instead of a loop, I used the usual @each
, as a result of which I did the same thing, with the difference that in the line, at the end, there was the last comma with which the gradient did not want to be displayed. The solution with the @for
cycle I spied here . Who cares what this tricky part of the code is: $colors: nth($gradientList, $i); $color: nth($colors, 1); $position: nth($colors, 2);
$colors: nth($gradientList, $i);
selects the desired pair of values in a loop from the entire array, and $color: nth($colors, 1); $position: nth($colors, 2)
$color: nth($colors, 1); $position: nth($colors, 2)
write to variable colors and positions respectively.Source: https://habr.com/ru/post/160677/
All Articles