Created
June 20, 2015 20:36
-
-
Save Rplus/3ca52e2d42c4b922cee5 to your computer and use it in GitHub Desktop.
string encode in SASS/SCSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // source: http://codepen.io/philippkuehn/pen/zGEjxB/?editors=010 | |
| $icon-color: #F84830; | |
| // icon styles | |
| // note the fill="' + $icon-color + '" | |
| .icon { | |
| display: inline-block; | |
| width: 50px; | |
| height: 50px; | |
| background: inline-svg('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" | |
| viewBox="0 0 30 30" enable-background="new 0 0 30 30" xml:space="preserve"> | |
| <path fill="' + $icon-color + '" d="M18.7,10.1c-0.6,0.7-1,1.6-0.9,2.6c0,0.7-0.6,0.8-0.9,0.3c-1.1-2.1-0.4-5.1,0.7-7.2c0.2-0.4,0-0.8-0.5-0.7 | |
| c-5.8,0.8-9,6.4-6.4,12c0.1,0.3-0.2,0.6-0.5,0.5c-0.6-0.3-1.1-0.7-1.6-1.3c-0.2-0.3-0.4-0.5-0.6-0.8c-0.2-0.4-0.7-0.3-0.8,0.3 | |
| c-0.5,2.5,0.3,5.3,2.1,7.1c4.4,4.5,13.9,1.7,13.4-5.1c-0.2-2.9-3.2-4.2-3.3-7.1C19.6,10,19.1,9.6,18.7,10.1z"/> | |
| </svg>'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // source: http://codepen.io/philippkuehn/pen/zGEjxB/?editors=010 | |
| // functions to urlencode the svg string | |
| @function str-replace($string, $search, $replace: '') { | |
| $index: str-index($string, $search); | |
| @if $index { | |
| @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); | |
| } | |
| @return $string; | |
| } | |
| @function url-encode($string) { | |
| $map: ( | |
| "%": "%25", | |
| "<": "%3C", | |
| ">": "%3E", | |
| " ": "%20", | |
| "!": "%21", | |
| "*": "%2A", | |
| "'": "%27", | |
| '"': "%22", | |
| "(": "%28", | |
| ")": "%29", | |
| ";": "%3B", | |
| ":": "%3A", | |
| "@": "%40", | |
| "&": "%26", | |
| "=": "%3D", | |
| "+": "%2B", | |
| "$": "%24", | |
| ",": "%2C", | |
| "/": "%2F", | |
| "?": "%3F", | |
| "#": "%23", | |
| "[": "%5B", | |
| "]": "%5D" | |
| ); | |
| $new: $string; | |
| @each $search, $replace in $map { | |
| $new: str-replace($new, $search, $replace); | |
| } | |
| @return $new; | |
| } | |
| @function inline-svg($string) { | |
| @return url('data:image/svg+xml;utf8,#{url-encode($string)}'); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://codepen.io/philippkuehn/pen/zGEjxB/?editors=010
在 SCSS 裡將 string encode 後塞進 data url image 裡
可變數化