日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > CSS >内容正文

CSS

一个SCSS里mixin的使用例子

發布時間:2023/12/19 CSS 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个SCSS里mixin的使用例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Mixins are the Sass equivalent of macros in other programming languages. If you’ve programmed before you could think of them as functions, procedures, or methods, but they aren’t technically any of these concepts because their function is to generate code at compile time not execute code at run time.

可以把mixins當成編程語言里的宏,但是技術上說,scss mixins用于在編譯器生成代碼,而不是在運行時執行代碼。

一個例子:

a.button {background: black;color: white;padding: 10px 20px;@include border-radius(5px); }

編譯之后:

a.button {background: black;color: white;padding: 10px 20px;-moz-border-radius: 5px;-webkit-border-radius: 5px;-ms-border-radius: 5px;-o-border-radius: 5px;-khtml-border-radius: 5px;border-radius: 5px; }

I used the @include directive to tell Sass that I wanted to call out to a mixin.
使用@include指令,告訴sass,需要在css里call out mixin. 通過括號傳遞參數。

看下border-radius的實現代碼:

@mixin border-radius($radius) {-moz-border-radius: $radius;-webkit-border-radius: $radius;-ms-border-radius: $radius;border-radius: $radius; }

一個mixin傳遞默認參數的做法:

@mixin border-radius($radius: 5px) {... }

或者定義一個全局變量:

$default-border-radius: 5px !default; @mixin border-radius($radius: $default-border-radius) {... }

scss mixin里還支持條件指令@if:

@mixin border-radius($radius: 5px, $moz: true, $webkit: true, $ms: true) {@if $moz { -moz-border-radius: $radius; }@if $webkit { -webkit-border-radius: $radius; }@if $ms { -ms-border-radius: $radius; }border-radius: $radius; }

上面的用法叫做keyword argument.

這樣,如果項目里我們不考慮對IE的支持,只需要下面這樣寫就行了:

@include border-radius($ms: false);

而不用這種繁瑣的寫法:

@include border-radius(5px, true, true, true);

也不需要按照順序傳遞參數:

@include border-radius($ms: false, $radius: 10px);

總結

以上是生活随笔為你收集整理的一个SCSS里mixin的使用例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。