当前位置:首页 > VUE

vue实现日历周滑动

2026-01-23 02:50:59VUE

Vue 实现日历周滑动功能

核心思路

日历周滑动功能通常需要结合日期计算、动态渲染和触摸事件处理。Vue 的响应式特性可以轻松实现数据与视图的同步更新。

日期计算逻辑

使用 Date 对象或第三方库(如 date-fns)处理日期增减。以下为计算当前周日期范围的示例代码:

function getWeekDates(baseDate) {
  const start = new Date(baseDate);
  start.setDate(start.getDate() - start.getDay()); // 调整到周日
  const dates = [];

  for (let i = 0; i < 7; i++) {
    const date = new Date(start);
    date.setDate(date.getDate() + i);
    dates.push(date);
  }
  return dates;
}

组件数据设计

在 Vue 组件中维护当前基准日期:

vue实现日历周滑动

data() {
  return {
    currentDate: new Date(), // 基准日期
    weekDates: [] // 存储当前周日期
  };
},
created() {
  this.updateWeekDates();
},
methods: {
  updateWeekDates() {
    this.weekDates = getWeekDates(this.currentDate);
  }
}

滑动事件处理

通过 @touchstart@touchend 实现滑动检测:

data() {
  return {
    touchStartX: 0
  };
},
methods: {
  handleTouchStart(e) {
    this.touchStartX = e.touches[0].clientX;
  },
  handleTouchEnd(e) {
    const diffX = e.changedTouches[0].clientX - this.touchStartX;
    if (Math.abs(diffX) > 50) { // 滑动阈值
      diffX > 0 ? this.prevWeek() : this.nextWeek();
    }
  },
  prevWeek() {
    const date = new Date(this.currentDate);
    date.setDate(date.getDate() - 7);
    this.currentDate = date;
    this.updateWeekDates();
  },
  nextWeek() {
    const date = new Date(this.currentDate);
    date.setDate(date.getDate() + 7);
    this.currentDate = date;
    this.updateWeekDates();
  }
}

模板渲染

在模板中绑定事件和渲染日期:

vue实现日历周滑动

<div 
  class="calendar-week"
  @touchstart="handleTouchStart"
  @touchend="handleTouchEnd"
>
  <div v-for="(date, index) in weekDates" :key="index" class="day">
    <div class="day-name">{{ formatDayName(date) }}</div>
    <div class="date">{{ date.getDate() }}</div>
  </div>
</div>

样式优化

添加 CSS 过渡效果提升用户体验:

.calendar-week {
  display: flex;
  overflow-x: auto;
  transition: transform 0.3s ease;
}

.day {
  flex: 1;
  text-align: center;
  padding: 10px;
}

完整组件示例

整合以上代码的完整组件实现:

<template>
  <div 
    class="calendar-container"
    @touchstart="handleTouchStart"
    @touchend="handleTouchEnd"
  >
    <div class="header">
      <button @click="prevWeek">←</button>
      <span>{{ monthYear }}</span>
      <button @click="nextWeek">→</button>
    </div>
    <div class="calendar-week">
      <div 
        v-for="(date, index) in weekDates" 
        :key="index" 
        class="day"
        :class="{ 'today': isToday(date) }"
      >
        <div class="day-name">{{ formatDayName(date) }}</div>
        <div class="date">{{ date.getDate() }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { format } from 'date-fns';

export default {
  data() {
    return {
      currentDate: new Date(),
      touchStartX: 0
    };
  },
  computed: {
    weekDates() {
      return this.getWeekDates(this.currentDate);
    },
    monthYear() {
      return format(this.currentDate, 'MMMM yyyy');
    }
  },
  methods: {
    getWeekDates(baseDate) {
      const start = new Date(baseDate);
      start.setDate(start.getDate() - start.getDay());
      const dates = [];

      for (let i = 0; i < 7; i++) {
        const date = new Date(start);
        date.setDate(date.getDate() + i);
        dates.push(date);
      }
      return dates;
    },
    isToday(date) {
      return format(date, 'yyyy-MM-dd') === format(new Date(), 'yyyy-MM-dd');
    },
    formatDayName(date) {
      return format(date, 'EEE');
    },
    handleTouchStart(e) {
      this.touchStartX = e.touches[0].clientX;
    },
    handleTouchEnd(e) {
      const diffX = e.changedTouches[0].clientX - this.touchStartX;
      if (Math.abs(diffX) > 50) {
        diffX > 0 ? this.prevWeek() : this.nextWeek();
      }
    },
    prevWeek() {
      const date = new Date(this.currentDate);
      date.setDate(date.getDate() - 7);
      this.currentDate = date;
    },
    nextWeek() {
      const date = new Date(this.currentDate);
      date.setDate(date.getDate() + 7);
      this.currentDate = date;
    }
  }
};
</script>

<style scoped>
.calendar-container {
  user-select: none;
}
.header {
  display: flex;
  justify-content: space-between;
  padding: 10px;
}
.calendar-week {
  display: flex;
}
.day {
  flex: 1;
  padding: 10px;
}
.today {
  background-color: #e3f2fd;
}
</style>

注意事项

  • 移动端适配需考虑 touch 事件,PC 端可添加鼠标事件模拟
  • 性能优化:对于复杂日历可考虑虚拟滚动
  • 国际化:使用 date-fns 的本地化功能支持多语言
  • 周起始日:根据地区需求调整(中国通常周一为第一天)

标签: 日历vue
分享给朋友:

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: np…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓、面…

vue前端分页怎么实现

vue前端分页怎么实现

前端分页实现方法 在Vue中实现前端分页通常涉及数据分割和页面控制逻辑。以下是常见的实现方式: 使用计算属性分页 通过计算属性对数据进行切片处理: computed: { paginatedD…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过…