I use a tabpanel, on the first tab I have a lightbox gallery, on the first page load it works fine, but then there are problems
Sometimes when I switch to the profile tab, then back to the home tab, the styles in the gallery can fly off and it will lose all its set height and merge with the footer
I was given one solution
var overviewEl = document.getElementById('overview-tab');
overviewEl.addEventListener('shown.bs.tab', function (event) {
let masonryGrid = $('.masonry-grid').masonry({
itemSelector: '.grid-item',
columnWidth: '.grid-sizer',
percentPosition: true
});
setTimeout(function() {
masonryGrid.masonry('layout')
}, 200);
setTimeout(function() {
masonryGrid.masonry('layout')
}, 1000);
setTimeout(function() {
masonryGrid.masonry('layout')
}, 3000);
});
But this only works when I switch to this tab, and when I reload the page, the gallery itself will not work at all
How can I modify this code so that the gallery is loaded when opening a page with an active tab and then also when switching to this tab?
html, body {
margin: 0;
padding: 0;
font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
font-size: 14px;
}
.tab-content {
padding: 100px;
}
.masonry-grid {
margin: 30px -10px 20px;
}
.grid-item, .grid-sizer {
float: left;
width: 35%;
padding: 10px;
}
.grid-item div {
background: FFFFFF;
border-radius: 8px;
box-shadow: 0 2px 8px 0 rgba(37, 33, 97, 0.1);
}
.grid-item img {
width: 100%;
border-radius: 5px;
}
.footer {
padding: 40px 0;
position: relative;
width: 100%;
bottom: 0;
}
.line {
position: absolute;
width: 100%;
height: 1px;
top: 0;
background: red;
}
.copy {
text-align: center;
font-size: 20px;
}
<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" />
<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>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.4/css/lightbox.css" integrity="sha512-Woz+DqWYJ51bpVk5Fv0yES/edIMXjj3Ynda+KWTIkGoynAMHrqTcDUQltbipuiaD5ymEo9520lyoVOo9jCQOCA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<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>
</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 class="rows">
<div class="masonry-grid" data-ma-sonry="{ 'itemSelector': '.grid-item'}" style="position: relative; height:640px;">
<div class="grid-item" style="position: absolute;">
<div><a href="https://i.imgur.com/3M8MxvZ.jpg" data-lightbox="image-1" data-title="image 1"><img src="https://i.imgur.com/3M8MxvZ.jpg" alt=""></a></div>
</div>
<div class="grid-item" style="position: absolute;">
<div><a href="https://i.imgur.com/3yAfpYK.jpg" data-lightbox="image-1" data-title="image 2"><img src="https://i.imgur.com/3yAfpYK.jpg" alt=""></a></div>
</div>
<div class="grid-item" style="position: absolute;">
<div><a href="https://i.imgur.com/CEdPx8x.jpg" data-lightbox="image-1" data-title="image 3"><img src="https://i.imgur.com/CEdPx8x.jpg" alt=""></a></div>
</div>
<div class="grid-sizer"></div>
</div>
</div>
</div>
<div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">Profile</div>
</div>
<footer class="footer">
<div class="line"></div>
<div class="copy">FOOTER</div>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.0/masonry.pkgd.min.js" integrity="sha512-coKEwkkVJR6lO1aL/rJeVFojNKG1DRNbAfuxcvFC98W9TYBO9TQIVFXiHBBx6dRL9+effSNEYIr/5vLe5HENVg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.4/js/lightbox.js" integrity="sha512-MBa5biLyZuJEdQR7TkouL0i1HAqpq8lh8suPgA//wpxGx4fU1SGz1hGSlZhYmm+b7HkoncCWpfVKN3NDcowZgQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
let masonryGrid = $('.masonry-grid').masonry({
itemSelector: '.grid-item',
columnWidth: '.grid-sizer',
percentPosition: true
});
setTimeout(function() {
masonryGrid.masonry('layout')
}, 200);
setTimeout(function() {
masonryGrid.masonry('layout')
}, 1000);
setTimeout(function() {
masonryGrid.masonry('layout')
}, 3000);
</script>

