{"id":205794,"date":"2020-12-02T09:18:39","date_gmt":"2020-12-02T01:18:39","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=205794"},"modified":"2020-11-23T11:19:27","modified_gmt":"2020-11-23T03:19:27","slug":"sass-mixin-include","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sass-mixin-include.html","title":{"rendered":"\u5b66\u4e60Sass @mixin \u4e0e @include"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSass (\u82f1\u6587\u5168\u79f0\uff1aSyntactically Awesome Stylesheets) \u662f\u4e00\u4e2a\u6700\u521d\u7531 Hampton Catlin \u8bbe\u8ba1\u5e76\u7531 Natalie Weizenbaum \u5f00\u53d1\u7684\u5c42\u53e0\u6837\u5f0f\u8868\u8bed\u8a00\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n@mixin \u6307\u4ee4\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u53ef\u4ee5\u5728\u6574\u4e2a\u6837\u5f0f\u8868\u4e2d\u91cd\u590d\u4f7f\u7528\u7684\u6837\u5f0f\u3002<\/p>\n

@include \u6307\u4ee4\u53ef\u4ee5\u5c06\u6df7\u5165\uff08mixin\uff09\u5f15\u5165\u5230\u6587\u6863\u4e2d\u3002<\/p>\n

\u5b9a\u4e49\u4e00\u4e2a\u6df7\u5165<\/strong><\/div>\n

\u6df7\u5165(mixin)\u901a\u8fc7 @mixin \u6307\u4ee4\u6765\u5b9a\u4e49\u3002 @mixin name { property: value; property: value; ... }
\n\u4ee5\u4e0b\u5b9e\u4f8b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \"important-text\" \u7684\u6df7\u5165\uff1a<\/p>\n

Sass \u4ee3\u7801\uff1a<\/p>\n

\r\n@mixin important-text {\r\n  color: red;\r\n  font-size: 25px;\r\n  font-weight: bold;\r\n  border: 1px solid blue;\r\n}<\/pre>\n

\u6ce8\u610f\uff1aSass \u7684\u8fde\u63a5\u7b26\u53f7 - \u4e0e\u4e0b\u5212\u7ebf\u7b26\u53f7 _ \u662f\u76f8\u540c\u7684\uff0c\u4e5f\u5c31\u662f @mixin important-text { } \u4e0e @mixin important_text { } \u662f\u4e00\u6837\u7684\u6df7\u5165\u3002<\/p>\n

\u4f7f\u7528\u6df7\u5165<\/strong><\/div>\n

@include \u6307\u4ee4\u53ef\u7528\u4e8e\u5305\u542b\u4e00\u6df7\u5165\uff1a<\/p>\n

Sass @include \u6df7\u5165\u8bed\u6cd5\uff1a<\/p>\n

\r\nselector {\r\n  @include mixin-name;\r\n}<\/pre>\n

\u56e0\u6b64\uff0c\u5305\u542b important-text \u6df7\u5165\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\n.danger {\r\n  @include important-text;\r\n  background-color: green;\r\n}<\/pre>\n

\u5c06\u4ee5\u4e0a\u4ee3\u7801\u8f6c\u6362\u4e3a CSS \u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

Css \u4ee3\u7801\uff1a<\/p>\n

\r\n.danger {\r\n  color: red;\r\n  font-size: 25px;\r\n  font-weight: bold;\r\n  border: 1px solid blue;\r\n  background-color: green;\r\n}<\/pre>\n

\u6df7\u5165\u4e2d\u4e5f\u53ef\u4ee5\u5305\u542b\u6df7\u5165\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\n@mixin special-text {\r\n  @include important-text;\r\n  @include link;\r\n  @include special-border;\r\n}<\/pre>\n

\u5411\u6df7\u5165\u4f20\u9012\u53d8\u91cf
\n\u6df7\u5165\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u5411\u6df7\u5165\u4f20\u9012\u53d8\u91cf\u3002<\/p>\n

\u5b9a\u4e49\u53ef\u4ee5\u63a5\u6536\u53c2\u6570\u7684\u6df7\u5165\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\n\/* \u6df7\u5165\u63a5\u6536\u4e24\u4e2a\u53c2\u6570 *\/\r\n@mixin bordered($color, $width) {\r\n  border: $width solid $color;\r\n}\r\n\r\n.myArticle {\r\n  @include bordered(blue, 1px);  \/\/ \u8c03\u7528\u6df7\u5165\uff0c\u5e76\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\r\n}\r\n\r\n.myNotes {\r\n  @include bordered(red, 2px); \/\/ \u8c03\u7528\u6df7\u5165\uff0c\u5e76\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\r\n}<\/pre>\n

\u4ee5\u4e0a\u5b9e\u4f8b\u7684\u6df7\u5165\u53c2\u6570\u4e3a\u8bbe\u7f6e\u8fb9\u6846\u7684\u5c5e\u6027 (color \u548c width) \u3002<\/p>\n

\u5c06\u4ee5\u4e0a\u4ee3\u7801\u8f6c\u6362\u4e3a CSS \u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

Css \u4ee3\u7801\uff1a<\/p>\n

\r\n.myArticle {\r\n  border: 1px solid blue;\r\n}\r\n\r\n.myNotes {\r\n  border: 2px solid red;\r\n}<\/pre>\n

\u6df7\u5165\u7684\u53c2\u6570\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u9ed8\u8ba4\u503c\uff0c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\n@mixin bordered($color: blue, $width: 1px) {\r\n  border: $width solid $color;\r\n}<\/pre>\n

\u5728\u5305\u542b\u6df7\u5165\u65f6\uff0c\u4f60\u53ea\u9700\u8981\u4f20\u9012\u9700\u8981\u7684\u53d8\u91cf\u540d\u53ca\u5176\u503c\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\n@mixin sexy-border($color, $width: 1in) {\r\n  border: {\r\n    color: $color;\r\n    width: $width;\r\n    style: dashed;\r\n  }\r\n}\r\np { @include sexy-border(blue); }\r\nh1 { @include sexy-border(blue, 2in); }<\/pre>\n

\u5c06\u4ee5\u4e0a\u4ee3\u7801\u8f6c\u6362\u4e3a CSS \u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

Css \u4ee3\u7801\uff1a<\/p>\n

\r\np {\r\n  border-color: blue;\r\n  border-width: 1in;\r\n  border-style: dashed; }\r\n\r\nh1 {\r\n  border-color: blue;\r\n  border-width: 2in;\r\n  border-style: dashed;\r\n}<\/pre>\n
\u53ef\u53d8\u53c2\u6570<\/strong><\/div>\n

\u6709\u65f6\uff0c\u4e0d\u80fd\u786e\u5b9a\u4e00\u4e2a\u6df7\u5165\uff08mixin\uff09\u6216\u8005\u4e00\u4e2a\u51fd\u6570\uff08function\uff09\u4f7f\u7528\u591a\u5c11\u4e2a\u53c2\u6570\uff0c\u8fd9\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 ... \u6765\u8bbe\u7f6e\u53ef\u53d8\u53c2\u6570\u3002<\/p>\n

\u4f8b\u5982\uff0c\u7528\u4e8e\u521b\u5efa\u76d2\u5b50\u9634\u5f71\uff08box-shadow\uff09\u7684\u4e00\u4e2a\u6df7\u5165\uff08mixin\uff09\u53ef\u4ee5\u91c7\u53d6\u4efb\u4f55\u6570\u91cf\u7684 box-shadow \u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\n@mixin box-shadow($shadows...) {\r\n      -moz-box-shadow: $shadows;\r\n      -webkit-box-shadow: $shadows;\r\n      box-shadow: $shadows;\r\n}\r\n\r\n.shadows {\r\n  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);\r\n}<\/pre>\n

\u5c06\u4ee5\u4e0a\u4ee3\u7801\u8f6c\u6362\u4e3a CSS \u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

Css \u4ee3\u7801\uff1a<\/p>\n

\r\n.shadows {\r\n  -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;\r\n  -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;\r\n  box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;\r\n}<\/pre>\n

\u6d4f\u89c8\u5668\u524d\u7f00\u4f7f\u7528\u6df7\u5165
\n\u6d4f\u89c8\u5668\u524d\u7f00\u4f7f\u7528\u6df7\u5165\u4e5f\u662f\u975e\u5e38\u65b9\u4fbf\u7684\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

\r\n@mixin transform($property) {\r\n  -webkit-transform: $property;\r\n  -ms-transform: $property;\r\n  transform: $property;\r\n}\r\n\r\n.myBox {\r\n  @include transform(rotate(20deg));\r\n}<\/pre>\n

\u5c06\u4ee5\u4e0a\u4ee3\u7801\u8f6c\u6362\u4e3a CSS \u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

Css \u4ee3\u7801\uff1a<\/p>\n

\r\n.myBox {\r\n  -webkit-transform: rotate(20deg);\r\n  -ms-transform: rotate(20deg);\r\n  transform: rotate(20deg);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

@mixin \u6307\u4ee4\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u53ef\u4ee5\u5728\u6574\u4e2a\u6837\u5f0f\u8868\u4e2d\u91cd\u590d\u4f7f\u7528\u7684\u6837\u5f0f\u3002 @include \u6307\u4ee4\u53ef\u4ee5\u5c06\u6df7\u5165\uff08mix […]<\/p>\n","protected":false},"author":1481,"featured_media":205778,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-205794","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=205794"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205794\/revisions"}],"predecessor-version":[{"id":205796,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/205794\/revisions\/205796"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/205778"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=205794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=205794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=205794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}