实现一个日期组件,如图:

components.js代码如下:


Vue.component('sc-calendar',{
template:'<div class="scCalendar">' +
'<div class="calendar_header">' +
'<div class="prev" @click="prevMonth"> < </div>' +
'<span class="text_year">{{currentYear}}年</span>' +
'<span class="text_month">{{currentMonth}}月</span>' +
'<div class="next" @click="nextMonth"> > </div>' +
'</div>' +
'<div class="calendar_content">' +
'<ul class="week">' +
'<li v-for="item in weeks">{{item}}</li>' +
'</ul>' +
'<ul class="day">' +
'<li v-for="item in dayList" :class="{marginRight0:item.marginRight0}">{{item.text}}</li>' +
'</ul>' +
'</div>' +
'</div>',
data:function(){
return {
weeks: ['日', '一', '二', '三', '四', '五', '六'],
dayList:[],
currentYear:'',
currentMonth: ''
}
},
created:function(){
var date=new Date;
this.currentYear = date.getFullYear();
this.currentMonth = date.getMonth()+1;
this.calDay(this.currentYear, this.currentMonth); },
methods:{
//计算指定月份的天数
calDay:function(year,month){
var oDate = new Date();
//setFullYear(year,month,day) 方法用于设置年份,返回调整过的日期的毫秒表示。
oDate.setFullYear(year, month-1,1);
oDate.setDate(1);//设置一个月中的第一天
var oNow = oDate.getDay();//当前月的第一天是星期几
var dayNum = 0; //指定月份的天数
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
dayNum = 31;
}else if(month==4 || month==6 || month==9 || month==11){
dayNum = 30;
}else if(month==2&&this.isLeaYear(year)){
dayNum = 29;
}else{
dayNum = 28;
}
var SumDayLiNum = 0;//总共的格子数
var lastNum = (dayNum-(7-oNow))%7; //最后剩余的数
lastNum = lastNum == 0?0:7;
SumDayLiNum = 7 + parseInt((dayNum-(7-oNow))/7)*7+lastNum;
this.showDayList(dayNum,SumDayLiNum,oNow);
},
//判断是否是闰年
isLeaYear:function(year){
if(year%4==0&&year%100!=0){
return true;
}else{
if(year%400==0){
return true;
}else{
return false;
}
}
},
//显示当前日历内容
showDayList:function(dayNum,SumDayLiNum,oNow){
this.dayList = [];
var rows = parseInt(SumDayLiNum/7);
var cols = 7;
for(var i=0;i<rows;i++){
for(var j=0;j<cols ;j++){
if(j == cols-1){
this.dayList.push({
text:'',
marginRight0:true
})
}else{
this.dayList.push({
text:'',
marginRight0:false
})
}
}
}
for(var z=1;z<=dayNum;z++){
this.dayList[oNow].text = z;
oNow++;
}
},
prevMonth:function(){
if( this.currentMonth == 1){
this.currentYear = this.currentYear - 1;
this.currentMonth = 12;
}else{
this.currentMonth = this.currentMonth - 1;
}
this.calDay(this.currentYear, this.currentMonth);
},
nextMonth:function(){
if( this.currentMonth == 12){
this.currentYear = this.currentYear + 1;
this.currentMonth = 1;
}else{
this.currentMonth = this.currentMonth + 1;
}
this.calDay(this.currentYear, this.currentMonth);
} }
});

ccal.css代码:

html {
font-family: '微软雅黑';
}
body,
div,
span,
img,
ul,
li,
p {
margin: 0;
padding: 0;
}
ul,
li {
list-style: none;
}
.commonprev {
width: 0.46666667rem;
height: 0.93333333rem;
color: #ffffff;
position: absolute;
display: inline-block;
}
.commonyear {
width: 5.46666667rem;
height: 1.6rem;
font-size: 1rem;
color: #ffffff;
position: absolute;
}
.scCalendar {
width: 25rem;
height: 21.66666667rem;
background: #005498;
background-size: 100%;
}
.scCalendar .calendar_header {
height: 2.93333333rem;
width: 100%;
position: relative;
line-height: 2.93333333rem;
}
.scCalendar .calendar_header .prev {
width: 0.46666667rem;
height: 0.93333333rem;
color: #ffffff;
position: absolute;
display: inline-block;
left: 2.76666667rem;
}
.scCalendar .calendar_header .next {
width: 0.46666667rem;
height: 0.93333333rem;
color: #ffffff;
position: absolute;
display: inline-block;
right: 2.76666667rem;
}
.scCalendar .calendar_header .text_year {
width: 5.46666667rem;
height: 1.6rem;
font-size: 1rem;
color: #ffffff;
position: absolute;
left: 9.76666667rem;
}
.scCalendar .calendar_header .text_month {
width: 5.46666667rem;
height: 1.6rem;
font-size: 1rem;
color: #ffffff;
position: absolute;
left: 13.1rem;
}
.scCalendar .calendar_content {
padding: 0 1rem;
}
.scCalendar .calendar_content li {
width: 2rem;
height: 2rem;
line-height: 2rem;
margin-right: 1.5rem;
text-align: center;
margin-bottom: 0.66666667rem;
float: left;
color: white;
font-size: 0.93333333rem;
}
.scCalendar .calendar_content .week li:nth-of-type(7) {
margin-right: 0;
} .scCalendar .calendar_content .day .marginRight0 {
margin-right: 0;
}

index.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./ccal.css">
<script>
var pixRatio = 1/window.devicePixelRatio; //像素比
var html = document.documentElement;
document.write('<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale='+ pixRatio +',minimum-scale='+ pixRatio +',maximum-scale='+ pixRatio +'">');
html.style.fontSize = html.clientWidth/25 + 'px';
</script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<script src="./components.js"></script>
</head>
<body>
<div id="app">
<div class="calendar">
<sc-calendar></sc-calendar>
</div>
</div>
</body>
<script>
var vm = new Vue({
el:'#app'
}) </script>
</html>

vue自定义日历组件的实现的更多相关文章

  1. Vue自定义日历组件

    今天给大家介绍Vue的日历组件,可自定义样式.日历类型及支持扩展,可自定义事件回调.Props数据传输. 线上demo效果 示例 Template: <Calendar :sundayStart ...

  2. vue 自定义日历组件

    <template> <div class=""> <div class="calendarTraffic" name=" ...

  3. 使用ant design vue的日历组件,实现一个简单交易日与非交易日的切换

    使用ant design vue的日历组件,实现一个简单交易日与非交易日的切换 需求: 日历区分交易日.非交易日 可以切换面板查看整年交易日信息 可以在手动调整交易日.非交易日 演示实例 序--使用软 ...

  4. vue自定义全局组件(自定义插件)

    有时候我们在做开发的时候,就想自己写一个插件然后就可以使用自己的插件,那种成就感很强.博主最近研究element-ui和axios的时候,发现他们是自定义组件,但是唯一有一点不同的是,在用elemen ...

  5. 一个vue的日历组件

    说明: 1.基于element-ui开发的vue日历组件. 地址 更新: 1.增加value-format指定返回值的格式2.增加头部插槽自定义头部 <ele-calendar > < ...

  6. vue初学实践之路——vue简单日历组件(1)

    ---恢复内容开始--- 最近做的项目有一个需求,需要有一个日历组件供预定功能使用,之前的代码过于繁琐复杂,所以我采用vue重写了这个组件. npm.vue等等安装. 只是一个简单的日历组件,所以并不 ...

  7. vue 自定义报警组件

    1.自定义报警组件 Alarm.vue <!-- 报警 组件 --> <template> <div class="alarm"> <!- ...

  8. vue自定义select组件

    1.目的 看了很多element-ui的源码,决定自己实现一个简单的select组件,遇到的几个难点,便记录下来. 2.难点一 element-ui中的select组件通过v-model可以绑定数据, ...

  9. vue 自定义分页组件

    vue2.5自定义分页组件,可设置每页显示条数,带跳转框直接跳转到相应页面 Pagination.vue 效果如下图: all: small(只显示数字和上一页和下一页): html <temp ...

随机推荐

  1. [bug] VUE 的 template 中使用 ES6 语法导致页面空白

    如果你在 template 中,使用了 es6 及以上的语法,那么,在部分ios.安卓.微信浏览器中,打开页面后显示一片空白内容.如下: <ul id="example-1" ...

  2. 解决 ArchLinux 下中文 Chinese 不能输入 couldnt input 的问题

    解决 ArchLinux 下中文 Chinese 不能输入 couldnt input 的问题 一.Question 一年多的 ArchLinux 用户再次回归.然鹅,见面礼就是终端不能输入中文. 在 ...

  3. 【10】JMicro微服务-API网关

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 往下看前,建议完成前面1到9小节 1. Api网关基本特性: Api网关作为对外网提供服务的基本入口,地位类似于NGINX, ...

  4. C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇)

    C语言-apache mod(模块开发)-采用VS2017开发实战(windows篇) 名词解释:apxs apxs is a tool for building and installing ext ...

  5. 死锁、Lock锁、等待唤醒机制、线程组、线程池、定时器、单例设计模式_DAY24

    1:线程(理解) (1)死锁 概念: 同步中,多个线程使用多把锁之间存在等待的现象. 原因分析: a.线程1将锁1锁住,线程2将锁2锁住,而线程1要继续执行锁2中的代码,线程2要继续执行锁1中的代码, ...

  6. [转]SQL的主键和外键约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  7. database lock

    USE masterEXEC sp_lock select * from sys.sysprocesses where blocked<>0 DBCC INPUTBUFFER(120) k ...

  8. android studio 3.1.4下载安装配置(附旧版本下载地址)

    windows下安装android studio.当前时间2018年9月. 最新版本的android studio3.2.0-release出来了,拥有许多新的特性 可能我是一个业余的android开 ...

  9. jQuery Event.which 属性

    which属性用于返回触发当前事件时按下的键盘按键或鼠标按钮. 对于键盘和鼠标事件,该属性用于确定你按下的是哪一个键盘按键或鼠标按钮. which属性对DOM原生的event.keyCode和even ...

  10. springboot-4-整合fastjson

    使用fastjson作为springboot的默认json解析, 原来使用的是jackson 1, 引入依赖 <dependencies> <dependency> <g ...