Nje ya turubai
Unda pau zilizofichwa kwenye mradi wako kwa urambazaji, mikokoteni ya ununuzi, na zaidi ukitumia madarasa machache na programu-jalizi yetu ya JavaScript.
Inavyofanya kazi
Offcanvas ni sehemu ya upau wa kando ambayo inaweza kugeuzwa kupitia JavaScript ili kuonekana kutoka upande wa kushoto, kulia, juu, au ukingo wa chini wa kituo cha kutazama. Vifungo au nanga hutumika kama vichochezi ambavyo vimeambatishwa kwa vipengele maalum unavyogeuza, na data
sifa hutumika kuomba JavaScript yetu.
- Offcanvas hushiriki baadhi ya msimbo sawa wa JavaScript kama moduli. Kwa kweli, zinafanana kabisa, lakini ni programu-jalizi tofauti.
- Vile vile, viambajengo vingine vya chanzo vya Sass vya mitindo na vipimo vya offcanvas vinarithiwa kutoka kwa vigeu vya modal.
- Inapoonyeshwa, offcanvas inajumuisha mandhari chaguomsingi ambayo inaweza kubofya ili kuficha offcanvas.
- Sawa na moduli, ni turubai moja pekee inayoweza kuonyeshwa kwa wakati mmoja.
Vichwa juu! Kwa kuzingatia jinsi CSS inavyoshughulikia uhuishaji, huwezi kutumia margin
au translate
kwenye .offcanvas
kipengele. Badala yake, tumia darasa kama nyenzo huru ya kufunga.
prefers-reduced-motion
hoja ya midia. Tazama
sehemu ya mwendo iliyopunguzwa ya hati zetu za ufikivu .
Mifano
Vipengele vya Offcanvas
Ifuatayo ni mfano wa offcanvas ambao unaonyeshwa kwa chaguo-msingi (kupitia .show
kwenye .offcanvas
). Offcanvas inajumuisha matumizi ya kichwa kilicho na kitufe cha kufunga na darasa la hiari la mwili kwa baadhi ya awali padding
. Tunapendekeza kwamba ujumuishe vichwa vya offcanvas na vitendo vya kuondoa kila inapowezekana, au utoe hatua ya wazi ya kukataa.
Nje ya turubai
<div class="offcanvas offcanvas-start show" tabindex="-1" id="offcanvas" aria-labelledby="offcanvasLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasLabel">Offcanvas</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
Content for the offcanvas goes here. You can place just about any Bootstrap component or custom elements here.
</div>
</div>
Onyesho la moja kwa moja
Tumia vitufe vilivyo hapa chini ili kuonyesha na kuficha kipengele cha offcanvas kupitia JavaScript ambacho hugeuza .show
darasa kwenye kipengele na .offcanvas
darasa.
.offcanvas
huficha yaliyomo (chaguo-msingi).offcanvas.show
inaonyesha maudhui
Unaweza kutumia kiungo href
kilicho na sifa, au kitufe kilicho na data-bs-target
sifa. Katika visa vyote viwili, data-bs-toggle="offcanvas"
inahitajika.
Nje ya turubai
<a class="btn btn-primary" data-bs-toggle="offcanvas" href="#offcanvasExample" role="button" aria-controls="offcanvasExample">
Link with href
</a>
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasExample" aria-controls="offcanvasExample">
Button with data-bs-target
</button>
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasExampleLabel">Offcanvas</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div>
Some text as placeholder. In real life you can have the elements you have chosen. Like, text, images, lists, etc.
</div>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown">
Dropdown button
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
</div>
</div>
Kusonga kwa mwili
Usogezaji wa <body>
kipengele huzimwa wakati turubai na mandhari yake yanapoonekana. Tumia data-bs-scroll
sifa kuwezesha <body>
kusogeza.
Offcanvas na mwili scrolling
Jaribu kusogeza sehemu iliyobaki ya ukurasa ili kuona chaguo hili likiendelea.
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasScrolling" aria-controls="offcanvasScrolling">Enable body scrolling</button>
<div class="offcanvas offcanvas-start" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="offcanvasScrolling" aria-labelledby="offcanvasScrollingLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasScrollingLabel">Offcanvas with body scrolling</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<p>Try scrolling the rest of the page to see this option in action.</p>
</div>
</div>
Kusogeza kwa mwili na mandhari
Unaweza pia kuwezesha <body>
kusogeza kwa mandhari inayoonekana.
Mandhari yenye kusogeza
Jaribu kusogeza sehemu iliyobaki ya ukurasa ili kuona chaguo hili likiendelea.
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasWithBothOptions" aria-controls="offcanvasWithBothOptions">Enable both scrolling & backdrop</button>
<div class="offcanvas offcanvas-start" data-bs-scroll="true" tabindex="-1" id="offcanvasWithBothOptions" aria-labelledby="offcanvasWithBothOptionsLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasWithBothOptionsLabel">Backdrop with scrolling</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<p>Try scrolling the rest of the page to see this option in action.</p>
</div>
</div>
Mandhari tuli
Mandhari ya nyuma yanapowekwa kuwa tuli, turubai haitafungwa wakati wa kubofya nje yake.
Nje ya turubai
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#staticBackdrop" aria-controls="staticBackdrop">
Toggle static offcanvas
</button>
<div class="offcanvas offcanvas-start" data-bs-backdrop="static" tabindex="-1" id="staticBackdrop" aria-labelledby="staticBackdropLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="staticBackdropLabel">Offcanvas</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div>
I will not close if you click outside of me.
</div>
</div>
</div>
Turubai nyeusi
Imeongezwa katika v5.2.0Badilisha mwonekano wa turubai zilizo na huduma ili zilingane vyema na miktadha tofauti kama vile pau za giza. Hapa tunaongeza .text-bg-dark
kwa .offcanvas
na .btn-close-white
kwa .btn-close
styling sahihi na offcanvas giza. Ikiwa una menyu kunjuzi ndani, zingatia pia kuongeza .dropdown-menu-dark
kwa .dropdown-menu
.
Nje ya turubai
Weka maudhui ya nje ya turubai hapa.
<div class="offcanvas offcanvas-start show text-bg-dark" tabindex="-1" id="offcanvasDark" aria-labelledby="offcanvasDarkLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasDarkLabel">Offcanvas</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvasDark" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<p>Place offcanvas content here.</p>
</div>
</div>
Msikivu
Imeongezwa katika v5.2.0Madarasa ya kujibu ya offcanvas huficha maudhui nje ya kituo cha kutazama kutoka kwa sehemu maalum ya kugawanyika na chini. Juu ya sehemu hiyo ya kuvunja, yaliyomo ndani yatatenda kama kawaida. Kwa mfano, .offcanvas-lg
huficha maudhui kwenye turubai chini ya sehemu ya lg
kukatiza, lakini huonyesha maudhui yaliyo juu ya lg
kipenyo.
Msikivu offcanvas
Haya ni yaliyomo ndani ya .offcanvas-lg
.
<button class="btn btn-primary d-lg-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasResponsive" aria-controls="offcanvasResponsive">Toggle offcanvas</button>
<div class="alert alert-info d-none d-lg-block">Resize your browser to show the responsive offcanvas toggle.</div>
<div class="offcanvas-lg offcanvas-end" tabindex="-1" id="offcanvasResponsive" aria-labelledby="offcanvasResponsiveLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasResponsiveLabel">Responsive offcanvas</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#offcanvasResponsive" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<p class="mb-0">This is content within an <code>.offcanvas-lg</code>.</p>
</div>
</div>
Madarasa ya kuitikia ya offcanvas yanapatikana kote kwa kila sehemu ya kukagua.
.offcanvas
.offcanvas-sm
.offcanvas-md
.offcanvas-lg
.offcanvas-xl
.offcanvas-xxl
Uwekaji
Hakuna uwekaji chaguomsingi wa vijenzi vya offcanvas, kwa hivyo ni lazima uongeze mojawapo ya madarasa ya kurekebisha hapa chini.
.offcanvas-start
huweka turubai upande wa kushoto wa kituo cha kutazama (kilichoonyeshwa hapo juu).offcanvas-end
inaweka nje ya turubai upande wa kulia wa kituo cha kutazama.offcanvas-top
huweka nje ya turubai juu ya kituo cha kutazama.offcanvas-bottom
huweka turubai chini ya kituo cha kutazama
Jaribu mifano ya juu, kulia na ya chini hapa chini.
Juu ya turubai
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasTop" aria-controls="offcanvasTop">Toggle top offcanvas</button>
<div class="offcanvas offcanvas-top" tabindex="-1" id="offcanvasTop" aria-labelledby="offcanvasTopLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasTopLabel">Offcanvas top</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
...
</div>
</div>
Offcanvas kulia
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasRight" aria-controls="offcanvasRight">Toggle right offcanvas</button>
<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasRight" aria-labelledby="offcanvasRightLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasRightLabel">Offcanvas right</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
...
</div>
</div>
Chini ya turubai
<button class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasBottom" aria-controls="offcanvasBottom">Toggle bottom offcanvas</button>
<div class="offcanvas offcanvas-bottom" tabindex="-1" id="offcanvasBottom" aria-labelledby="offcanvasBottomLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasBottomLabel">Offcanvas bottom</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body small">
...
</div>
</div>
Ufikivu
Kwa kuwa kidirisha cha offcanvas kimawazo ni kidadisi cha mtindo, hakikisha kuwa aria-labelledby="..."
umeongeza - ukirejelea kichwa cha offcanvas - kwa .offcanvas
. Kumbuka kuwa huhitaji kuongeza role="dialog"
kwa kuwa tayari tunaiongeza kupitia JavaScript.
CSS
Vigezo
Imeongezwa katika v5.2.0Kama sehemu ya mkabala wa mabadiliko ya vigeu vya CSS ya Bootstrap, offcanvas sasa inatumia viwezo vya ndani vya CSS .offcanvas
ili kuboresha ubinafsishaji wa wakati halisi. Thamani za anuwai za CSS zimewekwa kupitia Sass, kwa hivyo ubinafsishaji wa Sass bado unatumika, pia.
--#{$prefix}offcanvas-zindex: #{$zindex-offcanvas};
--#{$prefix}offcanvas-width: #{$offcanvas-horizontal-width};
--#{$prefix}offcanvas-height: #{$offcanvas-vertical-height};
--#{$prefix}offcanvas-padding-x: #{$offcanvas-padding-x};
--#{$prefix}offcanvas-padding-y: #{$offcanvas-padding-y};
--#{$prefix}offcanvas-color: #{$offcanvas-color};
--#{$prefix}offcanvas-bg: #{$offcanvas-bg-color};
--#{$prefix}offcanvas-border-width: #{$offcanvas-border-width};
--#{$prefix}offcanvas-border-color: #{$offcanvas-border-color};
--#{$prefix}offcanvas-box-shadow: #{$offcanvas-box-shadow};
Vigezo vya Sass
$offcanvas-padding-y: $modal-inner-padding;
$offcanvas-padding-x: $modal-inner-padding;
$offcanvas-horizontal-width: 400px;
$offcanvas-vertical-height: 30vh;
$offcanvas-transition-duration: .3s;
$offcanvas-border-color: $modal-content-border-color;
$offcanvas-border-width: $modal-content-border-width;
$offcanvas-title-line-height: $modal-title-line-height;
$offcanvas-bg-color: $modal-content-bg;
$offcanvas-color: $modal-content-color;
$offcanvas-box-shadow: $modal-content-box-shadow-xs;
$offcanvas-backdrop-bg: $modal-backdrop-bg;
$offcanvas-backdrop-opacity: $modal-backdrop-opacity;
Matumizi
Programu-jalizi ya offcanvas hutumia madarasa na sifa chache kushughulikia unyanyuaji mzito:
.offcanvas
huficha yaliyomo.offcanvas.show
inaonyesha yaliyomo.offcanvas-start
huficha turubai upande wa kushoto.offcanvas-end
huficha turubai upande wa kulia.offcanvas-top
huficha turubai juu.offcanvas-bottom
huficha turubai chini
Ongeza kitufe cha kuondoa chenye data-bs-dismiss="offcanvas"
sifa, ambayo huanzisha utendaji wa JavaScript. Hakikisha umetumia <button>
kipengele nacho kwa tabia ifaayo kwenye vifaa vyote.
Kupitia sifa za data
Geuza
Ongeza data-bs-toggle="offcanvas"
na data-bs-target
au href
kwa kipengele ili kukabidhi kiotomatiki udhibiti wa kipengele kimoja cha nje ya turubai. Sifa data-bs-target
inakubali kiteuzi cha CSS cha kutumia turubai. Hakikisha umeongeza darasa offcanvas
kwenye kipengele cha offcanvas. Ikiwa ungependa ifungue chaguomsingi, ongeza darasa la ziada show
.
Ondoa
Kuachishwa kazi kunaweza kupatikana kwa data
sifa iliyo kwenye kitufe ndani ya turubai kama inavyoonyeshwa hapa chini:
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
au kwenye kitufe nje ya turubai kwa kutumia data-bs-target
kama inavyoonyeshwa hapa chini:
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#my-offcanvas" aria-label="Close"></button>
Kupitia JavaScript
Washa wewe mwenyewe na:
const offcanvasElementList = document.querySelectorAll('.offcanvas')
const offcanvasList = [...offcanvasElementList].map(offcanvasEl => new bootstrap.Offcanvas(offcanvasEl))
Chaguo
Kwa vile chaguo zinaweza kupitishwa kupitia sifa za data au JavaScript, unaweza kuongeza jina la chaguo kwa data-bs-
, kama katika data-bs-animation="{value}"
. Hakikisha umebadilisha aina ya kipochi cha jina la chaguo kutoka " camelCase " hadi " kebab-case " wakati wa kupitisha chaguo kupitia sifa za data. Kwa mfano, tumia data-bs-custom-class="beautifier"
badala ya data-bs-customClass="beautifier"
.
Kufikia Bootstrap 5.2.0, vipengee vyote vinaauni sifa ya data iliyohifadhiwa ya majaribiodata-bs-config
ambayo inaweza kuweka usanidi wa sehemu rahisi kama mfuatano wa JSON. Wakati kipengele kina data-bs-config='{"delay":0, "title":123}'
na data-bs-title="456"
sifa, title
thamani ya mwisho itakuwa 456
na sifa tofauti za data zitabatilisha thamani zilizotolewa kwenye data-bs-config
. Kwa kuongezea, sifa zilizopo za data zinaweza kuweka maadili ya JSON kama data-bs-delay='{"show":0,"hide":150}'
.
Jina | Aina | Chaguomsingi | Maelezo |
---|---|---|---|
backdrop |
boolean au kambastatic |
true |
Tumia mandhari kwenye mwili wakati offcanvas imefunguliwa. Vinginevyo, bainisha static kwa mandhari ambayo haifungi turubai inapobofya. |
keyboard |
boolean | true |
Hufunga turubai wakati kitufe cha Escape kinapobozwa. |
scroll |
boolean | false |
Ruhusu usogezaji wa mwili wakati offcanvas imefunguliwa. |
Mbinu
Njia za Asynchronous na mabadiliko
Njia zote za API ni za asynchronous na zinaanzisha mpito . Wanarudi kwa mpigaji mara tu mpito unapoanza lakini kabla haujaisha . Kwa kuongeza, simu ya mbinu kwenye sehemu ya mpito itapuuzwa .
Huwasha maudhui yako kama kipengele cha nje ya turubai. Inakubali chaguo za hiari object
.
Unaweza kuunda mfano wa offcanvas na mjenzi, kwa mfano:
const bsOffcanvas = new bootstrap.Offcanvas('#myOffcanvas')
Njia | Maelezo |
---|---|
getInstance |
Njia tuli ambayo hukuruhusu kupata mfano wa offcanvas unaohusishwa na kipengee cha DOM. |
getOrCreateInstance |
Mbinu tuli inayokuruhusu kupata mfano wa offcanvas unaohusishwa na kipengee cha DOM, au uunde kipya endapo hakijaanzishwa. |
hide |
Huficha kipengele cha offcanvas. Hurejesha kwa anayepiga kabla kipengele cha offcanvas hakijafichwa (yaani kabla ya hidden.bs.offcanvas tukio kutokea). |
show |
Inaonyesha kipengele cha offcanvas. Hurejesha kwa anayepiga kabla kipengele cha offcanvas hakijaonyeshwa (yaani kabla ya shown.bs.offcanvas tukio kutokea). |
toggle |
Hugeuza kipengele cha offcanvas kuonyeshwa au kufichwa. Hurejesha kwa anayepiga kabla kipengele cha offcanvas hakijaonyeshwa au kufichwa (yaani kabla ya tukio shown.bs.offcanvas au hidden.bs.offcanvas tukio). |
Matukio
Darasa la offcanvas la Bootstrap linafichua matukio machache ya kuunganisha kwenye utendaji wa offcanvas.
Aina ya tukio | Maelezo |
---|---|
hide.bs.offcanvas |
Tukio hili linafukuzwa mara moja wakati hide njia imeitwa. |
hidden.bs.offcanvas |
Tukio hili linafutwa wakati kipengele cha offcanvas kimefichwa kutoka kwa mtumiaji (itasubiri mabadiliko ya CSS kukamilika). |
hidePrevented.bs.offcanvas |
Tukio hili hutupwa wakati turubai inaonyeshwa, mandhari yake ni static na kubofya nje ya turubai kutekelezwa. Tukio pia huwashwa wakati kitufe cha kutoroka kinapobonyezwa na keyboard chaguo limewekwa kuwa false . |
show.bs.offcanvas |
Tukio hili huwaka mara moja wakati show njia ya mfano inaitwa. |
shown.bs.offcanvas |
Tukio hili linafutwa wakati kipengele cha offcanvas kimefanywa kuonekana kwa mtumiaji (itasubiri mabadiliko ya CSS kukamilika). |
const myOffcanvas = document.getElementById('myOffcanvas')
myOffcanvas.addEventListener('hidden.bs.offcanvas', event => {
// do something...
})