Updating Masonry Layout with Collapsible Feature using Bootstrap 4

Solution:

Resolved the issue by replacing Cycle2 with a responsive slideshow script called Slick.

Check out the code on jsfiddle.

     (function($) {

  var $carousel = $('.carousel'),
            $masonry    = $('#cards');

    $carousel.on('afterChange', function(slick){

        $masonry.masonry({
            itemSelector: '.card-item'
        }).on('shown.bs.collapse hidden.bs.collapse', function() {
            $masonry.masonry();
        });

    }).slick({
        arrows: false,
        speed: 0,
        infinite: true,
        autoplay: true,
        autoplaySpeed: 400
    });

})(jQuery);

Original Question:

Adapted the card-column "masonry" from bootstrap 4 to work with masonry js instead, but ran into an issue with the collapse link. The grid-item does not push its container down to reformat the masonry layout as intended.

Despite the collapse link showing and hiding properly, the layout remains unchanged, causing elements to overlap instead of maintaining the masonry layout.

Check out the jsfiddle here.

Attempted to incorporate the following code with no success:

$grid.on('shown.bs.collapse', function() {
  $grid.masonry('layout');
});

Struggled to merge bootstrap events with masonry layout to achieve desired effect.

$('.collapsing').on('show.bs.collapse', function () {
  // do something…
  $grid.masonry('layout');
});

Enhanced code snippet in the fiddle includes both of these attempts.

Looking for a solution that involves:

  1. Determine container size when closed
  2. Determine container size when opened
  3. Adjust layout to accommodate open size

Struggling to translate this concept into proper code implementation.

Answer â„–1

Give this a shot using the .on method

(function ($) {

    var $grid = $('#cards');
    $grid.masonry({
        itemSelector: '.card-item'
    }).on('shown.bs.collapse hidden.bs.collapse', function() {
        $grid.masonry();
    });

})(jQuery);

Check out the working fiddle https://jsfiddle.net/joshmoto/ax1d7ok6/

The downside here is that the masonry won't animate smoothly with the bootstrap collapse, it will only be triggered after the collapse is completed. Quite disappointing, I agree :-(

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The variable $ has not been defined in Jquery framework

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript" src="deployJava.js"></script> <script type="text/javascr ...

Tips on dividing a div into two separate pages when converting to PDF

I am working on an asp.net MVC project and I need to convert a HTML div into a PDF with two separate pages. Here is an example of my code: HTML Code <div class="row" id="mycanvas"> <p>This is the first part of my content.</p> <p ...

Can you help me modify the navbar color in Bootstrap 4 based on the screen width?

For example, if I use the navbar-dark and bg-dark classes in a nav tag, I want to switch them to navbar-light and bg-light when the screen width is changed to approximately 600px (using Bootstrap 4). ...

Ensuring that a minimum of one checkbox has been selected

When employees submit a work order, they fill out a form that includes a section titled "Departments Affected" with checkboxes for 5 different departments. The form is set up so that at least one department must be checked before it can be sent. I have imp ...

How to extract a value from the bootbox confirm callback function

When I use bootbox, console.log(r) returns nothing. How can I successfully pass the result to the r variable? function customConfirm(message){ bootbox.confirm({ message: message, buttons: { 'cancel': { ...

Strange behavior in Internet Explorer when using jQuery to simulate a click on an input=file element

My customized jQuery and HTML code achieves the following purpose: There is a placeholder image that can be clicked by the user, triggering a file selection dialog to open. Once a file is chosen, the corresponding multipart form is sent to the server. To ...

Using jQuery to deactivate buttons upon submission or clicking within forms and hyperlinks

Within my Rails application, there are buttons that send data to the server. Some of these buttons are part of a form while others are standalone. I am seeking a solution to implement my jQuery code, which disables the buttons upon click, for both scenario ...

Retrieve all data with the same attribute name in one statement from the ajax call

After making an Ajax call, I receive the following data, which includes two images but the number of images can vary: [{"BadgeImage":"http:\/\/localhost:8666\/web1\/profile\/images\/badge image 2\/1.png"}, {"BadgeImag ...

I'm having an issue where my footer is getting cut off when I resize the browser and start side scrolling. Can

I designed a webpage with a footer that contains another div holding centered content. The footer is supposed to span the entire width, while the inner content div is set to 960px with a margin auto to center it. <footer> <div class="footer-in ...

Display data on webpage by returning value from PHP through AJAX and passing it into specified ID

I have successfully implemented a post and comment system on my webpage. Each post on my index.php page allows for comments, which are sent along with the post ID to comment.php. The comment is then inserted into the corresponding record in MySql and retur ...

Guidelines for crafting an intricate selector by utilizing mergeStyleSets and referencing a specific class

I'm currently in the process of developing a web application using ReactJS. In my project, I have the following code: const MyComponent = (props: { array: Array<Data> }) => { const styles = mergeStyleSets({ container: { ...

struggling to perfect the CSS styling

I am in need of a design similar to the image linked below: https://i.sstatic.net/Er1wT.png I attempted to create this layout using a table, but I have not been successful in aligning the side images properly. testTable.html <style> .container{ ...

Interactive loading of datalist choices using AJAX in the Firefox browser

I have recently decided to replace the Jquery UI autocomplete function on my website with HTML5 datalists that load dynamic options. After researching this topic extensively, I came across various answers on Stack Overflow, such as How do you refresh an HT ...

Unable to populate Select2 v4.x using ajax calls

I am currently working with the latest version of select2 (version 4). I have successfully implemented AJAX requests and confirmed that the responses are coming through correctly. However, I am facing an issue where the select element is not populating i ...

Adjusting the image size to match the browser height and width in a

Here is a JavaScript script I am working with: window.onresize = function() { var height = $(window).height(); var width = $(window).width(); $(".item").height(height); } The relevant HTML code is as follows: <div class="carousel-inner"> < ...

Struggling with Incorporating Variables from Jquery?

I am facing an issue with my local website built using PHP. Within my project, I have a file named functions.php containing the following code: public function saveAll($idmt_salesarea, $thn_bln, $no_pesanan, $tgl_pesan, $idms_langganan, $idms_kodebarang, ...

Determine whether a div that has been generated automatically has been selected by the user

My JSON file contains a list of elements, each creating a container. See the code below: $.getJSON('/data/risks.json', function(data) { var html = ''; $.each(data.risk, function(key, value){ html += '<div class="elementlis ...

Struggling to locate div#ID from the loaded fixtures with the Jasmine JS Framework

I successfully loaded my fixtures by using the following code: var fixture = loadFixtures("path/fixture.html"); Now, I am attempting to identify a specific DIV element with the ID #ID using the script below: expect(fixture.find('DIV#ID')).toBe ...

Customize Bootstrap 4's .btn-link style to include a smooth transition for under

Recently, I've been working on customizing Bootstrap 4's .btn-link by adding a special hover effect - a line that smoothly transitions from left to right beneath the link when the mouse hovers over it. Although things are mostly going according ...

"Is there a way to reverse the action of $( "button" ).remove()

Currently, I have implemented JQuery's $( ".button" ).remove(); function to get rid of all the buttons on my webpage immediately after a user clicks the print PDF button that changes the HTML page into a PDF. Nevertheless, once this process is done ...