One of my favorite Boostrap plugins is dropdowns, but I had a bit of difficulty getting started with them. The docs say you can apply a jq-dropdown to nearly any element, but they really seem to work best with Bootstrap’s navbar, tab, and pill components. Since I was retrofitting an app that was around long before Bootstrap, this wasn’t going to work for me. That and I was hoping to get a scrollable jq-dropdown (with tips!) and a few other features out of the deal. Thus, this plugin was born.
While the appearance is almost identical to those of Bootstrap’s, it’s important to note that JavaScript for this plugin was developed entirely from scratch. Some CSS was borrowed, however, especially for generating those gorgeous tips.
Wanna see what they look like? Try it! Wanna contribute to this project? It’s on GitHub
1. Start by including the necessary files into your app:
<link type="text/css" rel="stylesheet" href="jquery.dropdown.css" /> <script type="text/javascript" src="jquery.dropdown.js"></script>
2. Create a jq-dropdown by placing a div
with a unique id
and the jq-dropdown
class immediately before your closing body
tag.
For jq-dropdown menus, the div
should contain a ul
with the
jq-dropdown-menu
class:
<div id="jq-dropdown-1" class="jq-dropdown jq-dropdown-tip"> <ul class="jq-dropdown-menu"> <li><a href="#1">Item 1</a></li> <li><a href="#2">Item 2</a></li> <li><a href="#3">Item 3</a></li> <li class="jq-dropdown-divider"></li> <li><a href="#4">Item 4</a></li> <li><a href="#5">Item 5</a></li> <li><a href="#5">Item 6</a></li> </ul> </div>
For jq-dropdown panels, the div
should contain a div
with the
jq-dropdown-panel
class:
<div id="jq-dropdown-1" class="jq-dropdown jq-dropdown-tip"> <div class="jq-dropdown-panel"> You can put whatever HTML you want in a panel! </div> </div>
3. Add the data-jq-dropdown
attribute to any HTML element, using the id
of the
jq-dropdown as the value. That’s all you have to do–you don’t even have to initialize it! The plugin
also works on dynamically generated triggers.
<a href="#" data-jq-dropdown="#jq-dropdown-1">dropdown</a>
Dropdowns can be attached to any element that you want:
Here’s a link and here’s a button and here’s a span
Now let’s see a really long jq-dropdown that will scroll: Example
You can add form controls to your dropdowns: Example
Dropdowns can have regular HTML, too. Example
jq-dropdown-disabled
class to the trigger elementdata-horizontal-offset="10"
and/or data-vertical-offset="10"
to the trigger elementjq-dropdown-tip
class to the jq-dropdown elementjq-dropdown-anchor-right
class: Examplejq-dropdown-relative
class to the jq-dropdown containerYou probably won’t need these, but they’re there just in case:
attach
– attach a jq-dropdown to the selected trigger element(s); pass in the #jq-dropdown-id-1
as value
detach
– detach a jq-dropdown from the selected trigger element(s)enable
– enables the jq-dropdown and removes the jq-dropdown-disabled
class from the triggerdisable
– disables the jq-dropdown and adds the jq-dropdown-disabled
class to the triggerhide
– hide the jq-dropdownshow
– show the jq-dropdown
Example usage: $('#trigger').jqDropdown('method', [value]);
You can attach events to each jq-dropdown:
show
— triggered when the jq-dropdown is shownhide
— triggered when the jq-dropdown is hiddenExample usage:
$('#jq-dropdown-id-1').on('show', function(event, dropdownData) { console.log(dropdownData); }).on('hide', function(event, dropdownData) { console.log(dropdownData); });
jq-dropdown-anchor-right
class.
id
intact.
jq-dropdown-tip
class from your jq-dropdown.