Last active
September 9, 2016 16:37
-
-
Save Bobz-zg/0bb51a5b89cc2ce67503b19204d61417 to your computer and use it in GitHub Desktop.
Simple jquery toggle function
This file contains hidden or 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
$.fn.toggler = function() { | |
return this.each( function() { | |
$(this).click(function(event) { | |
// Prevent click event on a tag | |
if ($(this).is('a')) { | |
if(event.preventDefault) { event.preventDefault(); } | |
} | |
$target = $(this).data('toggle') | |
$elem = $('#' + $target); | |
$height = $elem.prop('scrollHeight'); | |
// Remove inline max-height | |
if (typeof $elem.attr('style') !== typeof undefined && $elem.attr('style') !== false) { | |
$elem.removeAttr('style'); | |
} | |
else { | |
$elem.css('max-height', $height); | |
} | |
// Do something based on toggle target, eg scroll to position of element etc .. | |
switch ($target) { | |
case 'toggle-3': | |
$elem.toggleClass('active').css('background-color', 'red'); | |
break; | |
default: | |
$elem.toggleClass('active'); | |
break; | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment