Skip to content

Instantly share code, notes, and snippets.

@torounit
Last active August 29, 2015 14:01
Show Gist options
  • Save torounit/4f2f315c35bf2a0e2716 to your computer and use it in GitHub Desktop.
Save torounit/4f2f315c35bf2a0e2716 to your computer and use it in GitHub Desktop.
URLをParseして、適当にクラスをつける。適当実装。
do($=jQuery) =>
$(".l-navigation__item").find("a").each ->
origin = location.origin
href = @.href.replace origin, ""
dir = href.replace "index.html", ""
if dir == "/" or dir == ""
#TOPの場合。
pathname = location.pathname.replace "index.html", ""
if pathname == "/" or pathname == ""
$(@).addClass("current")
else
#通常の処理
if location.pathname.indexOf(dir) != -1
$(@).addClass("current")
#URL構造と、サイト構造が合っていない場合。。。/hogeにいるときに、piyoへのリンクをcurrentへ
else if location.pathname.indexOf("/^hoge/") == 0 and dir.indexOf("piyo") != -1
$(@).addClass("current")
@torounit
Copy link
Author

(function(_this) {
  return (function($) {
    return $(".l-navigation__item").find("a").each(function() {
      var dir, href, origin, pathname;
      origin = location.origin;
      href = this.href.replace(origin, "");
      dir = href.replace("index.html", "");
      if (dir === "/" || dir === "") {
        pathname = location.pathname.replace("index.html", "");
        if (pathname === "/" || pathname === "") {
          return $(this).addClass("current");
        }
      } else {
        if (location.pathname.indexOf(dir) !== -1) {
          return $(this).addClass("current");
        } else if (location.pathname.indexOf("/^hoge/") === 0 && dir.indexOf("piyo") !== -1) {
          return $(this).addClass("current");
        }
      }
    });
  });
})(this)(jQuery);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment