0

0

vue.js的原生日历实现方法

小云云

小云云

发布时间:2018-03-14 15:54:47

|

2444人浏览过

|

来源于php中文网

原创

之前的CSDN编译器有问题,所以现在重新整理出来给大家,本文主要和大家分享vue.js的原生日历实现方法,希望能帮助到大家。

先po上效果图:

这里写图片描述

html代码:

  
    

js代码:

  Vue.component("calendar", {
            template: "#calendar",
            data: function () {
                return {
                    currentDay: 1,
                    currentMonth: 1,
                    currentYear: 1970,
                    currentWeek: 1,
                    days: [],
                }
            },
            created() {                let that = this;
                that.initData(null);
            },
            methods: {
                initData: function (cur) {
                    let that = this;                    let leftcount = 0; //存放剩余数量
                    let date;                    if (cur) {
                        date = new Date(cur);
                    } else {                        let now = new Date();                        let d = new Date(that.formatDate(now.getFullYear(), now.getMonth(), 1));
                        d.setDate(35);
                        date = new Date(that.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
                    }
                    that.currentDay = date.getDate();
                    that.currentYear = date.getFullYear();
                    that.currentMonth = date.getMonth() + 1;
                    that.currentWeek = date.getDay(); // 1...6,0
                    if (that.currentWeek == 0) {
                        that.currentWeek = 7;
                    }                    let str = that.formatDate(that.currentYear, that.currentMonth, that.currentDay);
                    that.days.length = 0;                    // 今天是周日,放在第一行第7个位置,前面6个
                    //初始化本周
                    for (let i = that.currentWeek - 1; i >= 0; i--) {                        let d = new Date(str);
                        d.setDate(d.getDate() - i);                        let dayobject = {}; //用一个对象包装Date对象  以便为以后预定功能添加属性
                        dayobject.day = d;
                        that.days.push(dayobject); //将日期放入data 中的days数组 供页面渲染使用
                    }                    //其他周
                    for (let i = 1; i <= 35 - that.currentWeek; i++) {                        let d = new Date(str);
                        d.setDate(d.getDate() + i);                        let dayobject = {};
                        dayobject.day = d;
                        that.days.push(dayobject);
                    }

                },
                pickPre: function (year, month) {
                    let that = this;                    // setDate(0); 上月最后一天
                    // setDate(-1); 上月倒数第二天
                    // setDate(dx) 参数dx为 上月最后一天的前后dx天
                    let d = new Date(that.formatDate(year, month, 1));
                    d.setDate(0);
                    that.initData(that.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
                },
                pickNext: function (year, month) {
                    let that = this;                    let d = new Date(that.formatDate(year, month, 1));
                    d.setDate(35);
                    that.initData(that.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
                },
                pickYear: function (year, month) {
                    alert(year + "," + month);
                },                // 返回 类似 2016-01-02 格式的字符串
                formatDate: function (year, month, day) {
                    let y = year;                    let m = month;                    if (m < 10) m = "0" + m;                    let d = day;                    if (d < 10) d = "0" + d;                    return y + "-" + m + "-" + d
                },
            }
        })        let vm = new Vue({
            el: '#app',
        })

css代码:

Akkio
Akkio

Akkio 是一个无代码 AI 的全包平台,任何人都可以在几分钟内构建和部署AI

下载

立即学习前端免费学习笔记(深入)”;

* {    margin: 0;    padding: 0;}/*日历*/#calendar 
{    width: 98%;    
border: 2px solid #A4A7B0;    
height: 335px;   
margin-left: 0.5%;}.month 
{    width: 92%;    height: 48px;    
border: 2px solid #FFFFFF;    
margin-left: 3%;    margin-top: 20px;}.month ul 
{    margin: 0;    padding: 0;    
display: flex;    
margin-top: 11px;    
justify-content: space-between;}.year-month 
{    flex-direction: column;    
align-items: center;    
justify-content: space-around;}.choose-year 
{    padding: 0 20px;   
font-size: 16px;    
font-weight: 200;}.choose-month 
{    text-align: center;    
font-size: 16px;    
font-weight: 200;}.arrow 
{    width: 3%;   
 height: 25px;}.arrow1 
 {    background: url(left.png) no-repeat 0 0 /100% 100%;    
 margin-left: 44px;}.arrow2 
 {    background: url(right.png) no-repeat 0 0 /100% 100%;   
  margin-right: 44px;}.month ul li {    color: #999;    
  font-size: 20px;    
  text-transform: uppercase;    
  letter-spacing: 3px;    
  list-style: none;}.weekdays 
  {    margin: 0;    
  color: #FFFFFF;    
  background: #A4A7B0;    
  width: 96.6%;    
  margin-top: 26px;    
  height: 34px;    
  line-height: 34px;    
  margin-left: 2.2%;}.weekdays li 
  {    display: inline-block;    
  text-align: center;    
  color: #11616f;    
  font-size: 14px;    
  font-weight: 100;    
  width: 12.7%;}.days 
  {    padding: 0;    
  margin: 0;    
  display: flex;    
  flex-wrap: wrap;    
  justify-content: space-around;}.days 
  li {    list-style-type: none;    
  display: inline-block;    
  width: 14.2%;    
  text-align: center;    
  padding-bottom: 3px;    
  padding-top: 7px;    
  font-size: 12.78px;    
  color: rgb(14, 220, 235);    
  font-weight: 200;}.days li span span 
  {    height: 29.5px;    
  width: 27px;    
  line-height: 29.5px;    
  display: inline-block;}.days li .class-30 
  {    background: url(bg_30.png) no-repeat 0 0 /100% 100%;}.days li .class-60 
  {    background: url(bg_60.png) no-repeat 0 0 /100% 100%;}.days li .class-3060 
  {    background: url(bg_3060.png) no-repeat 0 0 /100% 100%;}.days li .other-month 
  {    padding: 5px;    color: #84a8ae;}

相关推荐:

js最简单的原生日历

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

2

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

2

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

0

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

0

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

3

2026.01.29

clawdbot ai使用教程 保姆级clawdbot部署安装手册
clawdbot ai使用教程 保姆级clawdbot部署安装手册

Clawdbot是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

25

2026.01.29

clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址
clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址

clawdbot龙虾机器人官网入口:https://clawd.bot/,clawdbot ai是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

16

2026.01.29

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

8

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

622

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
React 教程
React 教程

共58课时 | 4.3万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 2.5万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号