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代码:

ChatX翻译
ChatX翻译

最实用、可靠的社交类实时翻译工具。 支持全球主流的20+款社交软件的聊天应用,全球200+语言随意切换。 让您彻底告别复制粘贴的翻译模式,与世界各地高效连接!

下载

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

* {    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最简单的原生日历

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

65

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

43

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

35

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

41

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

204

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

9

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

8

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

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

精品课程

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

共58课时 | 3.2万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 1.9万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.7万人学习

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

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