Bootstrap Plugins Overview
In the previous Layout widgets chapter, the components discussed are just the beginning. Bootstrap comes with 12 jQuery plugins that extend functionality and add more interactivity to a site. Even if you are not an advanced JavaScript developer, you can start learning Bootstrap's JavaScript plugins. With the Bootstrap Data API (Bootstrap Data API), most plugins can be triggered without writing any code.
There are two ways for a site to reference Bootstrap plugins:
- Individual Inclusion: use the individual *.js files. Some plugins and CSS components depend on other plugins. If you include plugins individually, first make sure you understand the dependencies between them.
- Compiled (Simultaneous) Inclusion: When using bootstrap.js or the compressed version bootstrap.min.js。
Do not try to include both files at the same time, because bootstrap.js and bootstrap.min.js both contain all the plugins.
All plugins depend on jQuery, so jQuery must be included before the plugin files. Please visit bower.json to see the jQuery versions currently supported by Bootstrap.
The data attribute
- You can use all Bootstrap plugins simply through the data attribute API, without writing a single line of JavaScript code. This is the first-class API in Bootstrap and should be your preferred approach.
- That said, in some cases you may need to turn this feature off. Therefore, we also provide a way to disable the data attribute API, by unbinding events under the namespace of data-api that are bound to the document, like this:
$(document).off('.data-api')
$(document).off('.alert.data-api')
Programmatic API
We provide a pure JavaScript API for all Bootstrap plugins. All public APIs support individual or chained calls and return the collection of elements they operate on (note: consistent with jQuery's calling style). For example:
$(".btn.danger").button("toggle").addClass("fat")
All methods can accept an optional options object as a parameter, or a string representing a specific method, or no parameters at all (in which case the plugin is initialized with default behavior), as shown below:
// Initialize to default behavior
$("#myModal").modal()
// Initialize as keyboard unsupported
$("#myModal").modal({ keyboard: false })
// Initialize and immediately call show
$("#myModal").modal('show')
Each plugin also exposes its raw constructor on the Constructor property:$.fn.popover.Constructor. If you want to get an instance of a specific plugin, you can get it directly through the page element:
$('[rel=popover]').data('popover').
Avoiding Namespace Conflicts
Sometimes Bootstrap plugins may need to be used together with other UI frameworks. In such cases, namespace conflicts may occur. If this unfortunately happens, you can call the plugin's .noConflict method to restore its original value.
// Return the value assigned before $.fn.button
var bootstrapButton = $.fn.button.noConflict()
// Assign Bootstrap functionality to $().bootstrapBtn
$.fn.bootstrapBtn = bootstrapButton
Events
Bootstrap provides custom events for the unique behaviors of most plugins. Generally, these events come in two forms:
- Infinitive Verb Form: this is triggered at the start of the event. For example ex: show. Infinitive events provide the preventDefault feature. This makes it possible to stop the execution of the operation before the event begins.
$('#myModal').on('show.bs.modal', function (e) { // Prevent modal from showing if (!data) return e.preventDefault() })