Patene
Whakamahia nga momo patene ritenga a Bootstrap mo nga mahi i roto i nga puka, nga korero, me etahi atu me te tautoko mo nga rahi maha, ahua, me etahi atu.
Tauira
Kei roto i te Bootstrap te maha o nga momo patene kua tautuhia, e mahi ana ia tangata ki tana ake kaupapa korero, me etahi taapiri ka maka ki roto kia kaha ake te whakahaere.
<button type="button" class="btn btn-primary">Primary</button>
<button type="button" class="btn btn-secondary">Secondary</button>
<button type="button" class="btn btn-success">Success</button>
<button type="button" class="btn btn-danger">Danger</button>
<button type="button" class="btn btn-warning">Warning</button>
<button type="button" class="btn btn-info">Info</button>
<button type="button" class="btn btn-light">Light</button>
<button type="button" class="btn btn-dark">Dark</button>
<button type="button" class="btn btn-link">Link</button>
Te kawe i te tikanga ki nga hangarau awhina
Ko te whakamahi i te tae ki te taapiri i te tikanga he tohu tirohanga noa iho, kaore e tukuna ki nga kaiwhakamahi hangarau awhina - penei i nga kaipanui mata. Me mohio kei te kitea nga korero e tohuhia ana e te tae mai i te ihirangi ake (hei tauira, te tuhinga ka kitea), ka whakaurua ranei ma etahi atu huarahi, penei i etahi atu tuhinga huna ki te .visually-hidden
akomanga.
Monokia te takai kuputuhi
Ki te kore koe e pirangi kia takai te kupu patene, ka taea e koe te taapiri i te .text-nowrap
karaehe ki te paatene. I roto i te Sass, ka taea e koe te tautuhi $btn-white-space: nowrap
ki te whakakore i te takai kuputuhi mo ia paatene.
Nga tohu paatene
Kua .btn
hoahoatia nga karaehe hei whakamahi me te <button>
huānga. Heoi, ka taea hoki e koe te whakamahi i enei karaehe ki runga , ki nga huānga <a>
ranei <input>
(ahakoa he rereke te ahua o etahi kaitirotiro).
Ina whakamahi i nga karaehe patene i runga i <a>
nga huānga e whakamahia ana ki te whakaoho i te mahi-a-wharangi (penei i te tiango ihirangi), kaua ki te hono ki nga wharangi hou, ki nga waahanga ranei o te wharangi o naianei, me hoatu enei hononga ki te role="button"
kawe tika i o raatau kaupapa ki nga hangarau awhina penei i te kaipānui mata.
<a class="btn btn-primary" href="#" role="button">Link</a>
<button class="btn btn-primary" type="submit">Button</button>
<input class="btn btn-primary" type="button" value="Input">
<input class="btn btn-primary" type="submit" value="Submit">
<input class="btn btn-primary" type="reset" value="Reset">
Patene Whakahuahua
Kei te hiahia te paatene, engari ehara i nga tae papamuri nui e mauria mai ana? Whakakapihia nga karaehe whakarereke taunoa me nga .btn-outline-*
mea hei tango i nga whakaahua papamuri me nga tae katoa i runga i tetahi paatene.
<button type="button" class="btn btn-outline-primary">Primary</button>
<button type="button" class="btn btn-outline-secondary">Secondary</button>
<button type="button" class="btn btn-outline-success">Success</button>
<button type="button" class="btn btn-outline-danger">Danger</button>
<button type="button" class="btn btn-outline-warning">Warning</button>
<button type="button" class="btn btn-outline-info">Info</button>
<button type="button" class="btn btn-outline-light">Light</button>
<button type="button" class="btn btn-outline-dark">Dark</button>
Rahi
Kei te pirangi koe ki nga paatene nui, iti ake ranei? Tāpirihia mo etahi atu rahinga .btn-lg
ranei ..btn-sm
<button type="button" class="btn btn-primary btn-lg">Large button</button>
<button type="button" class="btn btn-secondary btn-lg">Large button</button>
<button type="button" class="btn btn-primary btn-sm">Small button</button>
<button type="button" class="btn btn-secondary btn-sm">Small button</button>
Ka taea e koe te huri i to ake rahinga ritenga me nga taurangi CSS:
<button type="button" class="btn btn-primary"
style="--bs-btn-padding-y: .25rem; --bs-btn-padding-x: .5rem; --bs-btn-font-size: .75rem;">
Custom button
</button>
Te ahua haua
Kia hohekore nga paatene ma te taapiri i te disabled
huanga boolean ki tetahi <button>
huānga. Kua tonohia nga patene monokia pointer-events: none
ki te aukati i te whakaparo me nga ahuatanga hohe kia kore e puta.
<button type="button" class="btn btn-primary" disabled>Primary button</button>
<button type="button" class="btn btn-secondary" disabled>Button</button>
<button type="button" class="btn btn-outline-primary" disabled>Primary button</button>
<button type="button" class="btn btn-outline-secondary" disabled>Button</button>
<a>
He rereke te ahua o nga patene monoa e whakamahi ana i te huānga:
<a>
s kore e tautoko i tedisabled
huanga, no reira me tapiri e koe te.disabled
karaehe kia kore ai e kitea te haua.- Ko etahi o nga momo ahua pai mo nga ra kei te heke mai hei whakakore i nga mea katoa
pointer-events
i runga i nga paatene punga. - Ko nga paatene kua monokia
<a>
me whakauru tearia-disabled="true"
huanga hei tohu i te ahua o te huānga ki nga hangarau awhina. - Ko nga paatene kua monokia
<a>
kaua e whakauru i tehref
huanga.
<a class="btn btn-primary disabled" role="button" aria-disabled="true">Primary link</a>
<a class="btn btn-secondary disabled" role="button" aria-disabled="true">Link</a>
hono hono taumahinga whakatupato
Hei hipoki i nga keehi me pupuri koe i te href
huanga i runga i te hononga haua, ka .disabled
whakamahi te karaehe pointer-events: none
ki te ngana ki te whakakore i te taumahinga hono a <a>
s. Kia mahara ko tenei taonga CSS kaore ano kia paerewahia mo HTML, engari kei te tautoko nga kaitirotiro hou katoa. I tua atu, ahakoa i roto i nga kaitirotiro e tautoko pointer-events: none
ana, kaore e pa ana te whakatere papapātuhi, ko te tikanga ka taea tonu e nga kaiwhakamahi papapātuhi me nga kaiwhakamahi o nga hangarau awhina te whakahohe i enei hononga. Na kia noho haumaru, hei taapiri atu ki te aria-disabled="true"
, whakauruhia he tabindex="-1"
huanga ki runga i enei hononga kia kore ai ratou e whiwhi arotahi papapātuhi, me te whakamahi i te JavaScript ritenga ki te whakakore i o raatau mahi.
<a href="#" class="btn btn-primary disabled" tabindex="-1" role="button" aria-disabled="true">Primary link</a>
<a href="#" class="btn btn-secondary disabled" tabindex="-1" role="button" aria-disabled="true">Link</a>
Patene poraka
Waihangahia he puranga urupare o te whanui-katoa, "patene paraka" pera i era i Bootstrap 4 me te whakakotahitanga o a maatau taputapu whakaatu me te waatea. Ma te whakamahi i nga taputapu hei utu mo nga karaehe patene, ka nui ake te mana whakahaere mo te mokowā, te tirohanga, me nga whanonga aro.
<div class="d-grid gap-2">
<button class="btn btn-primary" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
I konei ka waihangahia e matou he rereketanga urupare, ka timata mai i nga patene poutū tae noa ki te md
waahi pakaru, ka .d-md-block
whakakapi i te .d-grid
akomanga, na reira ka whakakorehia te gap-2
whaipainga. Whakanuia te rahi o to kaitirotiro kia kitea e huri ana.
<div class="d-grid gap-2 d-md-block">
<button class="btn btn-primary" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Ka taea e koe te whakatika i te whanui o o paatene paraka me nga karaehe whanui poupae matiti. Hei tauira, mo te hawhe-whanui "patene poraka", whakamahia .col-6
. Whakawaenga whakapae ki te .mx-auto
, rawa.
<div class="d-grid gap-2 col-6 mx-auto">
<button class="btn btn-primary" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Ka taea te whakamahi i etahi atu taputapu hei whakatika i te tirohanga o nga paatene ina whakapae. I konei kua tangohia e matou ta matou tauira urupare o mua me te taapiri i etahi taputapu ngawari me tetahi taputapu tawhē i runga i te paatene ki te whakatika tika i nga paatene ka kore e tapae.
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<button class="btn btn-primary me-md-2" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Pātene monomai
Ma te paatene mono ka taea e koe te hanga i nga paatene takahuri ngawari.
Takahuri āhua
Tāpirihia data-bs-toggle="button"
hei takahuri i te active
ahua o te paatene. Mēnā kei te takahuri i mua i te paatene, me taapiri a ringa koe i te .active
akomanga me aria-pressed="true"
te whakarite kia tika te kawe ki nga hangarau awhina.
<button type="button" class="btn btn-primary" data-bs-toggle="button">Toggle button</button>
<button type="button" class="btn btn-primary active" data-bs-toggle="button" aria-pressed="true">Active toggle button</button>
<button type="button" class="btn btn-primary" disabled data-bs-toggle="button">Disabled toggle button</button>
<a href="#" class="btn btn-primary" role="button" data-bs-toggle="button">Toggle link</a>
<a href="#" class="btn btn-primary active" role="button" data-bs-toggle="button" aria-pressed="true">Active toggle link</a>
<a class="btn btn-primary disabled" aria-disabled="true" role="button" data-bs-toggle="button">Disabled toggle link</a>
Nga tikanga
Ka taea e koe te hanga i tetahi tauira paatene me te kaihanga paatene, hei tauira:
const bsButton = new bootstrap.Button('#myButton')
Tikanga | Whakaahuatanga |
---|---|
dispose |
Ka whakakore i te paatene o te huānga. (Tangohia ai nga raraunga rongoa i te huānga DOM) |
getInstance |
Tikanga pateko e taea ai e koe te tiki i te tauira paatene e hono ana ki tetahi huānga DOM, ka taea e koe te whakamahi penei: bootstrap.Button.getInstance(element) . |
getOrCreateInstance |
Aratuka pateko e whakahoki mai ana i tetahi tauira patene e hono ana ki tetahi huānga DOM, ki te hanga i tetahi mea hou mena kaore i arawhitia. Ka taea e koe te whakamahi penei bootstrap.Button.getOrCreateInstance(element) :. |
toggle |
Ka takahuri te ahua pana. Ka hoatu ki te paatene te ahua kua whakahohehia. |
Hei tauira, ki te takahuri i nga paatene katoa
document.querySelectorAll('.btn').forEach(buttonElement => {
const button = bootstrap.Button.getOrCreateInstance(buttonElement)
button.toggle()
})
CSS
Taurangi
Kua taapirihia ki te v5.2.0Hei waahanga o nga huringa CSS e tipu haere ana a Bootstrap, ka whakamahia e nga paatene inaianei nga taurangi CSS o te rohe .btn
mo te whakapai ake i te waa-waa. Ko nga uara mo nga taurangi CSS ka whakatauhia ma te Sass, no reira kei te tautoko tonu te whakaritenga Sass.
--#{$prefix}btn-padding-x: #{$btn-padding-x};
--#{$prefix}btn-padding-y: #{$btn-padding-y};
--#{$prefix}btn-font-family: #{$btn-font-family};
@include rfs($btn-font-size, --#{$prefix}btn-font-size);
--#{$prefix}btn-font-weight: #{$btn-font-weight};
--#{$prefix}btn-line-height: #{$btn-line-height};
--#{$prefix}btn-color: #{$body-color};
--#{$prefix}btn-bg: transparent;
--#{$prefix}btn-border-width: #{$btn-border-width};
--#{$prefix}btn-border-color: transparent;
--#{$prefix}btn-border-radius: #{$btn-border-radius};
--#{$prefix}btn-hover-border-color: transparent;
--#{$prefix}btn-box-shadow: #{$btn-box-shadow};
--#{$prefix}btn-disabled-opacity: #{$btn-disabled-opacity};
--#{$prefix}btn-focus-box-shadow: 0 0 0 #{$btn-focus-width} rgba(var(--#{$prefix}btn-focus-shadow-rgb), .5);
Ka .btn-*
whakahouhia e ia karaehe whakarereke nga taurangi CSS e tika ana hei whakaiti i etahi atu ture CSS me o maatau button-variant()
, button-outline-variant()
, me nga button-size()
ranunga.
Anei tetahi tauira o te hanga i tetahi .btn-*
karaehe whakarereke ritenga penei i a maatau mo nga paatene motuhake ki a maatau tuhinga ma te tuku ano i nga taurangi CSS a Bootstrap me te ranunga o a maatau ake CSS me nga taurangi Sass.
.btn-bd-primary {
--bs-btn-font-weight: 600;
--bs-btn-color: var(--bs-white);
--bs-btn-bg: var(--bd-violet);
--bs-btn-border-color: var(--bd-violet);
--bs-btn-border-radius: .5rem;
--bs-btn-hover-color: var(--bs-white);
--bs-btn-hover-bg: #{shade-color($bd-violet, 10%)};
--bs-btn-hover-border-color: #{shade-color($bd-violet, 10%)};
--bs-btn-focus-shadow-rgb: var(--bd-violet-rgb);
--bs-btn-active-color: var(--bs-btn-hover-color);
--bs-btn-active-bg: #{shade-color($bd-violet, 20%)};
--bs-btn-active-border-color: #{shade-color($bd-violet, 20%)};
}
Taurangi Sass
$btn-padding-y: $input-btn-padding-y;
$btn-padding-x: $input-btn-padding-x;
$btn-font-family: $input-btn-font-family;
$btn-font-size: $input-btn-font-size;
$btn-line-height: $input-btn-line-height;
$btn-white-space: null; // Set to `nowrap` to prevent text wrapping
$btn-padding-y-sm: $input-btn-padding-y-sm;
$btn-padding-x-sm: $input-btn-padding-x-sm;
$btn-font-size-sm: $input-btn-font-size-sm;
$btn-padding-y-lg: $input-btn-padding-y-lg;
$btn-padding-x-lg: $input-btn-padding-x-lg;
$btn-font-size-lg: $input-btn-font-size-lg;
$btn-border-width: $input-btn-border-width;
$btn-font-weight: $font-weight-normal;
$btn-box-shadow: inset 0 1px 0 rgba($white, .15), 0 1px 1px rgba($black, .075);
$btn-focus-width: $input-btn-focus-width;
$btn-focus-box-shadow: $input-btn-focus-box-shadow;
$btn-disabled-opacity: .65;
$btn-active-box-shadow: inset 0 3px 5px rgba($black, .125);
$btn-link-color: var(--#{$prefix}link-color);
$btn-link-hover-color: var(--#{$prefix}link-hover-color);
$btn-link-disabled-color: $gray-600;
// Allows for customizing button radius independently from global border radius
$btn-border-radius: $border-radius;
$btn-border-radius-sm: $border-radius-sm;
$btn-border-radius-lg: $border-radius-lg;
$btn-transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
$btn-hover-bg-shade-amount: 15%;
$btn-hover-bg-tint-amount: 15%;
$btn-hover-border-shade-amount: 20%;
$btn-hover-border-tint-amount: 10%;
$btn-active-bg-shade-amount: 20%;
$btn-active-bg-tint-amount: 20%;
$btn-active-border-shade-amount: 25%;
$btn-active-border-tint-amount: 10%;
Sass mixins
E toru nga whakaurunga mo nga patene: patene me te patene whakahuahua whakarakei rereke (e rua i runga i te $theme-colors
), me te ranunga rahi o te paatene.
@mixin button-variant(
$background,
$border,
$color: color-contrast($background),
$hover-background: if($color == $color-contrast-light, shade-color($background, $btn-hover-bg-shade-amount), tint-color($background, $btn-hover-bg-tint-amount)),
$hover-border: if($color == $color-contrast-light, shade-color($border, $btn-hover-border-shade-amount), tint-color($border, $btn-hover-border-tint-amount)),
$hover-color: color-contrast($hover-background),
$active-background: if($color == $color-contrast-light, shade-color($background, $btn-active-bg-shade-amount), tint-color($background, $btn-active-bg-tint-amount)),
$active-border: if($color == $color-contrast-light, shade-color($border, $btn-active-border-shade-amount), tint-color($border, $btn-active-border-tint-amount)),
$active-color: color-contrast($active-background),
$disabled-background: $background,
$disabled-border: $border,
$disabled-color: color-contrast($disabled-background)
) {
--#{$prefix}btn-color: #{$color};
--#{$prefix}btn-bg: #{$background};
--#{$prefix}btn-border-color: #{$border};
--#{$prefix}btn-hover-color: #{$hover-color};
--#{$prefix}btn-hover-bg: #{$hover-background};
--#{$prefix}btn-hover-border-color: #{$hover-border};
--#{$prefix}btn-focus-shadow-rgb: #{to-rgb(mix($color, $border, 15%))};
--#{$prefix}btn-active-color: #{$active-color};
--#{$prefix}btn-active-bg: #{$active-background};
--#{$prefix}btn-active-border-color: #{$active-border};
--#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
--#{$prefix}btn-disabled-color: #{$disabled-color};
--#{$prefix}btn-disabled-bg: #{$disabled-background};
--#{$prefix}btn-disabled-border-color: #{$disabled-border};
}
@mixin button-outline-variant(
$color,
$color-hover: color-contrast($color),
$active-background: $color,
$active-border: $color,
$active-color: color-contrast($active-background)
) {
--#{$prefix}btn-color: #{$color};
--#{$prefix}btn-border-color: #{$color};
--#{$prefix}btn-hover-color: #{$color-hover};
--#{$prefix}btn-hover-bg: #{$active-background};
--#{$prefix}btn-hover-border-color: #{$active-border};
--#{$prefix}btn-focus-shadow-rgb: #{to-rgb($color)};
--#{$prefix}btn-active-color: #{$active-color};
--#{$prefix}btn-active-bg: #{$active-background};
--#{$prefix}btn-active-border-color: #{$active-border};
--#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
--#{$prefix}btn-disabled-color: #{$color};
--#{$prefix}btn-disabled-bg: transparent;
--#{$prefix}btn-disabled-border-color: #{$color};
--#{$prefix}gradient: none;
}
@mixin button-size($padding-y, $padding-x, $font-size, $border-radius) {
--#{$prefix}btn-padding-y: #{$padding-y};
--#{$prefix}btn-padding-x: #{$padding-x};
@include rfs($font-size, --#{$prefix}btn-font-size);
--#{$prefix}btn-border-radius: #{$border-radius};
}
Sass koropiko
Ko nga rereke o te paatene (mo nga paatene auau me te whakahuahua) ka whakamahi i o raatau whakaurunga me to maatau $theme-colors
mapi hei whakaputa i nga karaehe whakarereke i roto scss/_buttons.scss
.
@each $color, $value in $theme-colors {
.btn-#{$color} {
@if $color == "light" {
@include button-variant(
$value,
$value,
$hover-background: shade-color($value, $btn-hover-bg-shade-amount),
$hover-border: shade-color($value, $btn-hover-border-shade-amount),
$active-background: shade-color($value, $btn-active-bg-shade-amount),
$active-border: shade-color($value, $btn-active-border-shade-amount)
);
} @else if $color == "dark" {
@include button-variant(
$value,
$value,
$hover-background: tint-color($value, $btn-hover-bg-tint-amount),
$hover-border: tint-color($value, $btn-hover-border-tint-amount),
$active-background: tint-color($value, $btn-active-bg-tint-amount),
$active-border: tint-color($value, $btn-active-border-tint-amount)
);
} @else {
@include button-variant($value, $value);
}
}
}
@each $color, $value in $theme-colors {
.btn-outline-#{$color} {
@include button-outline-variant($value);
}
}