cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              #wrapper
  header
    h1#month
    button#go-prev Prev
    button#go-next Next
  ul.days
    each val in ["Sun","Mon","Tue", "Wed","Thu","Fri","Sat"]
      li= val
  #cal-container
            
          
!
            
              html,body{
  height:100%;
  font-family: 'Play', sans-serif;
}
#wrapper{
  width:80%;
  height:100%;
  overflow:hidden;
  position:relative;
  margin:0 auto;
  box-sizing:border-box;
  padding-top:3rem;
}

header{
  height:48px;
  
  button{
    height:2rem;
    padding:0 1rem;
    margin:0.5rem;
    border-radius:4px;
    font-family: 'Play', sans-serif;
    background:#0196EF;
    border:none;
    color:#fff;
    font-size:1rem;
    cursor:pointer;
    
    &:hover{
      background:#0071B5;
    }
    &:active{
      background:#004D7B;
    }
  }
}

#go-prev{
  float:left;
  position:relative;
  z-index:2;
}
#go-next{
  float:right;
  position:relative;
  z-index:2;
}
h1{
  text-align:center;
  font-size:1.5rem;
  line-height:3rem;
  @media (min-width:350px){
    position:absolute;
    width:100%;
  }
}

.days li{
  background:#fff;
  font-size:11px;
  @media (min-width:350px){
    font-size:1rem;
  }
}

#cal-container{
  position:relative;
}

.cal{
  width:100%;
  position:absolute;
  top:48px;
  transition:left 0.5s;
}

.cal:nth-of-type(1){
  left:-100%;
}
.cal:nth-of-type(2){
  left:0;
}
.cal:nth-of-type(3){
  left:100%;
}
.cal:nth-of-type(4){
  left:200%;
}





ul{
  clear:left;
  border-top:1px solid #fff;
  
  li{
  float:left;
  width:calc(100% / 7);
  border-right:1px solid #fff;
  height:3rem;
  background:#F0F1F2;
  box-sizing:border-box;
  text-align:center;
  padding-top:1rem;
    color:#333;
    
    
    &:last-child{
      border-right:none;
    }
  }
  
}

.cal li{
  &:hover{
      background:#D1D7DB;
    }
}
            
          
!
            
              var BASICSAMPLE = BASICSAMPLE || {};

BASICSAMPLE.CALENDAR = {

  init: function() {
    this.setVariables();
    this.domReady();
    this.bindEvents();
  },

  setVariables: function() {
    this.today = new Date();
    this.currentYear = this.today.getFullYear();
    this.currentMonth = this.today.getMonth();
    this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    //Containers for each week  週を収めるコンテナ
    this.calDom = '<div class="cal">\
        <ul class="c-ul0"></ul>\
        <ul class="c-ul1"></ul>\
        <ul class="c-ul2"></ul>\
        <ul class="c-ul3"></ul>\
        <ul class="c-ul4"></ul>\
        <ul class="c-ul5"></ul>\
      </div>';
  },

  domReady: function() {
    for (var i = 0; i < 3; i++) {
      $('#cal-container').append(this.calDom);
    }
    this.createCalendar(0, this.currentYear, this.currentMonth - 1);
    this.createCalendar(1, this.currentYear, this.currentMonth);
    this.createCalendar(2, this.currentYear, this.currentMonth + 1);
    $('#month').html(this.months[this.currentMonth] + '. ' + this.currentYear);
  },
  bindEvents: function() {
    $('#go-prev').on('click', $.proxy(this.goToPrevious, this));
    $('#go-next').on('click', $.proxy(this.goToNext, this));
  },

  getCalObject: function(YYYY, m) {
    //Return an object of monthly calendar data 一月のカレンダーデータをオブジェクトで返す
    var cal_array = [];
    var targetYear = YYYY;
    var targetMonth = m;
    //Just in case if the month doesn't match with the year 
    //前年度や次年度に行ってしまった場合の処理
    if (targetMonth < 0) {
      targetMonth = 11 - targetMonth;
      targetYear--;
    } else if (targetMonth > 11) {
      targetMonth = targetMonth - 11;
      targetYear++;
    }

    //月の最終日と最初の日
    var lastDateOfCurrentMonth = new Date(targetYear, targetMonth + 1, 0).getDate();
    var firstDayOfCurrentMonth = new Date(targetYear, targetMonth, 1).getDay();

    var dateCounter = 1;
    for (var j = 0; j < 6; j++) { //各月に週をセット

      var eachWeek = [];
      for (var k = 0; k < 7; k++) { //毎週に日をセット

        if (dateCounter  <=  firstDayOfCurrentMonth) {
          eachWeek[k] = '&nbsp;'; //月初日までは空白
        } else if ((dateCounter - firstDayOfCurrentMonth - 1) >= lastDateOfCurrentMonth) {
          eachWeek[k] = '&nbsp;'; //月終わり後も空白
        } else {
          eachWeek[k] = dateCounter - firstDayOfCurrentMonth; //それ以外は日にちを表示
        }
        dateCounter++;
      }
      cal_array.push(eachWeek);
    }

    var calObject = {
      year: targetYear,
      month: targetMonth,
      cal: cal_array
    };
    return calObject;
  },
  createCalendar: function(n, y, m, on) {
    var calObj = this.getCalObject(y, m);
    for (var i = 0; i < 6; i++) {
      for (var j = 0; j < 7; j++) {
        $('.cal').eq(n).children('.c-ul' + i).append('<li>' + calObj.cal[i][j] + '</li>');
      }
    }
  },

  goToPrevious: function() {
    //cal を手前に作る
    this.currentMonth--;
    if (this.currentMonth < 0) {
      this.currentMonth = 10 - this.currentMonth;
      this.currentYear--;
    }
    $('#cal-container').prepend(this.calDom);
    //calに値を挿入する
    this.createCalendar(0, this.currentYear, this.currentMonth - 1);
    $('.cal').last().remove();   
    $('#month').html(this.months[this.currentMonth] + '. ' + this.currentYear);
  },
  goToNext: function() {
    this.currentMonth++;
    if (this.currentMonth > 11) {
      this.currentMonth = this.currentMonth - 12;
      this.currentYear++;
    }
    //cal を最後に作る
    $('#cal-container').append(this.calDom);
    //calに値を挿入する
    this.createCalendar(3, this.currentYear, this.currentMonth + 1);
    //最初のcalを消す
    $('.cal').first().remove();
    $('#month').html(this.months[this.currentMonth] + '. ' + this.currentYear);
  }
};

$(document).ready(function() {
  BASICSAMPLE.CALENDAR.init();
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console