From 6bb7861bddabe621ad7c8fc31b76241fb4456899 Mon Sep 17 00:00:00 2001 From: Mathieu Duponchelle Date: Fri, 3 Apr 2015 12:49:09 +0200 Subject: [PATCH] clean up event handlers and helper divs. + This allows to call the plugin again when the content of the page has changed. --- src/javascripts/jquery.tocify.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/javascripts/jquery.tocify.js b/src/javascripts/jquery.tocify.js index f464d95..45d199e 100644 --- a/src/javascripts/jquery.tocify.js +++ b/src/javascripts/jquery.tocify.js @@ -143,6 +143,9 @@ self.extendPageScroll = true; + // Getting rid of possibly existing helper divs + $("[data-unique]").remove(); + // Internal array that keeps track of all TOC items (Helps to recognize if there are duplicate TOC item strings) self.items = []; @@ -515,9 +518,12 @@ $self, // Instantiates a new variable that will be used to determine the smoothScroll animation time duration - duration; + duration, + + li = this.element.find("li"); // Event delegation that looks for any clicks on list item elements inside of the HTML element calling the plugin + this.element.off("click.tocify", "li"); this.element.on("click.tocify", "li", function(event) { if(self.options.history) { @@ -546,7 +552,9 @@ }); // Mouseenter and Mouseleave event handlers for the list item's within the HTML element calling the plugin - this.element.find("li").on({ + li.off("mouseenter.tocify"); + li.off("mouseleave.tocify"); + li.on({ // Mouseenter event handler "mouseenter.tocify": function() { @@ -576,6 +584,7 @@ if (self.options.extendPage || self.options.highlightOnScroll || self.options.scrollHistory || self.options.showAndHideOnScroll) { // Window scroll event handler + $(window).off("scroll.tocify"); $(window).on("scroll.tocify", function() { // Once all animations on the page are complete, this callback function will be called