You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by wa...@apache.org on 2022/12/12 07:17:10 UTC

[echarts-examples] 02/02: refactor: still use `vue-scrollactive` due to unexpected bug of `scroll-behavior: smooth`

This is an automated email from the ASF dual-hosted git repository.

wangzx pushed a commit to branch gh-pages
in repository https://gitbox.apache.org/repos/asf/echarts-examples.git

commit 30dc630ba0ed44a81a1b1794c3719f560b8d33b7
Author: plainheart <yh...@all-my-life.cn>
AuthorDate: Mon Dec 12 15:15:01 2022 +0800

    refactor: still use `vue-scrollactive` due to unexpected bug of `scroll-behavior: smooth`
---
 package-lock.json       |  29 ++++++++++-
 package.json            |   3 +-
 src/explore/Explore.vue | 135 +++++++++++-------------------------------------
 src/main.js             |   3 ++
 4 files changed, 62 insertions(+), 108 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 05477e71..fb93f338 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -19,7 +19,8 @@
         "semver": "^7.3.7",
         "sucrase": "^3.24.0",
         "vanilla-lazyload": "^17.8.3",
-        "vue-i18n": "^8.18.2"
+        "vue-i18n": "^8.18.2",
+        "vue-scrollactive": "^0.9.3"
       },
       "devDependencies": {
         "@babel/core": "^7.10.2",
@@ -2249,6 +2250,11 @@
       "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==",
       "dev": true
     },
+    "node_modules/bezier-easing": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/bezier-easing/-/bezier-easing-2.1.0.tgz",
+      "integrity": "sha512-gbIqZ/eslnUFC1tjEvtz0sgx+xTK20wDnYMIA27VA04R7w6xxXQPZDbibjA9DTWZRA2CXtwHykkVzlCaAJAZig=="
+    },
     "node_modules/big.js": {
       "version": "5.2.2",
       "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
@@ -10143,6 +10149,14 @@
         }
       }
     },
+    "node_modules/vue-scrollactive": {
+      "version": "0.9.3",
+      "resolved": "https://registry.npmjs.org/vue-scrollactive/-/vue-scrollactive-0.9.3.tgz",
+      "integrity": "sha512-W7Z2EQGDjnvgZ4h+/bpRVjiVU3bKWQDmT9f0bUL+zq3C/VSZaQiQ+qzWlMYDEpJbm13wJ6KIE6ry3HKLVyU4cQ==",
+      "dependencies": {
+        "bezier-easing": "^2.0.3"
+      }
+    },
     "node_modules/vue-style-loader": {
       "version": "4.1.2",
       "resolved": "https://registry.npmjs.org/vue-style-loader/-/vue-style-loader-4.1.2.tgz",
@@ -12603,6 +12617,11 @@
       "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==",
       "dev": true
     },
+    "bezier-easing": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmjs.org/bezier-easing/-/bezier-easing-2.1.0.tgz",
+      "integrity": "sha512-gbIqZ/eslnUFC1tjEvtz0sgx+xTK20wDnYMIA27VA04R7w6xxXQPZDbibjA9DTWZRA2CXtwHykkVzlCaAJAZig=="
+    },
     "big.js": {
       "version": "5.2.2",
       "resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
@@ -18812,6 +18831,14 @@
         "vue-style-loader": "^4.1.0"
       }
     },
+    "vue-scrollactive": {
+      "version": "0.9.3",
+      "resolved": "https://registry.npmjs.org/vue-scrollactive/-/vue-scrollactive-0.9.3.tgz",
+      "integrity": "sha512-W7Z2EQGDjnvgZ4h+/bpRVjiVU3bKWQDmT9f0bUL+zq3C/VSZaQiQ+qzWlMYDEpJbm13wJ6KIE6ry3HKLVyU4cQ==",
+      "requires": {
+        "bezier-easing": "^2.0.3"
+      }
+    },
     "vue-style-loader": {
       "version": "4.1.2",
       "resolved": "https://registry.npmjs.org/vue-style-loader/-/vue-style-loader-4.1.2.tgz",
diff --git a/package.json b/package.json
index 49566613..f956d32b 100644
--- a/package.json
+++ b/package.json
@@ -73,6 +73,7 @@
     "semver": "^7.3.7",
     "sucrase": "^3.24.0",
     "vanilla-lazyload": "^17.8.3",
-    "vue-i18n": "^8.18.2"
+    "vue-i18n": "^8.18.2",
+    "vue-scrollactive": "^0.9.3"
   }
 }
diff --git a/src/explore/Explore.vue b/src/explore/Explore.vue
index 670a4140..89098bf5 100644
--- a/src/explore/Explore.vue
+++ b/src/explore/Explore.vue
@@ -1,20 +1,20 @@
 <template>
-  <div
-    id="example-explore"
-    ref="scrollContainer"
-    @wheel.passive="onContentScroll"
-    @touchmove.passive="onContentScroll"
-    @scroll.passive="onScroll"
-    @pointerdown="onPointerUpDown"
-    @pointerup="onPointerUpDown"
-  >
+  <div id="example-explore">
     <div id="left-container" ref="leftContainer">
       <div id="left-chart-nav">
-        <nav ref="nav">
+        <scrollactive
+          ref="scrollactive"
+          active-class="active"
+          :offset="80"
+          :duration="500"
+          :scroll-container-selector="'#example-explore'"
+          bezier-easing-value=".5,0,.35,1"
+          @itemchanged="onActiveNavChanged"
+        >
           <ul>
             <li v-for="category in EXAMPLE_CATEGORIES" :key="category">
               <a
-                class="left-chart-nav-link"
+                class="left-chart-nav-link scrollactive-item"
                 :id="'left-chart-nav-' + category"
                 :href="'#chart-type-' + category"
               >
@@ -25,7 +25,7 @@
               </a>
             </li>
           </ul>
-        </nav>
+        </scrollactive>
       </div>
     </div>
     <div id="explore-container">
@@ -217,28 +217,7 @@ export default {
   },
 
   mounted() {
-    const { nav, scrollContainer } = this.$refs;
-    const navItems = [].slice.call(
-      nav.querySelectorAll('.left-chart-nav-link')
-    );
-    this._scrollactive = {
-      nav,
-      navItems,
-      scrollContainer,
-      scrollAnchors: navItems.map((item) =>
-        scrollContainer.querySelector(item.hash)
-      )
-    };
-    this.$nextTick(() => {
-      const hash = (location.hash = location.hash || navItems[0].hash);
-      this.onHashChange();
-      // const initialAnchor = hash && scrollContainer.querySelector(hash);
-      // initialAnchor && setTimeout(() => initialAnchor.scrollIntoView(), 0);
-      location.hash = '';
-      location.hash = hash;
-    });
     window.addEventListener('hashchange', this.onHashChange);
-    window.addEventListener('keydown', this.onKeyDown);
 
     this._lazyload = new LazyLoad({
       // Container should be the scroll viewport.
@@ -260,91 +239,39 @@ export default {
   },
 
   methods: {
-    onPointerUpDown(e) {
-      const scrollactive = this._scrollactive;
-      const scrollContainer = scrollactive.scrollContainer;
-      scrollactive.maybeDraggingScrollbar =
-        e.type === 'pointerdown'
-          ? e.button === 0 &&
-            e.clientX >=
-              scrollContainer.clientLeft + scrollContainer.clientWidth - 18
-          : false;
-    },
-    // manually scroll
-    onKeyDown(e) {
-      const keyCode = e.keyCode;
-      if (
-        // page up, page down, end, home
-        (keyCode > 32 && keyCode < 37) ||
-        // arrow up, arrow down
-        keyCode === 38 ||
-        keyCode === 40
-      ) {
-        this.onContentScroll();
-      }
-    },
-    // FIXME can't detect if it's scrolled by dragging scrollbar
-    onScroll() {
-      this._scrollactive.maybeDraggingScrollbar && this.onContentScroll();
-    },
-    // wheel, touchmove, keydown (see above)
-    onContentScroll() {
-      const { navItems, scrollAnchors, scrollContainer } = this._scrollactive;
-      const scrollTop = scrollContainer.scrollTop;
-      if (
-        scrollTop <= 1 ||
-        scrollTop >=
-          scrollContainer.scrollHeight - scrollContainer.offsetHeight - 1
-      ) {
-        return;
-      }
-      for (let i = 0, len = scrollAnchors.length; i < len; i++) {
-        const anchor = scrollAnchors[i];
-        const section = anchor.parentElement;
-        const targetOffsetTop = section.offsetTop - 80;
-        const hash = '#' + anchor.id;
-        if (
-          scrollTop >= targetOffsetTop &&
-          scrollTop < targetOffsetTop + section.offsetHeight &&
-          location.hash !== hash
-        ) {
-          // location.hash = hash;
-          history.replaceState(null, null, navItems[i].href);
-          this.onHashChange();
+    onHashChange() {
+      const hash = location.hash;
+      const items = this.$refs.scrollactive.items;
+      let activeItem;
+      for (let i = 0, len = items.length, item; i < len; i++) {
+        item = items[i];
+        if (item.hash === hash) {
+          activeItem = item;
           break;
         }
       }
+      (activeItem || items[0]).click();
     },
-    onHashChange(e) {
-      const { lastActiveNavItem, navItems } = this._scrollactive;
-      if (lastActiveNavItem) {
-        lastActiveNavItem.classList.remove('active');
-      }
-      const hash = location.hash;
-      const activeNavItem = (this._scrollactive.lastActiveNavItem =
-        navItems.find((item) => item.hash === hash));
-      if (!activeNavItem) {
+    onActiveNavChanged(event, currentItem, lastActiveItem) {
+      if (!event || !currentItem) {
         return;
       }
-      activeNavItem.classList.add('active');
+      // change url
+      history.replaceState(null, null, currentItem.href);
+
       // scroll nav
       const leftContainer = this.$refs.leftContainer;
       const containerOffsetHeight = leftContainer.offsetHeight;
-      const rect = activeNavItem.parentElement.getBoundingClientRect();
+      const rect = currentItem.parentElement.getBoundingClientRect();
       if (rect.top < 0 || rect.bottom > containerOffsetHeight) {
         const scrollTop =
-          activeNavItem.offsetTop -
+          currentItem.offsetTop -
           containerOffsetHeight +
-          activeNavItem.offsetHeight;
+          currentItem.offsetHeight;
         leftContainer.scrollTo
           ? leftContainer.scrollTo(0, scrollTop)
           : (leftContainer.scrollTop = scrollTop);
       }
-
-      if (e) {
-        const anchor = this._scrollactive.scrollContainer.querySelector(hash);
-        anchor && anchor.scrollIntoView();
-      }
     }
   }
 };
@@ -381,7 +308,6 @@ $pd-lg: 20px;
   bottom: 0;
   left: 0;
   overflow-y: auto;
-  scroll-behavior: smooth;
 
   ::-webkit-scrollbar {
     height: 4px;
@@ -426,8 +352,6 @@ $pd-lg: 20px;
     font-weight: normal;
     color: #464646;
     font-size: 20px;
-    // TODO use variable
-    scroll-margin-top: $nav-height - 20px;
   }
   .chart-type-head span {
     font-size: 16px;
@@ -446,7 +370,6 @@ $pd-lg: 20px;
   width: $chart-nav-width;
   box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
   overflow-y: auto;
-  scrollbar-width: thin;
   // scroll-behavior: smooth;
 }
 
diff --git a/src/main.js b/src/main.js
index 1bfc5d54..24ba141f 100644
--- a/src/main.js
+++ b/src/main.js
@@ -5,6 +5,9 @@ import EditorPage from './editor/Editor.vue';
 import ExplorePage from './explore/Explore.vue';
 import ViewPage from './editor/View.vue';
 import { store } from './common/store';
+import VueScrollactive from 'vue-scrollactive';
+
+Vue.use(VueScrollactive);
 
 /**
  *


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org