cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-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.

            
              <div id="wrapper">
<header>
  <h1 id="month"></h1>
  <button id="go-prev">Prev</button>
  <button id="go-next">Next</button>
  
  
</header>
  <ul class="days">
    <li>Sun</li>
    <li>Mon</li>
    <li>Tue</li>
    <li>Wed</li>
    <li>Thu</li>
    <li>Fri</li>
    <li>Sat</li>
  </ul>
  <div id="cal-container">
  </div>
</div>
            
          
!
            
              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;
    }
}
            
          
!
            
              $(document).ready(function() {
  //Variables 変数
  let today = new Date();
  let currentYear = today.getFullYear();
  let currentMonth = today.getMonth();
  const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  //------------------------------------
  // DOM  
  //------------------------------------
  //Containers for each week  週を収めるコンテナ
  const 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>';
  //Set the containers to DOM コンテナをDOMにセット
  for (var i = 0; i < 3; i++) {
    $('#cal-container').append(calDom);
  }

  //Set the elements for displaying 表示する要素をDOMへ
  createCalendar(0, currentYear, currentMonth - 1);
  createCalendar(1, currentYear, currentMonth);
  createCalendar(2, currentYear, currentMonth + 1);
  
  //Set the month and the year 月と年
  $('#month').html(months[currentMonth] + '. ' + currentYear);

  //------------------------------------
  //Functions
  //------------------------------------
  
  //Return an object of monthly calendar data 一月のカレンダーデータをオブジェクトで返す
  function getCalObject(YYYY, m) {
    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;
  }
  
  

  function createCalendar(n, y, m, on) {
    var calObj = 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>');
      }
    }
  }

  //------------------------------------
  //Event Handlers
  //------------------------------------

  $('#go-prev').on('click', function() {
    //alert('t');
    //cal を手前に作る
    currentMonth--;
    if (currentMonth < 0) {
      currentMonth = 10 - currentMonth;
      currentYear--;
    }
    $('#cal-container').prepend(calDom);
    //calに値を挿入する
    createCalendar(0, currentYear, currentMonth - 1);
    $('.cal').last().remove();   
    $('#month').html(months[currentMonth] + '. ' + currentYear);
  });

  $('#go-next').on('click', function() {
    //alert('t');
    currentMonth++;
    if (currentMonth > 11) {
      currentMonth = currentMonth - 12;
      currentYear++;
    }
    //cal を最後に作る
    $('#cal-container').append(calDom);
    //calに値を挿入する
    createCalendar(3, currentYear, currentMonth + 1);
    //最初のcalを消す
        
    $('.cal').first().remove();
    $('#month').html(months[currentMonth] + '. ' + currentYear);
  });

});
            
          
!
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