欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue自定义日期组件的实现

程序员文章站 2023-11-03 22:57:46
实现一个日期组件,如图: components.js代码如下: ......

实现一个日期组件,如图:
vue自定义日期组件的实现

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>