fullcalendar样式改造

本文介绍如何在Vue项目中使用FullCalendar并进行个性化样式调整,包括创建独特的日期展示风格,通过代码实现自定义布局和日期格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fullcalendar里默认是大表格,有日程事件显示在日期上,最近做了一个不一样的日期样式,项目中用了fullcalendar,所以就在此基础上做了一番改造,并把代码记录在这里,便于以后查询,效果如图所示:

日历

 

 代码在此(vue项目):

<template>
    <div class="schedule_box show_box">
        <div class="right_small right_calendar">
            <div id='calendar_index'></div>
        </div>
    </div>
</template>
<script>
export default {
    name:'Schedule',
    data(){
        return {
            
        }
    },
     mounted() {
        this.showData()
    },
    methods: {
        showData(){
            $("#calendar_index").fullCalendar({ 
                header: { left: "title", center:'', right: "prev,title,next,"},
                // titleFormat: 'YYYY-MM-DD',
                defaultView:'month',//默认视图
                navLinks: true, // can click day/week names to navigate views
                editable: false, 
                eventLimit: true, // allow "more" link when too many events 
                eventLimitText: "更多",
                aspectRatio:2,//单元格宽高的比例,宽是高的2倍
                selectable: true,//允许选择事件                         
            });
            var odiv=$("<div class='today_div'></div>")
            var p0=$("<p class='today_date'></p>").html(this.getTodaydate())
            var p1=$("<p class='today_all'></p>").html(this.formatDate(new Date))
            odiv.append(p0)
            odiv.append(p1)
            $(".right_calendar").append(odiv)

            // $("#calendar_index .fc-right h2").html(odiv)
        },
        // 日期格式处理 年-月-日 星期-
        formatDate(date){   
          var year = date.getFullYear();
		  var month = date.getMonth() + 1;
		  month = month < 10 ? ('0' + month) : month;
          var todaydate = date.getDate();
          var day = date.getDay();
          todaydate = todaydate < 10 ? ('0' + todaydate) : todaydate;
          //星期大写
          var dayCycleArray=["日","一","二","三","四","五","六"];
            for(var i=0;i<7;i++){
                if(day==i){
                    day=dayCycleArray[i];
                }
            }

          return year + '年' + month + '月' + todaydate + '日' + '  ' + '星期' + day;
        },
        //得到 今天 - 日 -
        getTodaydate(){
            var date = new Date();
            var hours = date.getDate();
            return hours
        },
    },
}
</script>
.right_small{ width:5.28rem; height:4.5rem; background: #ffffff; border-radius: 0.05rem; padding-left:0.28rem; padding-right:0.28rem; }
/* 日历日程 */
    /* 标题 */
#calendar_index{ width:100%; height:4.5rem; border-bottom:1px solid #f5f4f7; }
.right_calendar{ border-top:1px solid #e52355; position:relative;}
#calendar_index .fc-toolbar.fc-header-toolbar{ margin-top:0.15rem; height:0.6rem; }
#calendar_index .fc-left{ width:50%; border-right:1px solid #f5f4f7; }
#calendar_index .fc-right{ width:50%; line-height: 0.6rem; font-weight:normal; padding-left:0.3rem; }
  /* 标题左侧--今日 */
.today_div{ position:absolute; left:0; top:0; z-index: 999; opacity: 1; background: #ffffff; margin:0.15rem 0 0 0.28rem; text-align: center; }
.today_date{ color:#e52355; font-weight: lighter; font-size: 0.36rem; }
.today_all{ color:#adadad; font-weight: lighter; font-size: 0.14rem; }
  /* 标题右侧--左右键 */
#calendar_index .fc-right h2{ color:#333333; font-weight: lighter; font-size: 0.2rem; margin:0 0.1rem 0 0.1rem; }
#calendar_index .fc-right button{ border-radius: 50%; width:0.2rem; height:0.2rem; text-align: center; margin-top:0.2rem; background: #e8f0ff; }
#calendar_index .fc-right span{ margin-left:-0.09rem!important; color:#5d93ff; line-height: 0.1rem; }
#calendar_index .fc-right>div{ width:100%; display:flex; }
#calendar_index .fc-grid .fc-day-number { float: left;padding: 0 0.02rem;text-align: center;background: red !important; width:100%; height:0.55rem;line-height: 0.55rem; }
#calendar_index .fc-body{ height:2.88rem; }
#calendar_index .fc-day-grid-container{ overflow-x: hidden!important; height: 2.7rem!important; overflow-y: hidden!important; border:none!important; }
   /* table星期标题 */
#calendar_index .fc-widget-header { margin:0!important; text-align: center; height:0.38rem; line-height: 0.38rem; font-size: 0.18rem; font-weight: normal; color:#333333; }

#calendar_index .fc-basic-view .fc-body .fc-row{ height:0.42rem!important; }
#calendar_index .fc-day-number{ float:left!important; }
#calendar_index .fc .fc-row .fc-content-skeleton table, .fc .fc-row .fc-content-skeleton td, .fc .fc-row .fc-helper-skeleton td{ line-height: 0.4rem; border-color:#ffffff!important; }
#calendar_index .fc-today{ background: #ffffff!important; }
/*  */
#calendar_index .fc-past a,#calendar_index .fc-future a{ display:inline-block; background: #ffffff!important; border-radius: 50%; width:0.35rem; height:0.35rem; text-align: center; margin-left:0.1rem; }
#calendar_index .fc-today a{ display:inline-block; background: chartreuse!important; border-radius: 50%; width:0.35rem; height:0.35rem; text-align: center; margin-left:0.1rem;}
#calendar_index .fc-unthemed .fc-content, .fc-unthemed .fc-divider, .fc-unthemed .fc-list-heading td, .fc-unthemed .fc-list-view, .fc-unthemed .fc-popover, .fc-unthemed .fc-row, .fc-unthemed tbody, .fc-unthemed td, .fc-unthemed th, .fc-unthemed thead{ border:none!important; }
 

 

在使用 `FullCalendar` 时,如果需要自定义修改其样式,可以通过以下几种方式实现: ### 1. 使用 CSS 覆盖默认样式 `FullCalendar` 的默认样式是以 `.fc` 开头的类名进行控制。可以直接通过自定义 CSS 来覆盖这些样式。例如,可以修改日历标题栏、日期单元格或事件背景等样式。 ```css /* 修改日历标题栏背景颜色 */ .fc-toolbar { background-color: #f0f0f0; } /* 修改日期单元格边框和字体大小 */ .fc-daygrid-day { border: 1px solid #ddd; font-size: 14px; } /* 修改事件背景色 */ .fc-event { background-color: #42b983; } ``` 将上述代码添加到项目的 `<style>` 标签中,或者在单独的 CSS 文件中引入,即可生效[^2]。 --- ### 2. 使用 SCSS 变量(如果项目支持) 如果你的项目使用了 SCSS,并且安装了 `@fullcalendar/core` 的源文件,可以通过覆盖 SCSS 变量来自定义主题。例如: ```scss $fc-border-color: #ccc; $fc-today-bg-color: #f9f9f9; @import '~@fullcalendar/core/main.css'; @import '~@fullcalendar/daygrid/main.css'; ``` 这种方式更适合深度定制主题,但需要确保项目已配置好 SCSS 编译环境[^1]。 --- ### 3. 动态修改 DOM 元素 如果某些样式无法通过 CSS 直接覆盖,可以通过 JavaScript 在组件挂载后动态修改 DOM 元素的样式属性。例如,在 Vue 的 `mounted()` 生命周期钩子中操作 DOM: ```javascript mounted() { const calendarEl = this.$el.querySelector('#calendar'); const calendar = new Calendar(calendarEl, { plugins: [dayGridPlugin], events: [ { title: 'Event 1', start: '2023-09-01' }, { title: 'Event 2', start: '2023-09-05', end: '2023-09-07' } ] }); calendar.render(); // 动态修改某个元素的样式 const header = document.querySelector('.fc-toolbar'); if (header) { header.style.padding = '10px'; header.style.fontWeight = 'bold'; } } ``` 此方法适用于需要根据运行时状态调整样式的场景。 --- ### 4. 使用 `eventContent` 自定义事件渲染 如果希望对日历中的事件内容进行样式定制,可以使用 `eventContent` 回调函数来返回自定义 HTML 内容: ```javascript const calendar = new Calendar(calendarEl, { plugins: [dayGridPlugin], events: [ { title: 'Custom Event', start: '2023-09-10' } ], eventContent: function(arg) { return { html: `<div style="background-color:#ff6666; padding:4px;">${arg.event.title}</div>` }; } }); ``` 这允许完全控制事件的显示格式,包括内联样式或附加类名[^3]。 --- ### 5. 引入第三方主题 `FullCalendar` 支持多种主题,可以通过引入不同的 CSS 文件来切换主题风格。例如,引入 Bootstrap 主题: ```bash npm install @fullcalendar/bootstrap ``` 然后在代码中配置: ```javascript import bootstrapPlugin from '@fullcalendar/bootstrap'; const calendar = new Calendar(calendarEl, { plugins: [bootstrapPlugin, dayGridPlugin], themeSystem: 'bootstrap' }); ``` 确保引入对应的 CSS 文件以正确应用主题样式[^1]。 --- ### 总结 通过 CSS 覆盖、SCSS 变量、DOM 操作、`eventContent` 回调以及引入主题插件等多种方式,可以灵活地自定义 `FullCalendar` 的外观。选择合适的方法取决于具体需求及项目的技术栈。 ---
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值