Created
January 13, 2018 23:12
-
-
Save dboutote/e1fa1efcaeac3d651de0b329a75bc0af to your computer and use it in GitHub Desktop.
Toggle the height of Unordered or Ordered Lists
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($) { | |
$( document ).ready(function() { | |
var $sideNavs = $(); | |
$.fn.toggleHeight = function( options ){ | |
if( 'ontouchstart' in document ) return this; | |
$sideNavs = $sideNavs.add(this); | |
return this.each( function (){ | |
var $list = $( this ), | |
$lis = $list.find('li').length, | |
defaults = { | |
more: "More +", | |
less: "Less -", | |
length: 4, | |
}, | |
settings = $.extend( true, {}, defaults, options ); | |
if( $lis > settings.length ){ | |
$( 'li', $list ).eq( (settings.length - 1) ).nextAll().hide().addClass('toggleable'); | |
$list.append('<li class="view-more"><a class="more" href="#">'+settings.more+'</a></li>'); | |
} | |
$list.on( 'click', '.more', function( e ) { | |
var $link = $( e.currentTarget ); | |
e.preventDefault(); | |
if( $link.hasClass( 'less' ) ){ | |
$link.html(settings.more).removeClass('less'); | |
}else{ | |
$link.html(settings.less).addClass('less'); | |
} | |
$link.parent().siblings('li.toggleable').slideToggle('fast'); | |
}); | |
}); | |
} | |
$('.link-list').toggleHeight( {length: 4} ); | |
}); | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment