Source

JavaSkript fɔ di Baybul

Bring Bootstrap to layf wit wi opshɔnal JavaSkript plɔgin dɛn we dɛn bil pan jQuery. Lan bɔt ɛni plɔgin, wi data ɛn programmatik API opshɔn dɛn, ɛn ɔda tin dɛn.

Wan wan ɔ we dɛn dɔn kɔmpilayt

Plɔgin dɛn kin inklud wan wan (yuz Bootstrap in wan wan js/dist/*.js), ɔ ɔl wan tɛm yuz bootstrap.jsɔ di minified bootstrap.min.js(nɔ inklud ɔl tu).

If yu yuz bundler (Webpack, Rollup...), yu kin yuz /js/dist/*.jsfayl dɛn we dɔn rɛdi fɔ UMD.

Dipɛnsin dɛn

Sɔm plɔgin dɛn ɛn CSS kɔmpɔnɛnt dɛn de dipen pan ɔda plɔgin dɛn. If yu put plɔgin dɛn wan bay wan, mek shɔ se yu chɛk fɔ dɛn dipɛnsin dɛn ya na di dɔkyumɛnt dɛn. Notis bak se ɔl di plɔgin dɛn dipen pan jQuery (dis min se jQuery fɔ de bifo di plɔgin fayl dɛn). Kɔnsul wipackage.json fɔ si us vɛshɔn dɛn fɔ jQuery dɛn de sɔpɔt.

Wi dropdowns, popovers ɛn tultips dɛn bak de dipen pan Popper.js .

Data atribyut dɛn

Klose to ɔl di Bootstrap plɔgin dɛn kin ɛnabul ɛn kɔnfigyut tru HTML nɔmɔ wit data atribyut dɛn (wi we wi lɛk fɔ yuz JavaSkript funkshɔnaliti). Mek shɔ se yu jɔs yuz wan sɛt fɔ data atribyut dɛn pan wan ɛlimɛnt (ɛgz., yu nɔ go ebul fɔ trig wan tultip ɛn modal frɔm di sem bɔtin.)

Bɔt, insay sɔm tin dɛn, i kin fayn fɔ mek dɛn disable dis wok. Fɔ disable di data atribyut API, unbind ɔl di ivintɛns dɛn na di dɔkyumɛnt nemspɛs wit data-apilɛk so:

$(document).off('.data-api')

Ɔda we de fɔ du dat, fɔ tɔch wan patikyula plɔgin, jɔs put di plɔgin in nem as nemspɛs wit di data-api nemspɛs lɛk dis:

$(document).off('.alert.data-api')
Slɛktɔ dɛn we de rɔnawe

If yu yuz spɛshal sɛlɛktɔ dɛn, fɔ ɛgzampul: collapse:Example, mek shɔ se yu rɔnawe pan dɛn, bikɔs dɛn go pas dɛn tru jQuery.

Di tin dɛn we kin apin

Bootstrap de gi kɔstɔm ivin fɔ bɔku pan di plɔgin dɛn yunik akshɔn dɛn. Jɛnɛral wan, dɛn wan ya kin kam insay wan infinitiv ɛn past patisipul fɔm - usay di infinitiv (ɛks. show) kin trig we wan ivin bigin, ɛn in past patisipul fɔm (ɛks. shown) kin trig we wan akshɔn dɔn.

Ɔl di infinitiv ivin dɛn de gi preventDefault()funkshɔnaliti. Dis de gi di abiliti fɔ stɔp di ɛgzikishɔn fɔ wan akshɔn bifo i bigin. Ritɔn false frɔm wan ivent handla go kɔl bak ɔtomɛtik wan preventDefault().

$('#myModal').on('show.bs.modal', function (e) {
  if (!data) return e.preventDefault() // stops modal from being shown
})

Programmetik API fɔ di wan dɛn we de wok

Wi biliv bak se yu fɔ ebul fɔ yuz ɔl di Bootstrap plɔgin dɛn jɔs tru di JavaSkript API. Ɔl di pɔblik API dɛn na wan, chenj we dɛn kin yuz fɔ du tin, ɛn dɛn kin gi bak di kɔleshiɔn we dɛn akt pan.

$('.btn.danger').button('toggle').addClass('fat')

Ɔl di we dɛn fɔ aksept wan opshɔnal opshɔn ɔbjɛkt, wan string we de tɔch wan patikyula we, ɔ natin (we de bigin wan plɔgin wit difɔlt bihayvya):

$('#myModal').modal()                      // initialized with defaults
$('#myModal').modal({ keyboard: false })   // initialized with no keyboard
$('#myModal').modal('show')                // initializes and invokes show immediately

Ɛni plɔgin de ɛksplɔz in raw kɔnstrɔkta bak pan wan Constructorprɔpati: $.fn.popover.Constructor. If yu want fɔ gɛt wan patikyula plɔgin instans, tek am dairekt frɔm wan ɛlimɛnt: $('[rel="popover"]').data('popover').

Asynchronous fכnshכn dεm εn transishכn dεm

Ɔl di programmatik API mɛtɔd dɛn na asynchronous ɛn dɛn de go bak to di pɔsin we kɔl wans di transishɔn dɔn stat bɔt bifo i dɔn .

Fɔ mek yu ebul fɔ ɛksɛkutiv wan akshɔn wans di chenj dɔn, yu kin lisin to di kɔrɛspɔndɛns ivin.

$('#myCollapse').on('shown.bs.collapse', function (e) {
  // Action to execute once the collapsible area is expanded
})

Apat frɔm dat wan mɛtɔd kɔl pan wan transishɔn kɔmpɔnɛnt go ignore .

$('#myCarousel').on('slid.bs.carousel', function (e) {
  $('#myCarousel').carousel('2') // Will slide to the slide 2 as soon as the transition to slide 1 is finished
})

$('#myCarousel').carousel('1') // Will start sliding to the slide 1 and returns to the caller
$('#myCarousel').carousel('2') // !! Will be ignored, as the transition to the slide 1 is not finished !!

Difɔlt sɛtin dɛn

Yu kin chenj di difɔlt sɛtin fɔ wan plɔgin bay we yu chenj di tin we di plɔgin de Constructor.Defaultyuz:

$.fn.modal.Constructor.Default.keyboard = false // changes default for the modal plugin's `keyboard` option to false

Nɔ kɔnflikt nɔ de

Sɔntɛnde i nid fɔ yuz Bootstrap plɔgin dɛn wit ɔda UI fremwɔk dɛn. Insay dɛn kayn tin ya, nemspɛs kɔlishin kin apin wan wan tɛm. If dis apin, yu kin kɔl .noConflictdi plɔgin we yu want fɔ gi bak di valyu fɔ.

var bootstrapButton = $.fn.button.noConflict() // return $.fn.button to previously assigned value
$.fn.bootstrapBtn = bootstrapButton            // give $().bootstrapBtn the Bootstrap functionality

Vɛshɔn nɔmba dɛn

Di vɛshɔn fɔ ɛni wan pan Bootstrap in jQuery plɔgin dɛn kin akses tru di VERSIONprɔpati fɔ di plɔgin in kɔnstrɔkta. Fɔ ɛgzampul, fɔ di tultip plɔgin:

$.fn.tooltip.Constructor.VERSION // => "4.1.3"

Nɔ spɛshal fɔl bak we dɛn nɔ yuz JavaSkript

Bootstrap in plɔgin dɛn nɔ de fɔdɔm bak patikyula grasful wan we JavaSkript nɔ de wok. If yu bisin bɔt di yuza ɛkspiriɛns na dis kes, yuz <noscript>fɔ ɛksplen di sityueshɔn (ɛn aw fɔ ri-ɛnabul JavaSkript) to yu yuza dɛn, ɛn/ɔ ad yu yon kɔstɔm fɔlbak dɛn.

Tɔd-pati laybri dɛn

Bootstrap nɔ ɔfishal wan sɔpɔt tɔd-pati JavaSkript laybri dɛn lɛk Prototype ɔ jQuery UI. Pan ɔl .noConflictwe tin dɛn kin apin ɛn we gɛt nem-spɛs, prɔblɛm dɛn kin de we gɛt fɔ du wit di kɔmpitishɔn we yu nid fɔ fiks fɔ yusɛf.

Yutil

Ɔl di Bootstrap in JavaSkript fayl dɛn dipen pan util.jsɛn i gɛt fɔ de nia di ɔda JavaSkript fayl dɛn. If yu de yuz di kɔmpilayt (ɔ minified) bootstrap.js, nid nɔ de fɔ put dis insay—i dɔn ɔlrɛdi de.

util.jsinklud yutiliti fɛnshɔn dɛn ɛn wan besik ɛp fɔ transitionEndivintɛns dɛn ɛn bak wan CSS transishɔn ɛmulatɔ. Na di ɔda plɔgin dɛn de yuz am fɔ chɛk fɔ CSS transishɔn sɔpɔt ɛn fɔ kech transishɔn dɛn we de hang.