twitter-bootstrap-hover-dropdown.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Project: Twitter Bootstrap Hover Dropdown
  3. * Author: Cameron Spear
  4. * Contributors: Mattia Larentis
  5. *
  6. * Dependencies?: Twitter Bootstrap's Dropdown plugin
  7. *
  8. * A simple plugin to enable twitter bootstrap dropdowns to active on hover and provide a nice user experience.
  9. *
  10. * No license, do what you want. I'd love credit or a shoutout, though.
  11. *
  12. * http://cameronspear.com/blog/twitter-bootstrap-dropdown-on-hover-plugin/
  13. */
  14. ;(function($, window, undefined) {
  15. // outside the scope of the jQuery plugin to
  16. // keep track of all dropdowns
  17. var $allDropdowns = $();
  18. // if instantlyCloseOthers is true, then it will instantly
  19. // shut other nav items when a new one is hovered over
  20. $.fn.dropdownHover = function(options) {
  21. // the element we really care about
  22. // is the dropdown-toggle's parent
  23. $allDropdowns = $allDropdowns.add(this.parent());
  24. return this.each(function() {
  25. var $this = $(this).parent(),
  26. defaults = {
  27. delay: 500,
  28. instantlyCloseOthers: true
  29. },
  30. data = {
  31. delay: $(this).data('delay'),
  32. instantlyCloseOthers: $(this).data('close-others')
  33. },
  34. options = $.extend(true, {}, defaults, options, data),
  35. timeout;
  36. $this.hover(function() {
  37. if(options.instantlyCloseOthers === true)
  38. $allDropdowns.removeClass('open');
  39. window.clearTimeout(timeout);
  40. $(this).addClass('open');
  41. }, function() {
  42. timeout = window.setTimeout(function() {
  43. $this.removeClass('open');
  44. }, options.delay);
  45. });
  46. });
  47. };
  48. $('[data-hover="dropdown"]').dropdownHover();
  49. })(jQuery, this);