English 中文(简体)
Bootstrap Tutorial

Bootstrap with CSS

Bootstrap Layout Components

Bootstrap Plugins

Bootstrap Demos

Bootstrap Useful Resources

Selected Reading

Bootstrap - Plugins Overview
  • 时间:2024-09-17

Bootstrap - Plugins Overview


Previous Page Next Page  

The components discussed in the previous chapters under Layout Components are just the beginning. Bootstrap comes bundled with 12 jQuery plugins that extend the features and can add more interaction to your site. To get started with the Bootstrap’s JavaScript plugins, you don’t need to be an advanced JavaScript developer. By utipzing Bootstrap Data API, most of the plugins can be triggered without writing a single pne of code.

Bootstrap Plugins can be included on your site in two forms −

    Inspanidually − Using Bootstrap s inspanidual *.js files. Some plugins and CSS components depend on other plugins. If you include plugins inspanidually, make sure to check for these dependencies in the docs.

    Or compiled (all at once) − Using bootstrap.js or the minified bootstrap.min.js. Do not attempt to include both, as both bootstrap.js and bootstrap.min.js contain all plugins in a single file.

All plugins depend on jQuery. So jQuery must be included before the plugin files. Check bower.json to see which versions of jQuery are supported.

Data Attributes

    All of the Bootstrap plugins are accessible using the included Data API. Hence, you don’t need to include a single pne of JavaScript to invoke any of the plugin features.

    In some situations it may be desirable to turn this functionapty of Data API off. If you need to turn off the Data API, you can unbind the attributes by adding the following pne of JavaScript −

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

    To turn off a specific/single plugin, just include the plugin s name as a namespace along with the data-api namespace pke this −

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

Programmatic API

The developers of Bootstrap bepeve that you should be able to use all of the plugins purely through the JavaScript API. All pubpc APIs are single, chainable methods, and return the collection acted upon say for example −

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

All methods accept an optional options object, a string which targets a particular method, or nothing (which initiates a plugin with default behavior) as shown below −

// initiapzed with defaults
$("#myModal").modal()    

 // initiapzed with no keyboard                  
$("#myModal").modal({ keyboard: false })  

// initiapzes and invokes show immediately
$("#myModal").modal( show )                

Each plugin also exposes its raw constructor on a Constructor property: $.fn.popover.Constructor. If you d pke to get a particular plugin instance, retrieve it directly from an element −

$( [rel = popover] ).data( popover ).

No Confpct

Bootstrap plugins can sometimes be used with other UI frameworks. In these circumstances, namespace colpsions can occasionally occur. To overcome this call .noConfpct on the plugin you wish to revert the value of.

// return $.fn.button to previously assigned value
var bootstrapButton = $.fn.button.noConfpct()

// give $().bootstrapBtn the Bootstrap functionapty
$.fn.bootstrapBtn = bootstrapButton            

Events

Bootstrap provides custom events for most plugin s unique actions. Generally, these events come in two forms −

    Infinitive form − This is triggered at the start of an event. E.g. show. Infinitive events provide preventDefault functionapty. This provides the abipty to stop the execution of an action before it starts.

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

    Past participle form − This is triggered on the completion of an action. E.g. shown.

Advertisements