Javascript日历应用程序moment.js

Javascript日历应用程序moment.js,javascript,jquery,momentjs,Javascript,Jquery,Momentjs,我正在我的项目中使用此版本。。。当前,日历项是随机放置的。我想学习如何将日期跨度(范围[start,end])与每个项目相关联,并相应地放置它们。就像普通日历一样 var items = [ { eventName: 'Lunch Meeting w/ Mark', calendar: 'Work', color: 'orange' }, { eventName: 'Interview - Jr. Web Developer', calendar: 'Work',

我正在我的项目中使用此版本。。。当前,日历项是随机放置的。我想学习如何将日期跨度(范围[start,end])与每个项目相关联,并相应地放置它们。就像普通日历一样

    var items = [
      { eventName: 'Lunch Meeting w/ Mark', calendar: 'Work', color: 'orange' },
      { eventName: 'Interview - Jr. Web Developer', calendar: 'Work', color: 'orange' },
      { eventName: 'Demo New App to the Board', calendar: 'Work', color: 'orange' },
      { eventName: 'Dinner w/ Marketing', calendar: 'Work', color: 'orange' },

      { eventName: 'Game vs Portalnd', calendar: 'Sports', color: 'blue' },
      { eventName: 'Game vs Houston', calendar: 'Sports', color: 'blue' },
      { eventName: 'Game vs Denver', calendar: 'Sports', color: 'blue' },
      { eventName: 'Game vs San Degio', calendar: 'Sports', color: 'blue' },

      { eventName: 'School Play', calendar: 'Kids', color: 'yellow' },
      { eventName: 'Parent/Teacher Conference', calendar: 'Kids', color: 'yellow' },
      { eventName: 'Pick up from Soccer Practice', calendar: 'Kids', color: 'yellow' },
      { eventName: 'Ice Cream Night', calendar: 'Kids', color: 'yellow' },

      { eventName: 'Free Tamale Night', calendar: 'Other', color: 'green' },
      { eventName: 'Bowling Team', calendar: 'Other', color: 'green' },
      { eventName: 'Teach Kids to Code', calendar: 'Other', color: 'green' },
      { eventName: 'Startup Weekend', calendar: 'Other', color: 'green' }
    ];
这是为项目指定日期的地方

this.events.forEach(function(ev) {
   ev.date = self.current.clone().date(Math.random() * (29 - 1) + 1);
});

如何修改项目数组和脚本以将每个项目分配到前端日历上相应的位置

这就是您要搜索的内容吗

!函数(){
var today=时刻();
功能日历(选择器、事件){
this.el=document.querySelector(选择器);
这个事件=事件;
此.current=力矩().date(1);
this.events.forEach(函数(ev){
ev.date=时刻(ev.date);
});
这个.draw();
var current=document.querySelector('.today');
如果(当前){
var self=这个;
setTimeout(函数(){
开放日(当前);
}, 500);
}
}
Calendar.prototype.draw=函数(){
//创建标题
这个.drawHeader();
//取款月份
这个月();
这个.drawLegend();
}
Calendar.prototype.drawHeader=函数(){
var self=这个;
如果(!this.header){
//创建标题元素
this.header=createElement('div','header');
this.header.className='header';
this.title=createElement('h1');
var right=createElement('div','right');
right.addEventListener('click',function(){self.nextMonth();});
var left=createElement('div','left');
left.addEventListener('click',function(){self.prevMonth();});
//附加元素
this.header.appendChild(this.title);
this.header.appendChild(右);
this.header.appendChild(左);
this.el.appendChild(this.header);
}
this.title.innerHTML=this.current.format('MMMM-YYYY');
}
Calendar.prototype.drawMonth=函数(){
var self=这个;
如果(本月){
this.oldMonth=this.month;
this.oldMonth.className='month out'+(self.next?'next':'prev');
this.oldMonth.addEventListener('webkitAnimationEnd',function(){
self.oldMonth.parentNode.removeChild(self.oldMonth);
self.month=createElement('div','month');
self.backFill();
self.currentMonth();
self.fowardFill();
自身月数(self.month);
setTimeout(函数(){
self.month.className='month in'+(self.next?'next':'prev');
}, 16);
});
}否则{
this.month=createElement('div','month');
本月(本月);
这个。回填();
此.currentMonth();
这个。fowardFill();
this.month.className='month new';
}
}
Calendar.prototype.回填=函数(){
var clone=this.current.clone();
var dayOfWeek=clone.day();
如果(!dayOfWeek){return;}
克隆。减去('days',dayOfWeek+1);
对于(var i=dayOfWeek;i>0;i--){
this.drawDay(clone.add('days',1));
}
}
Calendar.prototype.fowardFill=函数(){
var clone=this.current.clone().add('months',1)。subtract('days',1);
var dayOfWeek=clone.day();
如果(dayOfWeek==6){return;}
对于(VarI=dayOfWeek;i<6;i++){
this.drawDay(clone.add('days',1));
}
}
Calendar.prototype.currentMonth=函数(){
var clone=this.current.clone();
而(clone.month()==此.current.month()){
这一天(克隆);
克隆。添加('天',1);
}
}
Calendar.prototype.getWeek=函数(天){
如果(!this.week | day.day()==0){
this.week=createElement('div','week');
本月(本周);
}
}
Calendar.prototype.drawDay=函数(天){
var self=这个;
本周(天);
//外天
var outer=createElement('div',this.getDayClass(day));
outer.addEventListener('click',function(){
self.openDay(本);
});
//日名
var name=createElement('div','day name',day.format('ddd');
//日数
var number=createElement('div','day number',day.format('DD');
//事件
var events=createElement('div','day events');
本节列出了事件(日期、事件);
外部。附加子对象(名称);
外部。追加子项(编号);
外部事件(事件);
本.周.儿童(外);
}
Calendar.prototype.DrainEvents=函数(日期,元素){
if(day.month()==此.current.month()){
var todaysEvents=this.events.reduce(函数(memo,ev){
如果(ev.date.ISAME(日期,'day')){
推送(ev);
}
返回备忘录;
}, []);
todaysEvents.forEach(功能(ev){
var evSpan=createElement('span',ev.color);
元素。追加子元素(evSpan);
});
}
}
Calendar.prototype.getDayClass=函数(天){
班级=['天'];
if(day.month()!==此.current.month()){
类。推送(“其他”);
}else if(today.isname(day,'day')){
课程。推送(“今天”);
}
返回类。加入(“”);
}
Calendar.prototype.openDay=函数(el){
变量详细信息,箭头;
var dayNumber=+el.querySelectorAll('.dayNumber')[0]。innerText | |+el.querySelectorAll('.dayNumber')[0]。textContent;
var day=this.current.clone().date(dayNumber);
var currentOpened=document.querySelector('.details');
//检查当前行上是否有打开的详细信息框
if(currentOpened&¤tOpened.parentNode==el.parentNode){
详细信息=当前已打开;
arrow=document.querySelector('.arrow');
}否则{
//关闭不同周行的开放事件
//currentOpened&¤tOpened.parentNode.removeChild(currentOpened);
如果(当前打开){
currentOpened.addEventListener('webkitAnimationEnd',function(){
currentOpened.parentNode.removeChild(currentOpened);
});
currentOpened.addEventListener('OAnimation