Skip to content

Instantly share code, notes, and snippets.

@MaxPleaner
Last active August 19, 2016 22:24
Show Gist options
  • Save MaxPleaner/8e0df5a4ea51288b6a4d to your computer and use it in GitHub Desktop.
Save MaxPleaner/8e0df5a4ea51288b6a4d to your computer and use it in GitHub Desktop.
$.fn.SPA = function( options ) {
// default settings
var settings = $.extend({
toggling_sections: ".toggling",
base_link: "a[href^='#']",
}, options );
// setup initial page layout
$(settings["toggling_sections"]).hide();
// event listeners for links
$(settings["base_link"]).click(function(e){
var $el = $(e.currentTarget),
$target = $(("." + $el.attr("data-target")));
window.location.hash = $el.attr("name");
$target.fadeIn().addClass("active");
});
// Routing
var route = window.location.hash
var routeParts = route.slice(1).split("/")
routeParts.forEach(function(part){
$(settings["base_link_class"])
.find("[name='" + part + "']")
.trigger("click")
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment