diff --git a/src/calendar/index.js b/src/calendar/index.js index 7838a0581..4015f186d 100644 --- a/src/calendar/index.js +++ b/src/calendar/index.js @@ -1,4 +1,5 @@ import { isDate } from '../utils/validate/date'; +import { getScrollTop } from '../utils/dom/scroll'; import { createComponent, bem, compareMonth, formatMonthTitle } from './utils'; import Header from './Header'; @@ -42,6 +43,8 @@ export default createComponent({ }, data() { + this.monthsHeight = []; + return { currentMonth: this.minDate }; @@ -69,12 +72,22 @@ export default createComponent({ } }, + mounted() { + this.getMonthsHeight(); + }, + methods: { + getMonthsHeight() { + this.$refs.month.forEach((month, index) => { + this.monthsHeight[index] = month.getBoundingClientRect().height; + }); + }, + genMonth(month, index) { const showTitle = index !== 0; return ( -
+
{showTitle &&
{month.title}
}
{month.days.map(item => ( @@ -83,6 +96,20 @@ export default createComponent({
); + }, + + onScroll() { + const scrollTop = getScrollTop(this.$refs.body); + let height = 0; + + for (let i = 0; i < this.months.length; i++) { + if (scrollTop < height) { + this.currentMonth = this.months[i - 1].date; + return; + } + + height += this.monthsHeight[i]; + } } }, @@ -90,7 +117,9 @@ export default createComponent({ return (
-
{this.months.map(this.genMonth)}
+
+ {this.months.map(this.genMonth)} +
); }