I have a tablist panel and the last tab uses a fullcalendar
And there is such a problem that when switching to the tab, the css of the calendar is not loaded, BUT if I switch to another month in the calendar itself, then the css starts working
My version of the calendar is 5.7.2, for versions 3 I did not observe such a problem, what could be the problem?
var data = [];
$(document).ready(function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth',
headerToolbar: {
left: 'prev',
center: 'title',
right: 'next'
},
editable: false,
contentHeight: 705,
events: data
});
calendar.render();
});
html, body {
margin: 0;
padding: 0;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
font-size: 14px;
}
#calendar {
max-width: 900px;
margin: 20px auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/main.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js" integrity="sha512-TAVtO5mj3yhqvqWq539ELZe1kdI+ggl2XkVNJ7h33EeYK03qTyiKPbTu2DrrsuWlggnvCr3+A29goA7y0aZSFg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://unpkg.com/[email protected]/main.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<button class="nav-link active" id="nav-home-tab" data-bs-toggle="tab" data-bs-target="#nav-home" type="button" role="tab" aria-controls="nav-home" aria-selected="true">
Home
</button>
<button class="nav-link" id="nav-profile-tab" data-bs-toggle="tab" data-bs-target="#nav-profile" type="button" role="tab" aria-controls="nav-profile" aria-selected="false">
Profile
</button>
<button class="nav-link" id="nav-calendar-tab" data-bs-toggle="tab" data-bs-target="#nav-calendar" type="button" role="tab" aria-controls="nav-calendar" aria-selected="false">
Calendar
</button>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">Home</div>
<div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">Profile</div>
<div class="tab-pane fade" id="nav-calendar" role="tabpanel" aria-labelledby="nav-calendar-tab">
<div id="calendar"></div>
</div>
</div>
3
Answers
It looks like your calendar is loading during page load but the css is not applied as the calendar element is hidden that time. You can move your calendar load on tab click and give a small time interval like 500 ms. Try the below code
call your function on
'shown.bs.tab'
your code run after this tab showYou need to ensure the calendar is (re-)rendered when the tab containing the calendar is shown. This is because the calendar appears to decide on its rendered sized based on the size of its container. This makes sense most of the time, but the problem here is that when the tab is hidden, the container size is 0, which obviously then causes the calendar to compress in the way we can see in your question.
Note that you don’t need to re-run the entire calendar configuration code each time the tab is shown, you only need to call
render()
.Solution:
If the calendar tab needs to be the first active tab, the best workaround I can find is to set a different tab as active, and then use Javascript to "show" the calendar tab, which will then trigger the "shown" event and render the calendar.
(Just rendering the calendar immediately after declaring it didn’t work – I assume because this happened before bootstrap had rendered the tab pane).
Demo:
Relevant documentation:
https://getbootstrap.com/docs/5.0/components/navs-tabs/#events