Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app">
  <div class="container">
    
    <h1>🗓 {{ showYear }} {{ monthZh[showMonth] }}月
      <span>{{ msg }}</span>
    </h1>
    <div class="calendar">
      <div class="week">
        <div class="wrap">
          <div class="col" v-for="week in weeks">
            <i>星期{{ week }}</i>
          </div>
        </div>
      </div>
      <div class="dates wrap" ref="calContent">
        <div class="col" v-for="date in datesList"
             :class="{thismonth: date.date === 1}"
             :data-month="date.month"
             :data-year="date.year">
          <div class="inner">
            <div class="num din"
                 :class="{cur: date.month === showMonth}">
                {{ date.date }}
            </div>
          </div>
        </div>
      </div>
    </div>
    
  </div>
</div>
              
            
!

CSS

              
                $primary: #c6c0ba;

body {
  background: $primary;
  padding: 0;
  margin: 0;
  font-weight: 400;
  font-family: "Noto Sans TC", sans-serif;
}
.container{
  padding: 0 2rem;
  position: relative;
}
h1 {
  font-size: 3rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  z-index: 2;
  position: absolute;
  top: 0;
  margin: 0;
  padding: 1rem 0 0;
  background: $primary;
  span{
    font-size: 1.6rem;
    font-weight: 400;
  }
}
.calendar{
  display: flex;
  flex-flow: column;
  height: 100vh;
  i{
    font-style: normal;
  }
  .wrap{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
  }
  .col{
    width: 14.285714%;
    padding: 10px 10px;
    box-sizing: border-box;
    border-left: 1px solid rgba(255,255,255,.2);
    &:nth-child(7n + 1),
    &:first-child{
      border-left: 0;
    }
    &:nth-child(7n - 1),
    &:nth-child(7n) {
      color: #fff;
    }
  }
}
.week{
  z-index: 1;
  div.col{
    font-size: 14px;
    padding: 7rem 10px 10px;
    background-color: $primary;
    border-bottom: 1px solid #fff;
  }
}
.dates{
  -ms-overflow-style: none;
  scrollbar-width: none; /* Firefox */
  &::-webkit-scrollbar{
      display: none;
  }
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  .col{
    min-height: 15rem;
    border-bottom: 1px solid rgba(255,255,255,.2);
  }
  .num{
    font-size: 1rem;
    font-weight: 900;
    transition: opacity .3s;
    opacity: 0.1
  }
  .cur{
    opacity: 1
  }
}

              
            
!

JS

              
                new Vue({
  el: "#app",
  
  data: {
    msg: '無限滾動日曆',
    currentDate: {
      year: 0, 
      month: 0, 
      date: 0
    },
    datesList: [],
    monthZh: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'],
    weeks: ['一', '二', '三', '四', '五', '六', '日'],
    showYear: 2022,
    showMonth: 1
  },
  created() {
    this.currentDate.year = new Date().getFullYear()
    this.currentDate.month = new Date().getMonth()
    this.currentDate.date = new Date().getDate()
    this.showMonth = this.currentDate.month
    this.getCoolDatesList(
        this.currentDate.year, this.currentDate.month, false
    )
  },    
  mounted() {
    this.$refs.calContent.addEventListener('scroll', this.coolScroll)
  },
  methods: {
    getCoolDatesList(year, month, next) {
      this.currentDate.year = year
      this.currentDate.month = month
      
      const monthFirstDate = new Date(year, month, 1)
      const monthLastDate = new Date(year, month + 1, 0)
      const currentMonthDates = new Date(year, month + 1, 0).getDate()
      
      // 第一週的日期格子
      const firstDateDay = monthFirstDate.getDay() === 0 ? 7 : monthFirstDate.getDay()
      let initFirstWeekDate = new Date(year, month, 2 - firstDateDay)
      // 最後一週的日期格子
      const lastWeekOverDates = 7 - monthLastDate.getDay() === 7 ? 0 : 7 - monthLastDate.getDay()
      
      // 加總該月份所有日期格子
      let totalDates
      if (next) { //下個月格數算法
        let newNextFirst // 新的第一天
        if (monthFirstDate.getDay() === 1) {
          newNextFirst = 0
        } else {
            newNextFirst = monthFirstDate.getDay() === 0 ? 7 : monthFirstDate.getDay()
            newNextFirst = 7 - newNextFirst + 1
            initFirstWeekDate = new Date(year, month, newNextFirst + 1)
        }
        totalDates = currentMonthDates - newNextFirst + lastWeekOverDates
        
      } else {  //初始化格數算法
        totalDates = firstDateDay - 1 + currentMonthDates + lastWeekOverDates
      }
      
      
      let dateObj
      for (let i = 0; i < totalDates; i++) {
        dateObj = new Date(
            initFirstWeekDate.getFullYear(),
            initFirstWeekDate.getMonth(),
            initFirstWeekDate.getDate() + i
        )
        this.datesList.push({
            year: dateObj.getFullYear(),
            month: dateObj.getMonth(),
            date: dateObj.getDate(),
            postLists: [],
            holiday: {},
            active: false
        })
      }
      
    },
    coolScroll(event){
      
      let height = event.target.clientHeight
      let totalHeight = event.target.scrollHeight
      let scrollValue = event.target.scrollTop
      console.log(scrollValue + height, height)
      
      if (scrollValue + height > height) {
        this.getCoolDatesList(
          this.currentDate.year, this.currentDate.month + 1, true
        )
      }
      
      // 該月進入畫面之中 更換月份 & 年份
      document.querySelectorAll('.thismonth').forEach((el) => {
          if (scrollValue > el.offsetTop - 100) {
              this.showMonth = parseInt(el.dataset.month)
              this.showYear = el.dataset.year
          }
      })
    }
  }
  
});

              
            
!
999px

Console