vue怎么实现自定义日期组件功能-创新互联
这篇文章给大家分享的是有关vue怎么实现自定义日期组件功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

vue是什么
Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它大型框架的区别是,使用Vue可以自底向上逐层应用,其核心库只关注视图层,方便与第三方库和项目整合,且使用Vue可以采用单文件组件和Vue生态系统支持的库开发复杂的单页应用。
实现一个日期组件,如图:

components.js代码如下:
Vue.component('sc-calendar',{
template:'' +
'' +
' < ' +
'{{currentYear}}年' +
'{{currentMonth}}月' +
' > ' +
'' +
'' +
'' +
'- {{item}}
' +
'
' +
'' +
'- {{item.text}}
' +
'
' +
'' +
'',
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;iccal.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代码:
Title
感谢各位的阅读!关于“vue怎么实现自定义日期组件功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文名称:vue怎么实现自定义日期组件功能-创新互联
标题路径:http://www.jxjierui.cn/article/dochdj.html


咨询
建站咨询
