• js实现日历 完整版


    <template>
      <div id="calendar">
        <!-- 年份 月份 -->
        <div class="title">
          <div class="label">活动日历</div>
          <div class="total">当前活动 {{ list.length }}</div>
        </div>
        <div class="content">
          <ul class="month">
            <!--点击会触发pickpre函数,重新刷新当前日期 @click(vue v-on:click缩写) -->
            <li class="arrow">
              <span
                @click="pickPre(currentYear, currentMonth, 'year')"
                style="margin-right: 20px"
                >❮❮</span
              >
              <span @click="pickPre(currentYear, currentMonth, 'month')"></span>
            </li>
            <li class="year-month">
              <span class="choose-year">{{ currentYear }}</span>
              <span class="choose-month">{{ currentMonth }}</span>
            </li>
            <li class="arrow">
              <span @click="pickNext(currentYear, currentMonth, 'month')"></span>
              <span
                @click="pickNext(currentYear, currentMonth, 'year')"
                style="margin-left: 20px"
                >❯❯</span
              >
            </li>
          </ul>
          <!-- 星期 -->
          <ul class="weekdays">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
          </ul>
          <!-- 日期 -->
          <ul class="days">
            <!-- 核心 v-for循环 每一次循环用<li>标签创建一天 -->
            <li v-for="(item, index) in days" :key="index">
              <!--本月-->
              <!--如果不是本月  改变类名加灰色-->
    
              <span
                v-if="item.month != currentMonth"
                class="other-month"
                @click="clickOtherItem(item)"
                >{{ +item.date }}</span
              >
    
              <!--如果是本月  还需要判断是不是这一天-->
              <span v-else>
                <!--今天  同年同月同日-->
                <span
                  class="current-month"
                  @click="clickItem(item)"
                  :class="{
                    'current-day':
                      item.day.getFullYear() == new Date().getFullYear() &&
                      item.day.getMonth() == new Date().getMonth() &&
                      item.day.getDate() == new Date().getDate(),
                    active: item.value === activeTime,
                    'not-allow': !item.hasActivity,
                    'has-activity': item.hasActivity,
                  }"
                  >{{ +item.date }}</span
                >
              </span>
            </li>
          </ul>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      name: "",
      components: {},
      mixins: [],
      props: {},
      computed: {},
      watch: {},
      data() {
        return {
          currentDay: 1,
          currentMonth: 1,
          currentYear: 1970,
          currentWeek: 1,
          days: [],
          activeTime: "2023-09-21",
          list: ["2023-09-20", "2023-09-21", "2023-09-11"],
        };
      },
      created() {
        this.initData(null);
      },
      mounted() {},
      methods: {
        // 点击其他月份的 判断
        clickOtherItem(item) {
          if (item.month < this.currentMonth) {
            this.pickPre(this.currentYear, this.currentMonth, "month");
          } else {
            this.pickNext(this.currentYear, this.currentMonth, "month");
          }
        },
        clickItem(item) {
          if (!item.hasActivity) return;
          this.activeTime = item.value;
        },
        initData: function (cur) {
          var date;
          console.log(cur);
          if (cur) {
            date = new Date(cur);
          } else {
            var now = new Date();
            var d = new Date(this.formatDate(now.getFullYear(), now.getMonth(), 1));
            d.setDate(35);
            date = new Date(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
          }
          this.currentDay = date.getDate();
          this.currentYear = date.getFullYear();
          this.currentMonth = date.getMonth() + 1;
    
          this.currentWeek = date.getDay(); // 1...6,0
          if (this.currentWeek == 0) {
            this.currentWeek = 7;
          }
          var str = this.formatDate(
            this.currentYear,
            this.currentMonth,
            this.currentDay
          );
          this.days.length = 0;
          // 今天是周日,放在第一行第7个位置,前面6个
          //初始化本周
          console.log(this.currentWeek, "this.currentWeek");
          for (let i = this.currentWeek - 1; i >= 0; i--) {
            const d = new Date(str);
            d.setDate(d.getDate() - i);
            const day = d;
            const year = d.getFullYear();
            const month =
              d.getMonth() + 1 < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1;
            const date = d.getDate() < 10 ? `0${d.getDate()}` : d.getDate();
            const value = `${year}-${month}-${date}`;
            const dayobject = {
              year,
              month,
              date,
              value,
              day,
            };
            this.days.push(dayobject); // 将日期放入data 中的days数组 供页面渲染使用
          }
          //其他周
          for (var i = 1; i <= 35 - this.currentWeek; i++) {
            var d = new Date(str);
            d.setDate(d.getDate() + i);
            const day = d;
            const year = d.getFullYear();
            const month =
              d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
            const date = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
            const value = year + "-" + month + "-" + date;
            var dayobject = {
              year,
              month,
              date,
              value,
              day,
              hasActivity: false, // 是否有活动数据
            };
            this.days.push(dayobject);
          }
          // 拿到日历数组后 根据返回的有活动的列表进行过滤
          this.days = this.days.map((item) => {
            if (this.list.indexOf(item.value) !== -1) {
              this.$set(item, "hasActivity", true);
            }
            return item;
          });
        },
        pickPre: function (year, month, type) {
          if (type === "month") {
            const preMonth = month - 1 <= 0 ? 12 : month - 1;
            const preYear = month - 1 <= 0 ? year - 1 : year;
            this.initData(this.formatDate(preYear, preMonth, 1));
          } else {
            this.initData(this.formatDate(year - 1, month, 1));
          }
        },
        pickNext: function (year, month, type) {
          if (type === "month") {
            const nextMonth = month + 1 > 12 ? 1 : month + 1;
            const nextYear = month + 1 > 12 ? year + 1 : year;
            this.initData(this.formatDate(nextYear, nextMonth, 1));
          } else {
            this.initData(this.formatDate(year + 1, month, 1));
          }
        },
    
        // 返回 类似 2016-01-02 格式的字符串
        formatDate: function (year, month, day) {
          var y = year;
          var m = month;
          if (m < 10) m = "0" + m;
          var d = day;
          if (d < 10) d = "0" + d;
          return y + "-" + m + "-" + d;
        },
      },
    };
    </script>
    <style lang="less" scoped>
    * {
      box-sizing: border-box;
    }
    ul,
    li {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    body {
      font-family: Verdana, sans-serif;
      background: #e8f0f3;
    }
    #calendar {
      width: 440px;
      height: 400px;
      display: flex;
      flex-direction: column;
      padding: 20px;
      margin: 0 auto;
      box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.1),
        0 1px 5px 0 rgba(0, 0, 0, 0.12);
    }
    .title {
      height: 28px;
      margin-bottom: 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .label {
        color: #000;
        font-family: PingFang SC;
        font-size: 16px;
        font-style: normal;
        font-weight: 600;
      }
      .total {
        height: 28px;
        padding: 8px 15px;
        border-radius: 32px;
        background: rgba(0, 91, 255, 0.1);
        display: flex;
        align-items: center;
        color: #005bff;
        font-family: PingFang SC;
        font-size: 14px;
        font-style: normal;
        font-weight: 400;
      }
    }
    .content {
      display: flex;
      flex: 1;
      height: 0;
      flex-direction: column;
      .month {
        height: 40.25px;
        display: flex;
        justify-content: space-between;
        padding: 0 10px;
        align-items: center;
        .year-month {
          color: rgba(0, 0, 0, 0.85);
          text-align: center;
          font-family: PingFang SC;
          font-size: 16px;
          font-style: normal;
          font-weight: 600;
          .choose-year {
            margin-right: 12px;
          }
        }
        .arrow {
          span:hover {
            cursor: pointer;
            color: #005bff;
          }
        }
      }
      .weekdays {
        height: 40.25px;
        display: flex;
        li {
          cursor: default;
          display: flex;
          align-items: center;
          justify-content: center;
          width: calc(100% / 7);
          height: 40px;
          color: rgba(0, 0, 0, 0.85);
          text-align: center;
          font-family: PingFang SC;
          font-size: 14px;
          font-style: normal;
          font-weight: 400;
        }
      }
      .days {
        flex: 1;
        height: 0;
        display: flex;
        flex-wrap: wrap;
        li {
          display: flex;
          align-items: center;
          justify-content: center;
          width: calc(100% / 7);
          height: 40px;
          color: rgba(0, 0, 0, 0.85);
          text-align: center;
          font-family: PingFang SC;
          font-size: 14px;
          font-style: normal;
          font-weight: 400;
          span {
            width: 34px;
            height: 34px;
            line-height: 34px;
          }
          .has-activity {
            display: inline-block;
            width: 34px;
            height: 34px;
            position: relative;
          }
          .current-day {
            color: #005bff;
          }
          .active {
            color: #fff;
            display: inline-block;
            width: 34px;
            height: 34px;
            background: #005bff;
            border-radius: 50%;
            position: relative;
          }
          .other-month {
            color: rgba(0, 0, 0, 0.45);
            cursor: default;
          }
          .current-month:hover {
            width: 34px;
            height: 34px;
            display: inline-block;
            cursor: pointer;
            background: #f2f2f2;
            color: rgba(0, 0, 0, 0.85);
            border-radius: 50%;
          }
          .active:hover {
            cursor: pointer;
            color: #fff;
            display: inline-block;
            width: 34px;
            height: 34px;
            background: #005bff;
            border-radius: 50%;
          }
          .not-allow:hover {
            cursor: not-allowed;
            background-color: white;
          }
          .has-activity::after {
            content: "";
            position: absolute;
            width: 4px;
            height: 4px;
            border-radius: 50%;
            background-color: #005bff;
            left: 50%;
            bottom: 2px;
            margin-left: -2px;
          }
          .active::after {
            content: "";
            position: absolute;
            width: 4px;
            height: 4px;
            border-radius: 50%;
            background-color: white;
            left: 50%;
            bottom: 2px;
            margin-left: -2px;
          }
        }
      }
    }
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411

    在这里插入图片描述

  • 相关阅读:
    强推这款键盘利器(Keychron),这次我彻底入坑了
    Redis(6)五大数据类型——List(列表)
    Java多线程学习笔记
    黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译
    分布式调度xxl-job实战应用解析
    HTTPS 之fiddler抓包--jmeter请求
    互动设计:深入了解用户体验的关键
    MyCat的入门介绍
    【CT】LeetCode手撕—53. 最大子数组和
    Deepwalk,Node2vec算法原理生动理解(图文)
  • 原文地址:https://blog.csdn.net/bobringtheboys/article/details/133810930