Skip to content

Instantly share code, notes, and snippets.

@gangsthub
Last active May 4, 2016 19:30
Show Gist options
  • Save gangsthub/157a833258b43cfbeb64148b6d0dcc7c to your computer and use it in GitHub Desktop.
Save gangsthub/157a833258b43cfbeb64148b6d0dcc7c to your computer and use it in GitHub Desktop.
Drop it like its hot
$(function(){
// dropdown function to pass to "dropdown actions"
// if you want the containers to be shown by default, add to the the class `.shown` in the markup
// Author: Paul Melero
// Url: https://gist.github.com/gangsthub/157a833258b43cfbeb64148b6d0dcc7c/
// Scroll func
var scrollSpeed = 500,
isShownByDefault = false;
// Dropdown Func
var dropdown = function(link, container, isShownByDefault, scrollSpeed) {
container.each(function() {
$this = $( this );
$this.addClass('toggle-dropdown-container');
link.dataClicked = isShownByDefault;
function dropItLikeItsHot() {
var $this = $(this);
link.toggleClass('clicked');
container.toggleClass('shown');
// aria toggle
container.attr('aria-hidden', container.hasClass('shown') ? 'false' : 'true');
// Toggle Boolean value
link.dataClicked = !link.dataClicked;
// Is it a link with toggleable text? (Kinda special case)
if (link.attr('data-hide-text')) {
link.html(link.dataClicked == true ? link.attr('data-hide-text') : link.attr('data-show-text'));
}
// Move your ass up there if the action is "Show"
if (link.dataClicked) {
(function scroll() {
$('html, body').animate({
scrollTop: $(container).offset().top - 50
}, scrollSpeed);
})();
}
}
link.bind('click', function(e) {
e = e || event;
e.preventDefault();
dropItLikeItsHot();
e.stopPropagation();
});
});
};
// Samples: Pass the joint!
// var defaultLinkClass = $('.desc__drop-action');
// dropdown(defaultLinkClass, $('.container__element')); // Hidden by default
// dropdown($('.another-container__link'), $('.another-container__element'), true); // Shown by default
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment