這篇來介紹button中elementUi、iview、ant中樣式結構
ant Design react
ant-react中button分兩個檔案less:
- mixins.less:根據button功能樣式不同封裝成函數。
- index.less:調用mixins.less中的函數來聲明button的相關class
我們先來看mixins.less的結構
.btn() { display: inline-block;//行內塊元素 font-weight: @btn-font-weight;//字型粗細 text-align: center;//字型置中 touch-action: manipulation;//瀏覽器只允許進行滾動和持續縮放操作 cursor: pointer;//滑鼠移上去形狀 background-image: none;//背景圖片為空白 border: @border-width-base @border-style-base transparent;//邊框透明 white-space: nowrap;//不換行 .button-size(@btn-height-base; @btn-padding-base; @font-size-base; @btn-border-radius-base);//padding height font-size border-radius設定 user-select: none;//文本不能被選擇 transition: all .3s @ease-in-out;//過渡 position: relative;//相對定位 > .@{iconfont-css-prefix} { line-height: 1;//行高不帶單位是相對字型的比例 } &, &:active, &:focus { outline: 0;//是繪製於元素周圍的一條線,位於邊框邊緣的外圍,可起到突出元素的作用 } &:not([disabled]):hover { text-decoration: none;//定義標準的文本 } &:not([disabled]):active { outline: 0; transition: none; } &.disabled, &[disabled] { cursor: not-allowed; > * { pointer-events: none;//元素永遠不會成為滑鼠事件的target } } &-lg { .button-size(@btn-height-lg; @btn-padding-lg; @btn-font-size-lg; @btn-border-radius-base); } &-sm { .button-size(@btn-height-sm; @btn-padding-sm; @btn-font-size-sm; @btn-border-radius-sm); }}
其中的具體屬性不多說了,不知道的可以百度屬性就知道了,大概就是設定字型粗細、字型置中、不換行、過渡、定位、邊框、啟用、焦點、hover、disabled等樣式,其中btn中調用了button-size函數。&-lg、&-sm設定大按鈕和小按鈕,調用了button-size函數
.button-size(@height; @padding; @font-size; @border-radius) { padding: @padding; font-size: @font-size; border-radius: @border-radius; height: @height;}
以height、padding、font-size、border-radius
為輸入參數來設定按鈕大小,寬度通過padding和font-size就能設定寬度。
- 下一個部分是設定按鈕類型為主按鈕、次按鈕、虛線按鈕、危險按鈕、幽靈按鍵樣式函數:
.button-variant-primary(@color; @background) { .button-color(@color; @background; @background); &:hover, &:focus { .button-color(@color; ~`colorPalette("@{background}", 5)`; ~`colorPalette("@{background}", 5)`); } &:active, &.active { .button-color(@color; ~`colorPalette("@{background}", 7)`; ~`colorPalette("@{background}", 7)`); } .button-disabled();}.button-variant-other(@color; @background; @border) { .button-color(@color; @background; @border); &:hover, &:focus { .button-color(@primary-5; @background; @primary-5); } &:active, &.active { .button-color(@primary-7; @background; @primary-7); } .button-disabled();}.button-variant-danger(@color; @background; @border) { .button-color(@color; @background; @border); &:hover { .button-color(@btn-primary-color; ~`colorPalette("@{color}", 5)`; ~`colorPalette("@{color}", 5)`); } &:focus { .button-color(~`colorPalette("@{color}", 5)`; #fff; ~`colorPalette("@{color}", 5)`); } &:active, &.active { .button-color(@btn-primary-color; ~`colorPalette("@{color}", 7)`; ~`colorPalette("@{color}", 7)`); } .button-disabled();}.button-variant-ghost(@color) { .button-color(@color; transparent; @color); &:hover, &:focus { .button-color(~`colorPalette("@{color}", 5)`; transparent; ~`colorPalette("@{color}", 5)`); } &:active, &.active { .button-color(~`colorPalette("@{color}", 7)`; transparent; ~`colorPalette("@{color}", 7)`); } .button-disabled();}
代碼中我們可以看到這些函數都是調用button-color來設定按鈕的邊框,背景,文字顏色,和調用button-disabled來設定禁用樣式。主要還是基礎顏色樣式不同,而且hover,active顏色樣式不一樣。而且在後面函數中btn-primary、btn-default、btn-ghost、btn-dashed、btn-danger調用上面的對應函數。代碼如下:
// primary button style.btn-primary() { .button-variant-primary(@btn-primary-color; @btn-primary-bg);}// default button style.btn-default() { .button-variant-other(@btn-default-color; @btn-default-bg; @btn-default-border); &:hover, &:focus, &:active, &.active { background: @btn-default-bg; text-decoration: none; }}// ghost button style.btn-ghost() { .button-variant-other(@btn-ghost-color, @btn-ghost-bg, @btn-ghost-border);}// dashed button style.btn-dashed() { .button-variant-other(@btn-default-color, @btn-default-bg, @btn-default-border); border-style: dashed;}// danger button style.btn-danger() { .button-variant-danger(@btn-danger-color, @btn-danger-bg, @btn-danger-border);}
剩下就是按鈕組的樣式和圓按鈕的樣式
.button-group-base(@btnClassName) {//按鈕組的基礎樣式 position: relative; display: inline-block; > .@{btnClassName}, > span > .@{btnClassName} { position: relative; line-height: @btn-height-base - 2px; &:hover, &:focus, &:active, &.active { z-index: 2; } &:disabled { z-index: 0; } } // size &-lg > .@{btnClassName}, &-lg > span > .@{btnClassName} { .button-size(@btn-height-lg; @btn-padding-lg; @btn-font-size-lg; 0); line-height: @btn-height-lg - 2px; } &-sm > .@{btnClassName}, &-sm > span > .@{btnClassName} { .button-size(@btn-height-sm; @btn-padding-sm; @font-size-base; 0); line-height: @btn-height-sm - 2px; > .@{iconfont-css-prefix} { font-size: @font-size-base; } }}.btn-group(@btnClassName: btn) {//按鈕組主要是設定裡面一排按鈕的邊框和圓角 .button-group-base(@btnClassName); .@{btnClassName} + .@{btnClassName}, .@{btnClassName} + &, span + .@{btnClassName}, .@{btnClassName} + span, > span + span, & + .@{btnClassName}, & + & { margin-left: -1px; } .@{btnClassName}-primary + .@{btnClassName}:not(.@{btnClassName}-primary):not([disabled]) { border-left-color: transparent; } .@{btnClassName} { border-radius: 0; } > .@{btnClassName}:first-child, > span:first-child > .@{btnClassName} { margin-left: 0; } > .@{btnClassName}:only-child { border-radius: @btn-border-radius-base; } > span:only-child > .@{btnClassName} { border-radius: @btn-border-radius-base; } > .@{btnClassName}:first-child:not(:last-child), > span:first-child:not(:last-child) > .@{btnClassName} { border-bottom-left-radius: @btn-border-radius-base; border-top-left-radius: @btn-border-radius-base; } > .@{btnClassName}:last-child:not(:first-child), > span:last-child:not(:first-child) > .@{btnClassName} { border-bottom-right-radius: @btn-border-radius-base; border-top-right-radius: @btn-border-radius-base; } &-sm { > .@{btnClassName}:only-child { border-radius: @btn-border-radius-sm; } > span:only-child > .@{btnClassName} { border-radius: @btn-border-radius-sm; } > .@{btnClassName}:first-child:not(:last-child), > span:first-child:not(:last-child) > .@{btnClassName} { border-bottom-left-radius: @btn-border-radius-sm; border-top-left-radius: @btn-border-radius-sm; } > .@{btnClassName}:last-child:not(:first-child), > span:last-child:not(:first-child) > .@{btnClassName} { border-bottom-right-radius: @btn-border-radius-sm; border-top-right-radius: @btn-border-radius-sm; } } & > & { float: left; } & > &:not(:first-child):not(:last-child) > .@{btnClassName} { border-radius: 0; } & > &:first-child:not(:last-child) { > .@{btnClassName}:last-child { border-bottom-right-radius: 0; border-top-right-radius: 0; padding-right: 8px; } } & > &:last-child:not(:first-child) > .@{btnClassName}:first-child { border-bottom-left-radius: 0; border-top-left-radius: 0; padding-left: 8px; }}