Skip to content

Instantly share code, notes, and snippets.

@taea
Last active October 13, 2015 08:38
Show Gist options
  • Save taea/4169309 to your computer and use it in GitHub Desktop.
Save taea/4169309 to your computer and use it in GitHub Desktop.
listのn番目以降を隠したり表示したりするjQuery (gist:4169169) をCoffeeScriptにした
$ ->
contents = ".skilltags-more li" #対象のlist
trigger = ".skilltag-more-trigger" #スイッチ
contentsLength = $(contents).length #listの個数
n = 10 #初期状態で表出したいlistの個数
#listが10個以上ある場合は
if contentsLength > n
#n番目以降を隠す
i = n
while i < contentsLength
$(contents + ":eq(" + i + ")").hide()
i++
$(trigger).click ->
if $(contents + ":eq(" + n + ")").is(":hidden")
i = n
while i < contentsLength
$(contents + ":eq(" + i + ")").fadeIn "fast"
i++
$(trigger).html ["<a href=\"#\">", "<i class=\"icon-sort-up\"></i>", "閉じる", "</a>"].join("")
else
i = n
while i < contentsLength
$(contents + ":eq(" + i + ")").fadeOut "fast"
i++
$(trigger).html ["<a href=\"#\">", "<i class=\"icon-sort-down\"></i>", "もっと見る", "</a>"].join("")
#listがn個以下の場合はスイッチを隠す
else
$(trigger).hide()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment