Created
June 23, 2011 23:07
-
-
Save johanbrook/1043851 to your computer and use it in GitHub Desktop.
Quick jQuery dropdown plugin. Bind to link trigger. View available options below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
/** | |
* Simple dropdown plugin | |
* By Johan | |
*/ | |
$.fn.toggleDropdown = function(options){ | |
var settings = { | |
activeClass: "dropdown-active", | |
menuSelector: ".dropdown-menu", | |
closeSelector: ".close" | |
}; | |
if(options) | |
settings = $.extend({}, settings, options); | |
return this.each(function(){ | |
var trigger = $(this), | |
menu = trigger.next(settings.menuSelector), | |
close = menu.find(settings.closeSelector) || null; | |
trigger.click(function(evt) { | |
evt.preventDefault(); | |
menu.toggle(); | |
trigger.toggleClass(settings.activeClass); | |
}); | |
if(close){ | |
close.click(function(evt){ | |
evt.preventDefault(); | |
trigger.removeClass(settings.activeClass); | |
menu.hide(); | |
}); | |
} | |
menu.mouseup(function() { | |
return false | |
}); | |
$(document).mouseup(function(evt) { | |
if($(evt.target).parent(trigger[0].className).length == 0) { | |
trigger.removeClass(settings.activeClass); | |
menu.hide(); | |
} | |
}); | |
}); | |
}; | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<script src="jquery.min.js"></script> | |
<script src="jquery.toggleDropdown"></script> | |
<!-- Usage: --> | |
<script> | |
$(function(){ | |
$("a.dropdown").toggleDropdown(); | |
}); | |
</script> | |
<style media="screen"> | |
.dropdown-menu{ | |
position: absolute; | |
display: none; | |
/* Fine tune this: */ | |
top: 0; | |
right: 0; | |
z-index: 99; | |
} | |
</style> | |
</head> | |
<body> | |
<div style="position: relative;"> | |
<a class="dropdown" href="#">Show dropdown</a> | |
<div class="dropdown-menu"> | |
<h2>Content</h2> | |
<p>Lorem</p> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment