From 4ce118d60f06b50a7c8d2cb5b166a8ab83f3dde2 Mon Sep 17 00:00:00 2001 From: fy Date: Wed, 30 Aug 2017 21:48:36 +0800 Subject: [PATCH] fixed sidebar item inactive issue & fixed _sidebar.md 404 issue & smooth scroll (#242) * fixed sidebar item inactive issue & fixed _sidebar.md 404 issue & smooth scroll * sidebar load fix --- package.json | 1 + src/core/event/scroll.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 86f1e2f1..665487db 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "marked": "^0.3.6", "prismjs": "^1.6.0", "tinydate": "^1.0.0", + "tweezer.js": "^1.4.0", "zoom-image": "^0.1.4" }, "devDependencies": { diff --git a/src/core/event/scroll.js b/src/core/event/scroll.js index 3c833dcd..d5d1e620 100644 --- a/src/core/event/scroll.js +++ b/src/core/event/scroll.js @@ -1,15 +1,33 @@ import { isMobile } from '../util/env' import * as dom from '../util/dom' +import Tweezer from 'tweezer.js' const nav = {} let hoverOver = false +let scroller = null +let enableScrollEvent = true + +function scrollTo (el) { + if (scroller) scroller.stop(); + enableScrollEvent = false; + scroller = new Tweezer({ + start: window.scrollY, + end: el.getBoundingClientRect().top + window.scrollY, + duration: 500 + }) + .on('tick', v => window.scrollTo(0, v)) + .on('done', () => { enableScrollEvent = true; scroller = null; }) + .begin() +} function highlight () { + if (!enableScrollEvent) return const sidebar = dom.getNode('.sidebar') const anchors = dom.findAll('.anchor') const wrap = dom.find(sidebar, '.sidebar-nav') let active = dom.find(sidebar, 'li.active') - const top = dom.body.scrollTop + let doc = document.documentElement + const top = doc && doc.scrollTop || document.body.scrollTop let last for (let i = 0, len = anchors.length; i < len; i += 1) { @@ -79,7 +97,13 @@ export function scrollActiveSidebar (router) { export function scrollIntoView (id) { const section = dom.find('#' + id) - section && section.scrollIntoView() + section && scrollTo(section) + + let li = nav[id] + const sidebar = dom.getNode('.sidebar') + let active = dom.find(sidebar, 'li.active') + active && active.classList.remove('active') + li && li.classList.add('active') } const scrollEl = dom.$.scrollingElement || dom.$.documentElement