ionic button 一些使用心得
原始碼檔案:button.scss 和 _button-bar.scss,以及variables.scss(66行-163行)。
按鈕是手機app不可或缺的一部分,不同風格的app,需要的按鈕多種多樣,按鈕的設定涉及按鈕的大小、顏色、狀態等。下面將對ionic的按鈕樣式做一一筆記。
ionic button樣式
ionic預設提供9種顏色風格:
$light: #fff !default;
$stable: #f8f8f8 !default;
$positive: #4a87ee !default;
$calm: #43cee6 !default;
$balanced: #66cc33 !default;
$energized: #f0b840 !default;
$assertive: #ef4e3a !default;
$royal: #8a6de9 !default;
$dark: #444 !default;
使用如下所示:
<button class="button">
Default
</button>
<button class="button button-light">
button-light
</button>
<button class="button button-stable">
button-stable
</button>
<button class="button button-positive">
button-positive
</button>
<button class="button button-calm">
button-calm
</button>
<button class="button button-balanced">
button-balanced
</button>
<button class="button button-energized">
button-energized
</button>
<button class="button button-assertive">
button-assertive
</button>
<button class="button button-royal">
button-royal
</button>
<button class="button button-dark">
button-dark
</button>
效果圖如下:
按鈕風格首先定義了基礎的.button,以及根據不同的顏色風格,定義不同ative行為特效。.button基礎樣式部分程式碼如下:
// _button.scss 第7行 - 30行
.button {
// set the color defaults
@include button-style($button-default-bg, $button-default-border, $button- default-active-bg, $button-default-active-border, $button-default-text);
position: relative;
display: inline-block;
margin: 0;
padding: 0 $button-padding;
min-width: ($button-padding * 3) + $button-font-size;
min-height: $button-height + 5px;
border-width: $button-border-width;
border-style: solid;
border-radius: $button-border-radius;
vertical-align: top;
text-align: center;
text-overflow: ellipsis;
font-size: $button-font-size;
line-height: $button-height - $button-border-width + 1px;
cursor: pointer;
另外,根據不同的顏色樣式的active的效果也不一樣,如positive顏色樣式的active效果如下:
$button-positive-bg: $positive !default;
$button-positive-text: #fff !default;
$button-positive-border: darken($positive, 15%) !default;
$button-positive-active-bg: darken($positive, 15%) !default;
$button-positive-active-border: darken($positive, 15%) !default;
字型顏色為白色(#fff),邊框顏色加深15%;當按鈕按下時,背景顏色加深15%。其它顏色樣式的active效果類似。
Block Buttons & Full Width Block Buttons
通常按鈕的寬度是由text長度+左右padding值決定的,所以很難滿足100%寬度的填充父容器。然而,ionic提供了block級的button樣式(Block Buttons & Full Width Block Buttons)。這兩類Buttons的程式碼如下:
.button-block {
display: block;
clear: both;
&:after{
clear: both;
}
}
.button-full,
.button-full > .button {
display: block;
margin-right: 0;
margin-left: 0;
border-right-width: 0;
border-left-width: 0;
border-radius: 0;
}
button.button-block,
button.button-full,
.button-full > button.button,
input.button.button-block {
width: 100%;
}
示例如下:
<buttonclass="button button-block button-positive">
BlockButton
</button>
<buttonclass="button button-full button-positive">
Full Width BlockButton
</button>
效果圖:
從上面可以看出,這兩者的相同點在於display: block,而Full Width Block Buttons刪除左右邊框和邊框半徑(border-radius);Block Buttons保留著padding,讓元素間有點呼吸空隙,而Full Width Bloc Buttons不包含padding值。
button大小
除了正常大小之外,ionic提供兩種不同的Sizes: button-large、button-small
.button-small {
padding: 2px $button-small-padding 1px;
min-width: $button-small-height;
min-height: $button-small-height + 2;
font-size: $button-small-font-size;
line-height: $button-small-height - $button-border-width - 1;
.icon:before,
&.icon:before,
&.icon-left:before,
&.icon-right:before {
font-size: $button-small-icon-size;
line-height: $button-small-icon-size + 3;
margin-top: 3px;
}
}
.button-large {
padding: 0 $button-large-padding;
min-width: ($button-large-padding * 3) + $button-large-font-size;
min-height: $button-large-height + 5;
font-size: $button-large-font-size;
line-height: $button-large-height - $button-border-width;
.icon:before,
&.icon:before,
&.icon-left:before,
&.icon-right:before {
padding-bottom: ($button-border-width * 2);
font-size: $button-large-icon-size;
line-height: $button-large-height - ($button-border-width * 2) - 1;
}
}
示例程式碼:
<button class="button button-small button-assertive">
Small Button
</button>
<button class="button">
Default Button
</button>
<button class="button button-large button-positive">
Large Button
</button>
<button class="button button-block button-small button-assertive">
Small Button
</button>
<button class="button button-block">
Default Button
</button>
<button class="button button-block button-large button-positive">
Large Button
</button>
效果圖:
兩者主要的區別在於:padding、min-width、min-height、font-size 和 line-height。
Outlined Button & Clear Button
這兩種button,都是無背景、字型顏色為button樣式顏色,其中clear button是無邊框的,原始碼如下:
button-clear {
@include button-clear($button-default-border);
@include transition(opacity .1s);
padding: 0 $button-clear-padding;
max-height: $button-height;
border-color: transparent;
background: none;
box-shadow: none;
&.active,
&.activated {
opacity: 0.3;
}
}
.button-outline {
@include button-outline($button-default-border);
@include transition(opacity .1s);
background: none;
box-shadow: none;
}
示例程式碼:
<button class="button button-outline button-positive">
Outlined Button
</button>
<button class="button button-clear button-positive">
Clear Button
</button>
效果圖:
Icon Buttons
一個按鈕只有冷冷的文字顯得沒有生氣,這時添加個icon,會使app的效果更上一個臺階。其中你可以使用ionic提供的Ionicons,或者其它型別的icon。
同時,雖然我們能夠在button中新增icon子元素,但這樣做會增加DOM元素,增加DOM載入負擔,所以ionic提供了Icon Buttons樣式。
使用示例:
<button class="button">
<i class="icon ion-loading-c"></i> Loading...
</button>
<button class="button icon-left ion-home">Home</button>
<button class="button icon-left ion-star button-positive">Favorites</button>
<a class="button icon-right ion-chevron-right button-calm">Learn More</a>
<a class="button icon-left ion-chevron-left button-clear button-dark">Back</a>
<button class="button icon ion-gear-a"></button>
<a class="button button-icon icon ion-settings"></a>
<a class="button button-outline icon-right ion-navicon button-balanced">Reorder</a>
效果圖:
不錯吧,不僅可以只顯示icon,也可以icon+text;不僅可以左邊顯示,可以右邊顯示icon。
header、footer下button的使用
button可以使用在app的各個空間中,如在header中使用,在app頭部左右各增加一個button,程式碼如下:
<div class="bar bar-header">
<button class="button icon ion-navicon"></button>
<h1class="title">Header Buttons</h1>
<button class="button">Edit</button>
</div>
效果圖:
其中,以上的button樣式基本都可以應用其中。
a button
此外,ionic提供了連結a的button效果,原始碼:
a.button {
text-decoration: none;
}
Button Bar
ionic提供了類似Bootstrap按鈕組.btn-group效果,原始碼如下:
/**
* Button Bar
* --------------------------------------------------
*/
.button-bar {
@include display-flex();
@include flex(1);
width: 100%;
&.button-bar-inline {
display: block;
width: auto;
@include clearfix();
> .button {
width: auto;
display: inline-block;
float: left;
}
}
}
.button-bar > .button {
@include flex(1);
display: block;
overflow: hidden;
padding: 0 16px;
width: 0;
border-width: 1px 0px 1px 1px;
border-radius: 0;
text-align: center;
text-overflow: ellipsis;
white-space: nowrap;
&:before,
.icon:before {
line-height: 44px;
}
&:first-child{
border-radius: 2px 0px 0px 2px;
}
&:last-child{
border-right-width: 1px;
border-radius: 0px 2px 2px 0px;
}
}
示例程式碼:
<div class="button-bar">
<a class="button">First</a>
<a class="button">Second</a>
<a class="button">Third</a>
</div>
其中按鈕組是一個block元件,100%寬度;
- 排除第一個和最後一個按鈕,其它按鈕都取消圓角設定;
- 第一個按鈕的左上角和左下角保留圓角設定;
- 最後一個按鈕保留右上角和右下角圓角設定。
- 除了最後一個按鈕之外,其它的border-width: 1px 0px 1px 1px;;
- 最後一個按鈕再補上最右邊的邊框: border-right-width: 1px;。