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 Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console