The art of masonry is not effective

I'm having trouble getting the Masonry cascading grid layout library to work in my code.

Stylesheet:

.post {
    background: #FFF;
    padding: 10px;
    border-bottom: 3px solid #e6e6e6;
    width: 30.7%;
    margin: 10px;
}

Source code:

<div id="content">
    <?php
    for($i = 1; $i <= 12; $i++) {
    ?>
    <div class="post fleft">
        <div class="title bbottom">
            <div class="fright">
                <div class="avatar fright" style="background-image: url(images/avatar.png)"></div>
                <span class="fright">سید امیرحسین رهنمافرد</span><br />
                <span class="fright">روشن</span>
            </div>
            <div class="fleft">
                <span class="fleft">14 خرداد 93 |  20:22</span><br />
                <span class="fleft">غزل در دیوان غزلیات</span>
            </div>
            <div class="clear"></div>
        </div>
   </div>
   <?php } ?>
    <script src="<?php echo ROOT ?>/scripts/jquery-1.11.0.min.js"></script>
    <script src="<?php echo ROOT ?>/scripts/masonry.pkgd.min.js"></script>
    <script src="<?php echo ROOT ?>/scripts/loggedIn.js"></script>

Script (loggedIn.js):

$(document).ready(function($){
    var $container = $('#content');
    $container.masonry({
      columnWidth: 200,
      itemSelector: '.post'
    });
});

I have verified that the addresses are correct, so why isn't it working?

EDIT:

Interestingly, adding an alert to the JS code seems to fix the issue!

$(document).ready(function($){
    var $container = $('#content');
    alert($container.html());
    // initialize
    $container.masonry({
      columnWidth: 100,
      itemSelector: '.post'
    });
});

Answer №1

Feel free to utilize the following snippet:

$(document).ready(function($){
    var $box = $('#container');
    setTimeout(function(){
        // begin
        $box.gridify({
          width: 120,
          item: '.item'
        });
    }, 300);
});

Please be aware that if you implement the $.getScript function, this issue may arise.

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

Why is it that the value of "json_encode( $custom_query['query_vars']" is null?

After attempting to pass variables from a PHP function using wp_localize_script into a jQuery script, I encountered an issue with a NULL variable that I can't seem to figure out. I'm hopeful that someone else might be able to identify the proble ...

Remove the </div> text and replace it with nothing

I'm attempting to substitute the end tag div with an empty string. Here is my code: $('#textDiv').html().replace(/'</div>'/g,'').replace(/<div>/g,'\n') This is the HTML: <div id='tex ...

No entries found in the Nuxt/content module's array

<template> <div> <input v-model="searchQuery" type="search" autocomplete="off" placeholder="Search Articles" /> <ul v-if="articles.length"> ...

Is there a way to deactivate a div in Blazor when clicking outside of the div?

Is it possible to have two divs on a single page, where one appears on the right side and the other on the left side? Is there a way to close or hide the left side div whenever we click on the right side div? ...

Sorting through JSON data obtained through YQL

Hello coding enthusiasts, After facing challenges with CORS in an AJAX project, I discovered a workaround using YQL to successfully retrieve JSON data. Now, I'm looking for ways to access and organize this data according to my preferences. Below is t ...

Is there a way to match a compressed javascript stack trace with a source map to pinpoint the correct error message?

When managing our production server, I have implemented minified javascript without including a map file to prevent users from easily deciphering errors. To handle angular exceptions caught by $exceptionHandler, I developed a logging service that forwards ...

Utilizing Jquery to automatically scroll to a specific div on page load by setting an

I am attempting to automatically scroll to the div specified in the URL. The URL may include one of 21 different IDs such as: url.com/test#lite1 url.com/test#lite2 url.com/test#lite3 This scrolling action should happen when the page loads (so that user ...

Altering the background color of an individual mat-card component in an Angular application

Here is the representation of my mat-card list in my Angular application: <div [ngClass]="verticalResultsBarStyle"> <div class="innerDiv"> <mat-card [ngClass]="barStyle" *ngFor="let card of obs | async | paginate: { id: 'paginato ...

Troubleshooting PHP/MySQL integration with Google Maps - issues persist

I have come across several other posts on this theme but unfortunately, none of them have been able to help me. I am using the article https://developers.google.com/maps/articles/phpsqlajax_v3, however, the code provided is not working for me. I have a fil ...

"Develop a mobile application using PhoneGap designed specifically for use on iPad and

Looking to create an app for iPad and iPad Mini using PhoneGap. While I have some basic knowledge of HTML and CSS, I am struggling with adjusting the image proportion, width, and height of the app. I want to write code that will adapt properly to the vary ...

Displaying Images on top of Images with React/Javascript/NextJS

I have two images. Let's say image 1 is a thumbnail for news content. I would like to overlay a promotional PNG banner on top of this image and create a new output image. This output image will be used as the OG (Open Graph) image. How can I achieve t ...

What could be causing the lack of updates for a watched object in vue?

Although there are many similar questions on this topic, mine has a unique twist. After delving into reactivity in depth, I feel like I have a solid grasp of the concept. However, one thing continues to baffle me: Summary: Why does a watched property det ...

Utilize Bootstrap to switch between 'collapse' and 'collapse in' depending on the device being used

I have a collapsible panel set to default "expand" (collapse in) and it is working well. However, I would like to make it "collapse" (shrink) for mobile and tablet devices. Is there any built-in feature in Bootstrap that can help me achieve this? <div ...

JavaScript function returning code

I am trying to create a JavaScript object with multiple functions, but I keep encountering an exception undefined is not a function Here is my JS code: var Cars = null; $(function () { Cars = function() { return { ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

Just easy highlighting using tags in Javascript

I have come across a code snippet that seems to be functioning well: <html> <head> <title>Testing JS Highlighting</title> <script type="text/javascript"> function highlight() { var t = ...

Upon reloading, the dynamically generated table does not appear accurately

When a table is dynamically created using JavaScript/jQuery/html from an Ajax call, it initially displays correctly formatted. However, upon refresh/reload, the formatting of the table becomes incorrect. To address this issue, I have implemented a Promise ...

Encountering a TypeScript type error when returning a promise from a function

I currently have a scenario in which there is a function that checks if user whitelisting is required. If not, it calls the allowUserToLogin function. If yes, it then checks if a specific user is whitelisted. If the user is not whitelisted, an error is thr ...

Utilize an Ajax function to call the BOT listening URL within a Web method

My recently developed web application features a submit button that triggers an Ajax call to communicate with the BOT. The code snippet used for the Ajax function is as follows: <script> $(document).ready(function () { $("#btnSend& ...

What is the method for incorporating jQuery UI effects into buttons that are imported from a basic HTML document?

I am continuously updating the content on my website in three distinct ways: 0) Utilizing jQuery's getJSON('bla.json') to read JSON files; 1) Fetching JSONized C#/Razor classes via jQuery's getJSON('bla.cshtml'); 2) Loading H ...