CubePortfolio切换内容未向下推送后续元素问题求助
Hey there! Let's get that annoying layout issue sorted out. The problem here is that when you toggle content inside a cbp-item, CubePortfolio doesn't automatically detect the height change of that item—so it doesn't reflow the rest of the waterfall grid, causing your toggled content to overlap or sit behind other items instead of pushing them down.
Where to Add the Required Code
The jQuery('.cbp').cubeportfolio('layout'); line needs to run right after your toggle action finishes (whether it's a simple show/hide or an animated transition). This tells CubePortfolio to recalculate all item positions and refresh the layout.
Example Implementation Based on Common Toggle Code
Assuming your toggle logic in custom.js looks something like this (a basic click-to-show/hide setup):
// Your existing toggle code $('.your-toggle-button').on('click', function() { // Find the target content inside the same cbp-item const $toggleContent = $(this).closest('.cbp-item').find('.your-toggle-content'); $toggleContent.toggle(); // Show/hide the content });
Update it to trigger the layout refresh after the toggle:
$('.your-toggle-button').on('click', function() { const $toggleContent = $(this).closest('.cbp-item').find('.your-toggle-content'); $toggleContent.toggle(); // Add this line (wrap in setTimeout for mobile reliability) setTimeout(function() { jQuery('.cbp').cubeportfolio('layout'); }, 100); // 100ms gives mobile browsers time to update the DOM before reflowing });
If You're Using Animated Toggles (like slideToggle)
If your toggle uses a smooth animation (e.g., slideToggle), put the layout code inside the animation's complete callback instead—this ensures it runs only after the height change is fully finished:
$('.your-toggle-button').on('click', function() { const $toggleContent = $(this).closest('.cbp-item').find('.your-toggle-content'); // Run animation, then trigger layout refresh $toggleContent.slideToggle(300, function() { jQuery('.cbp').cubeportfolio('layout'); }); });
Why This Works
CubePortfolio's waterfall grid is calculated on initial load and when filters are triggered. When you modify an item's height dynamically (via toggle), the plugin has no way to know the layout needs updating unless you explicitly call the layout method. This forces it to re-measure all items and reposition them correctly, fixing the mobile overlap issue.
Just make sure this code runs after CubePortfolio is initialized (which it should, since it's in your custom.js that loads after the plugin scripts).
内容的提问来源于stack exchange,提问作者user7972648

