手写日历组件

21 篇文章 0 订阅

现在的各种各样的ui框架里面都有 但是呢 有些不是我们想要的 此时就要自己手动去写一个了 这里是基于周来的  先看下原型效果 里面可以展示年月日或者月日 代码里面有

<template>
  <div class="weekList">
    <div v-for="item in arrWeek" :key="item.date" class="dateInfo decItem">
      <span class="week">{{ item.week }}</span>
      <span class="date">{{ item.date }}</span>
    </div>
  </div>
  <div>
    <el-button type="primary" @click='changeWeek(1)'>上一周</el-button>
    <el-button type="primary" @click="changeWeek(2)">下一周</el-button>
  </div>
</template>
<script>
import { reactive, toRefs,onMounted } from 'vue';
export default {
  setup () {
    const data = reactive({
      arrWeek: [],
      timestamp:'',
      yearData:[]
    })
    const getCurrent = () => {
      // data.timestamp = new Date().getTime()
      const week = new Date(data.timestamp).getDay() // 今天是星期几(0~6)
      const sunday = data.timestamp - 24 * 60 * 60 * 1000 * week // 第一天的时间戳
      for (let i = 0; i < 7; i++) {
        var day = sunday + 24 * 60 * 60 * 1000 * (i + 1)
        var dateStr = parseTime(day, '{m}-{d},{a}')
        data.arrWeek.push({ date: dateStr.split(',')[0], week: dateStr.split(',')[1] })
        var dateyear = getYearData(day, '{y}-{m}-{d},{a}')
        data.yearData.push(dateyear.split(',')[0])
      }
    //   handelYsList()
    }
    const parseTime = (time, cFormat) => {
      const format = cFormat || '{m}-{d}'
      let date = null
      if (typeof time === 'object') {
        date = time
      } else {
        if (typeof time === 'string') {
          if (/^[0-9]+$/.test(time)) {
            time = parseInt(time)
          } else {
            time = time.replace(new RegExp(/-/gm), '/')
          }
        }
        if (typeof time === 'number' && time.toString().length === 10) {
          time = time * 1000
        }
        date = new Date(time)
      }
      const formatObj = {
        m: date.getMonth() + 1,
        d: date.getDate(),
        a: date.getDay(),
        b: date.getHours() < 12 ? 1 : 2
      }
      const timeStr = format.replace(/{([ymdhisab])+}/g, (result, key) => {
        const value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') {
          return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][value]
        }
        if (key === 'b') {
          return value.toString()
        }
        return value.toString().padStart(2, '0')
      })
      return timeStr
    }
    //获取当前时间
    const getYearData = (time, cFormat) => {
      const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
      let date = null
      if (typeof time === 'object') {
        date = time
      } else {
        if (typeof time === 'string') {
          if (/^[0-9]+$/.test(time)) {
            time = parseInt(time)
          } else {
            time = time.replace(new RegExp(/-/gm), '/')
          }
        }
        if (typeof time === 'number' && time.toString().length === 10) {
          time = time * 1000
        }
        date = new Date(time)
      }
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay(),
        b: date.getHours() < 12 ? 1 : 2
      }
      const timeStr = format.replace(/{([ymdhisab])+}/g, (result, key) => {
        const value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') {
          return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][value]
        }
        if (key === 'b') {
          return value.toString()
        }
        return value.toString().padStart(2, '0')
      })
      return timeStr
    }
    const changeWeek=(num)=>{
      data.arrWeek = []
      data.yearData = []
      if (num === 1) {
        data.timestamp -= 24 * 60 * 60 * 1000 * 7
        getCurrent()
      } else {
        data.timestamp += 24 * 60 * 60 * 1000 * 7
        getCurrent()
      }
    }
    onMounted(()=>{
        data.timestamp = new Date().getTime()
        getCurrent()
    })
    return {
      ...toRefs(data),
      changeWeek
    }
  }
};
</script>
<style lang='scss' scoped>
.weekList{
    width:1200px;
    margin:0 auto;
    display: flex;
    justify-content: space-between;
    .decItem{
        width: 152px;
        height: 104px;
        background: rgba(243, 246, 250, 0.3);
        border-radius: 4px;
        border: 1px solid #D3D5E2;
        margin-bottom: 20px;
        font-size: 18px;
        font-family: SourceHanSansCN-Medium, SourceHanSansCN;
        font-weight: 500;
        color: #222B31;
        margin-right:17px;
        position: relative;
        .week{
            position: absolute;
            right:0;
            top:0;
            background: rgba(20, 133, 255, 0.1);
            border-radius: 0px 0px 0px 8px;
            padding:5px 10px;
            font-size: 12px;
            color:#1485FF;
        }
        .date{
            line-height:106px;
            color:#222B31;
            font-size: 20px;
        }
    }
}
</style>

代码是基于 vue3.0写的 不懂得可以参考vue3.0官网看看 或者自己拆分出来2.0的写法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个简单的Uni-app手写日历组件的实现步骤: 1. 创建一个日历组件,可以使用uni-calendar或者自己手写一个日历组件。 2. 在data中定义一个calendarData对象,用于存储日历相关的数据,比如当前年月、当前月有多少天等。 ``` data() { return { calendarData: { year: 2021, month: 9, days: [] } } } ``` 3. 在created生命周期中,初始化日历数据。 ``` created() { this.initCalendarData(); }, methods: { initCalendarData() { const date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; const days = this.getDaysOfMonth(year, month); this.calendarData = { year, month, days }; }, getDaysOfMonth(year, month) { const days = []; const date = new Date(year, month - 1, 1); const weekDay = date.getDay(); const lastMonthDays = this.getDaysOfLastMonth(year, month); const thisMonthDays = this.getDaysOfMonth(year, month); const nextMonthDays = 42 - thisMonthDays - weekDay; for (let i = weekDay - 1; i >= 0; i--) { days.push({ day: lastMonthDays - i, type: 'lastMonth' }); } for (let i = 1; i <= thisMonthDays; i++) { days.push({ day: i, type: 'thisMonth' }); } for (let i = 1; i <= nextMonthDays; i++) { days.push({ day: i, type: 'nextMonth' }); } return days; }, getDaysOfMonth(year, month) { return new Date(year, month, 0).getDate(); }, getDaysOfLastMonth(year, month) { if (month === 1) { year--; month = 12; } else { month--; } return this.getDaysOfMonth(year, month); } } ``` 4. 在日历组件中,使用v-for循环渲染日历。 ``` <template> <view class="calendar"> <view class="calendar-header">{{ calendarData.year }}年{{ calendarData.month }}月</view> <view class="calendar-body"> <view class="calendar-week"> <view class="calendar-day">日</view> <view class="calendar-day">一</view> <view class="calendar-day">二</view> <view class="calendar-day">三</view> <view class="calendar-day">四</view> <view class="calendar-day">五</view> <view class="calendar-day">六</view> </view> <view class="calendar-days"> <view class="calendar-day" v-for="(day, index) in calendarData.days" :key="index"> <view class="day-text" :class="day.type">{{ day.day }}</view> </view> </view> </view> </view> </template> ``` 5. 根据需要,可以为不同日期设置不同的样式。 ``` .day-text { width: 50px; height: 50px; line-height: 50px; text-align: center; } .lastMonth { color: #999; } .thisMonth { color: #333; } .nextMonth { color: #999; } ``` 6. 最后,可以为日历组件添加一些交互事件,比如点击某个日期时,弹出该日期对应的课程安排等。 以上就是一个简单的Uni-app手写日历组件的实现步骤,具体实现可以根据需求进行优化和改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沫熙瑾年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值