From cc3df84442cc78a7af99c793485185829ccdcc4d Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Wed, 26 Nov 2014 03:49:02 -0800 Subject: fix scrolling for reals --- .../demos/src/main/scala/scrollmenu/ScrollMenu.scala | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala b/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala index 7d718e2..1938c67 100644 --- a/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala +++ b/examples/demos/src/main/scala/scrollmenu/ScrollMenu.scala @@ -68,6 +68,7 @@ class ScrollSpy(structure: Tree[String], dom.requestAnimationFrame((d: Double) => start()) } } + private[this] var previousId = "" private[this] def start() = { scrolling = false def scroll(el: dom.Element) = { @@ -95,15 +96,15 @@ class ScrollSpy(structure: Tree[String], winFound = winFound | newWinFound } if (!winFound) { - // This means it's the leaf element, because it won but there - // aren't any children which won, so it must be the actual leaf - tree.children.foreach(_.value.frag.classList.remove("selected")) + if (previousId != itemId){ + previousId = itemId + // This means it's the leaf element, because it won but there + // aren't any children which won, so it must be the actual leaf + tree.children.foreach(_.value.frag.classList.remove("selected")) + scroll(menuItem.children(0)) - val top = main.scrollTop - dom.location.hash = itemId - main.scrollTop = top - - scroll(menuItem.children(0)) + dom.history.replaceState(null, null, "#" + itemId) + } } menuItem.children(0).classList.add("pure-menu-selected") }else{ -- cgit v1.2.3