Last active
December 22, 2015 23:39
-
-
Save lars-erik/6548389 to your computer and use it in GitHub Desktop.
Adds ... to too long lis.
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
$.fn.ellipsisUl = function() { | |
var sum = function(array, selector) { | |
var sum = 0; | |
array.each(function(i, e) { | |
sum += selector($(e)); | |
}); | |
return sum; | |
}, | |
paddingLeft = function(e) { | |
return Number(e.css("padding-left").replace("px", "")); | |
}; | |
$(this).each(function(i, ul) { | |
$("a", ul).each(function(i, a) { | |
var its = $(a), | |
orgWidth = its.width(), | |
uls = its.parents("ul"), | |
max = uls.width(), | |
pad = sum(uls, paddingLeft), | |
padMore = 30, | |
newWidth = max - pad - padMore; | |
if (orgWidth > max - pad - padMore) { | |
its.width(newWidth); | |
its.after($("<span>...</span>")); | |
} | |
}); | |
}); | |
}; |
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
ul { | |
width: 150px; | |
overflow: hidden; | |
border: 1px solid gray; | |
padding-left: 0px; | |
position: relative; | |
display: block; | |
margin: 0px; | |
} | |
ul ul { | |
padding-left: 20px; | |
} | |
a { | |
display: inline-block; | |
background-color: pink; | |
overflow: hidden; | |
white-space: nowrap; | |
} |
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
<ul class="ellipsisUl"> | |
<li> | |
<a href="#">blablablablablablablaasdf asdf asdf </a> | |
<ul> | |
<li> | |
<a href="#">lakjsdf ølaksjd følkajs df</a> | |
</li> | |
<li> | |
<a href="#">lakjsdf</a> | |
</li> | |
</ul> | |
</li> | |
</ul> | |
<script type="text/javascript"> | |
$(".ellipsisUl").ellipsisUl(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment