Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yomotsu/988275 to your computer and use it in GitHub Desktop.
Save yomotsu/988275 to your computer and use it in GitHub Desktop.
sass @Minxin linear-gradient() including SVG code for IE9
@mixin linear-gradient($angle, $color1, $offset1, $color2 ,$offset2:100, $color3:null, $offset3:100, $color4:null, $offset4:100, $color5:null, $offset5:100){
$angle_webkit:'';
$angle_svg:'';
$color-stop1_css:'#'#{$color1}' '#{$offset1}'%';
$color-stop2_css:',#'#{$color2}' '#{$offset2}'%';
$color-stop3_css:'';
$color-stop4_css:'';
$color-stop5_css:'';
$color-stop1_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color1}'%22%20offset%3d%22'#{$offset1}'%25%22%2f%3e';
$color-stop2_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color2}'%22%20offset%3d%22'#{$offset2}'%25%22%2f%3e';
$color-stop3_svg:'';
$color-stop4_svg:'';
$color-stop5_svg:'';
$color-stop1_webkit:'color-stop('#{$offset1}'%, #'#{$color1}')';
$color-stop2_webkit:',color-stop('#{$offset2}'%, #'#{$color2}')';
$color-stop3_webkit:'';
$color-stop4_webkit:'';
$color-stop5_webkit:'';
@if $angle == top{
$angle:'';
$angle_webkit:'left top, left bottom';
$angle_svg:'%20x2%3d%220%25%22%20y2%3d%22100%25%22';
}
@if $angle == right{
$angle:'right,';
$angle_webkit:'right top, left top';
$angle_svg:'%20x2%3d%22100%25%22';
}
@if $angle == bottom{
$angle:'bottom,';
$angle_webkit:'left bottom, left top';
$angle_svg:'%20x2%3d%220%22%20y1%3d%22100%25%22';
}
@if $angle == left{
$angle:'left,';
$angle_webkit:'left top, right top';
$angle_svg:'';
}
@if $color3 != null {
$color-stop3_css:',#'#{$color3}' '#{$offset3}'%';
$color-stop3_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color3}'%22%20offset%3d%22'#{$offset3}'%25%22%2f%3e';
$color-stop3_webkit:',color-stop('#{$offset3}'%, #'#{$color3}')';
}
@if $color4 != null {
$color-stop4_css:',#'#{$color4}' '#{$offset4}'%';
$color-stop4_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color4}'%22%20offset%3d%22'#{$offset4}'%25%22%2f%3e';
$color-stop4_webkit:',color-stop('#{$offset4}'%, #'#{$color4}')';
}
@if $color5 != null {
$color-stop5_css:',#'#{$color5}' '#{$offset5}'%';
$color-stop5_svg:'%3cstop%20style%3d%22stop%2dcolor%3a%23'#{$color5}'%22%20offset%3d%22'#{$offset5}'%25%22%2f%3e';
$color-stop5_webkit:',color-stop('#{$offset5}'%, #'#{$color5}')';
}
-pie-background: linear-gradient(#{$angle}#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image: url(data:image/svg+xml,%3c%3fxml%20version%3d%221%2e0%22%3f%3e%3csvg%20xmlns%3d%22http%3a%2f%2fwww%2ew3%2eorg%2f2000%2fsvg%22%20width%3d%22100%25%22%20height%3d%22100%25%22%3e%3cdefs%3e%3clinearGradient%20id%3d%22G%22#{$angle_svg}%3e#{$color-stop1_svg}#{$color-stop2_svg}#{$color-stop3_svg}#{$color-stop4_svg}#{$color-stop5_svg}%3c%2flinearGradient%3e%3c%2fdefs%3e%3crect%20width%3d%22100%25%22%20height%3d%22100%25%22%20fill%3d%22url%28%23G%29%22%2f%3e%3c%2fsvg%3e);
background-image: -webkit-gradient(linear,#{$angle_webkit},#{$color-stop1_webkit}#{$color-stop2_webkit}#{$color-stop3_webkit}#{$color-stop4_webkit}#{$color-stop5_webkit});
background-image:-webkit-linear-gradient(#{$angle}#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image: -moz-linear-gradient(#{$angle}#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image: -ms-linear-gradient(#{$angle}#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image: -o-linear-gradient(#{$angle}#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
background-image: linear-gradient(#{$angle}#{$color-stop1_css}#{$color-stop2_css}#{$color-stop3_css}#{$color-stop4_css}#{$color-stop5_css});
}
.selector {
-pie-background: linear-gradient(#dadada 0%,#ffffff 18%,#eaeaea 47%,#dbdbdb 54%,#ececec 100%);
background-image: url(data:image/svg+xml,%3c%3fxml%20version%3d%221%2e0%22%3f%3e%3csvg%20xmlns%3d%22http%3a%2f%2fwww%2ew3%2eorg%2f2000%2fsvg%22%20width%3d%22100%25%22%20height%3d%22100%25%22%3e%3cdefs%3e%3clinearGradient%20id%3d%22G%22%20x2%3d%220%25%22%20y2%3d%22100%25%22%3e%3cstop%20style%3d%22stop%2dcolor%3a%23dadada%22%20offset%3d%220%25%22%2f%3e%3cstop%20style%3d%22stop%2dcolor%3a%23ffffff%22%20offset%3d%2218%25%22%2f%3e%3cstop%20style%3d%22stop%2dcolor%3a%23eaeaea%22%20offset%3d%2247%25%22%2f%3e%3cstop%20style%3d%22stop%2dcolor%3a%23dbdbdb%22%20offset%3d%2254%25%22%2f%3e%3cstop%20style%3d%22stop%2dcolor%3a%23ececec%22%20offset%3d%22100%25%22%2f%3e%3c%2flinearGradient%3e%3c%2fdefs%3e%3crect%20width%3d%22100%25%22%20height%3d%22100%25%22%20fill%3d%22url%28%23G%29%22%2f%3e%3c%2fsvg%3e);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #dadada),color-stop(18%, #ffffff),color-stop(47%, #eaeaea),color-stop(54%, #dbdbdb),color-stop(100%, #ececec));
background-image: -webkit-linear-gradient(#dadada 0%,#ffffff 18%,#eaeaea 47%,#dbdbdb 54%,#ececec 100%);
background-image: -moz-linear-gradient(#dadada 0%,#ffffff 18%,#eaeaea 47%,#dbdbdb 54%,#ececec 100%);
background-image: -ms-linear-gradient(#dadada 0%,#ffffff 18%,#eaeaea 47%,#dbdbdb 54%,#ececec 100%);
background-image: -o-linear-gradient(#dadada 0%,#ffffff 18%,#eaeaea 47%,#dbdbdb 54%,#ececec 100%);
background-image: linear-gradient(#dadada 0%,#ffffff 18%,#eaeaea 47%,#dbdbdb 54%,#ececec 100%); }
@import "_mixin_linear-gradient.scss";
.selector{
//引数は、<方向>,<色1>,<位置1>,<色2>,<位置2>,<色3>,<位置3>...
//色には#は付けない。また、16進数のみ
//offset にはパーセンテージで渡す。単位は付けない
@include linear-gradient(top, "dadada", 0, "ffffff", 18, "eaeaea", 47, "dbdbdb", 54, "ececec", 100);
}
@yomotsu
Copy link
Author

yomotsu commented May 29, 2011

'top' のときは angle が省略されるようにした

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