2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-24 04:40:36 +03:00

docs(example): optimized selected el

This commit is contained in:
mengxiong10
2019-11-12 12:14:25 +08:00
parent 4626de2593
commit 9ea53a5923
3 changed files with 54 additions and 18 deletions
+17
View File
@@ -113,8 +113,24 @@ const titleAndDescMap = transformMd(en);
const App = { const App = {
name: 'App', name: 'App',
data() {
return {
currentId: this.getCurrentId(),
};
},
mounted() { mounted() {
hljs.initHighlighting(); hljs.initHighlighting();
window.onhashchange = () => {
this.currentId = this.getCurrentId();
};
if (this.currentId) {
document.getElementById(this.currentId).scrollIntoView();
}
},
methods: {
getCurrentId() {
return location.hash.slice(1);
},
}, },
render(h) { render(h) {
const menus = components.map(item => { const menus = components.map(item => {
@@ -130,6 +146,7 @@ const App = {
const props = { const props = {
id, id,
code, code,
active: id === this.currentId,
...titleAndDescMap[id], ...titleAndDescMap[id],
}; };
return <Card {...{ props }}>{h(component)}</Card>; return <Card {...{ props }}>{h(component)}</Card>;
+9 -3
View File
@@ -1,5 +1,5 @@
<template> <template>
<div class="card"> <div class="card" :class="{ active }">
<section :id="id" class="card-title" v-html="title"></section> <section :id="id" class="card-title" v-html="title"></section>
<section class="card-description markdown-body" v-html="description"></section> <section class="card-description markdown-body" v-html="description"></section>
<section class="card-demo markdown-body"> <section class="card-demo markdown-body">
@@ -27,6 +27,10 @@ export default {
title: String, title: String,
description: String, description: String,
code: String, code: String,
active: {
type: Boolean,
default: false,
},
}, },
data() { data() {
return { return {
@@ -52,8 +56,10 @@ $border-color: #ebedf0;
color: #314659; color: #314659;
border: 1px solid $border-color; border: 1px solid $border-color;
border-radius: 4px; border-radius: 4px;
& + & { margin-bottom: 60px;
margin-top: 60px; margin-top: 20px;
&.active {
border-color: #1284e7;
} }
} }
+28 -15
View File
@@ -18,32 +18,45 @@
</template> </template>
<script> <script>
function rafThrottle(fn) {
let isRunning = false;
return function fnBinfRaf(...args) {
if (isRunning) return;
isRunning = true;
requestAnimationFrame(() => {
isRunning = false;
fn.apply(this, args);
});
};
}
export default { export default {
name: 'Container',
props: { props: {
menus: Array, menus: Array,
}, },
data() { data() {
return { return {
offset: [],
activeIndex: 0, activeIndex: 0,
handleScroll: rafThrottle(this.scroll),
}; };
}, },
mounted() { computed: {
const scrollEl = this.$refs.main; menuIds() {
const els = document.querySelectorAll('.card-title'); return this.menus.map(v => v.id);
const { top } = scrollEl.getBoundingClientRect(); },
const offset = [];
for (let i = 0; i < els.length; i++) {
const el = els[i];
offset.push(el.getBoundingClientRect().top - top);
}
this.offset = offset;
}, },
methods: { methods: {
handleScroll(evt) { scroll() {
const value = evt.currentTarget.scrollTop - 10; for (let i = 0; i < this.menuIds.length; i++) {
const index = this.offset.findIndex(v => v > value); const id = this.menuIds[i];
this.activeIndex = index; const el = document.getElementById(id);
const { top } = el.getBoundingClientRect();
if (top >= 0) {
this.activeIndex = i;
break;
}
}
}, },
}, },
}; };