You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在jQuery Steps中正确显示FullCalendar?

解决jQuery Steps中FullCalendar切换步骤后无法渲染的问题

问题:在jQuery Steps组件中集成FullCalendar时,日历在第一步可正常加载,但切换至第二步及后续步骤后,再返回第一步时日历无法渲染。

问题根源

你当前的实现把FullCalendar初始化逻辑放在了onStepChanging事件中,且仅当currentIndex == 0时执行。但jQuery Steps切换步骤时会隐藏非当前步骤的DOM元素:

  1. 从第一步切换到其他步骤时,日历容器被隐藏,FullCalendar的DOM状态被破坏
  2. 从其他步骤返回第一步时,currentIndex是大于0的(比如从第二步返回第一步,currentIndex=1、newIndex=0),不会触发原有的初始化逻辑
  3. 即使之前初始化过实例,容器从隐藏变为显示时,FullCalendar无法自动感知尺寸变化,导致渲染失败

解决方案

1. 全局初始化FullCalendar实例(仅执行一次)

把日历初始化代码移到页面加载完成时执行,避免重复创建实例:

// 全局存储日历实例,避免重复创建
var calendar;

$(document).ready(function() {
    var today = new Date();
    document.getElementById("currentDate").innerHTML = moment(today).format('DD-MM-YYYY');
    
    var calendarEl = document.getElementById("calendar");
    calendar = new FullCalendar.Calendar(calendarEl, {
        headerToolbar: {
            left: "prev",
            center: "title",
            right: "today next",
        },
        selectable: true,
        validRange: function(nowDate){
            return {start: nowDate} // 禁止选择过去的日期
        },
        showNonCurrentDates: false,
        dateClick: function(info) {
            var chosenDate = info.dateStr;
            document.getElementById("currentDate").innerHTML = moment(chosenDate).format('DD-MM-YYYY');
            finalDate = chosenDate;
            if (document.getElementById("time-slots").style.display === "none") {
                $("#time-slots").fadeIn();
            }
        }
    });
});

2. 步骤切换时触发日历重新渲染

修改onStepChanging事件,当切换到第一步时,强制更新日历尺寸并重新渲染:

$(".steps-validation").steps({
    headerTag: "h6",
    bodyTag: "fieldset",
    transitionEffect: "fade",
    titleTemplate: '<span class="step">#index#</span> #title#',
    labels: {
        finish: 'Submit'
    },
    onStepChanging: function (event, currentIndex, newIndex)
    {
        // 切换到第一步时,重新渲染日历
        if (newIndex === 0 && calendar) {
            calendar.updateSize(); // 强制更新容器尺寸
            calendar.render();
        }

        // 原有逻辑保持不变
        if (currentIndex > newIndex) {
            return true;
        }
        
        if (currentIndex < newIndex) {
            form.find(".body:eq(" + newIndex + ") label.error").remove();
            form.find(".body:eq(" + newIndex + ") .error").removeClass("error");
        }
        form.validate().settings.ignore = ":disabled,:hidden";
        return form.valid();
    },
    onFinishing: function (event, currentIndex)
    {
        form.validate().settings.ignore = ":disabled";
        return form.valid();
    },
    onFinished: function (event, currentIndex)
    {
        alert("Submitted!");
    }
});

3. 可选优化:步骤切换完成后延迟更新

如果过渡动画导致渲染仍有问题,添加onStepChanged事件延迟处理:

$(".steps-validation").steps({
    // 其他配置不变
    onStepChanged: function(event, currentIndex, priorIndex) {
        if (currentIndex === 0 && calendar) {
            // 延迟100ms确保DOM完全显示
            setTimeout(function() {
                calendar.updateSize();
            }, 100);
        }
    }
});

核心注意事项

  • 禁止在步骤切换事件中重复创建FullCalendar实例,否则会导致内存泄漏和DOM冲突
  • updateSize()是解决隐藏容器渲染问题的关键,它会让FullCalendar重新计算容器尺寸
  • 延迟调用是为了等待jQuery Steps的过渡动画完成,确保DOM状态稳定

内容的提问来源于stack exchange,提问作者user19367608

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 10:35:17