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

What is V8's approach to managing dictionaries?

After attending V8 presentations, it became clear to me that it optimizes constructions such as the one below by tagging a type object: function Point(x, y) { this.x = x; this.y = y; } I am curious about what happens if I were to return an object (JS ...

Transferring data using AJAX between an AngularJS frontend and a Node.js backend

Just a heads up: The main question is at the bottom in case you find this post too lengthy ;) I'm currently working on developing my first angularjs app and I've hit a roadblock when it comes to fetching data via ajax from my nodejs (express) se ...

Having trouble applying CSS to SVG icons from defs file in Webkit Browser

I've been tinkering with a project that incorporates SVG icons, hoping to style them using CSS for various scenarios and hover effects. However, my attempts are falling short in Webkit, while Firefox is displaying the icons as intended: http://codepe ...

When outputting HTML, make sure to use an if statement

Looking to dynamically generate select options without using if statements. Instead of string manipulation, I want a cleaner solution. <select name="test"> <option disabled selected> -- Select an industry -- </option> <?php ...

NgFor is failing to display data from a fully populated array of objects

CLICK HERE FOR THE IMAGE So, let's tackle the issue at hand. I have an array that contains data fetched from an API. These data points are essentially messages. Now, below is the TypeScript file for a component where I aim to display all these messag ...

Why is my NextJs app loading slowly on Safari but quickly on Chrome?

Currently, I am in the process of developing a web app using nextjs. I have encountered some issues with linking to pages, particularly the home page which contains multiple svgs and images. The performance lags when using Safari, as it does not show a loa ...

Storing radio button selection in a database using Ajax upon clicking

I have managed to successfully save the value of a radio button to the database on click using AJAX. However, I am facing an issue where if I go back to select the radio button again, nothing happens in the database. The value only gets updated when I se ...

Different choices for pick component in Bootstrap

Struggling to create an inline label with a select element using Bootstrap, I came across this helpful example on jsfiddle: http://jsfiddle.net/kokilajs/1q53pr6j/ <form id="searchForm" method="get" class="form-horizontal" role="form"> <div class= ...

Struggle with the background div menu

I am trying to create a menu with a background color, but when I use "background" or "background-color" on the div that contains my menu, I can't see any color. It's frustrating because I know it should be working.. Here is the HTML : <head ...

Discovering the process to create an onclick function with node.js code

index.html: <html> <h2>Welcome To User Profile Page !!!</h2> <button type="button">Edit Profile</button> <button type="button">Logout</button> </html> On my webpage, I have two buttons - Edit Profile and Lo ...

Using $_POST method to navigate within the same web page

<!doctype html> <html> <head> <meta charset="UTF-8"> <title>PHP links</title> <?php echo '<div style="background-color:#ccc; padding:20px">' . $_POST['message'] . '</div>'; ...

What sets $(document).on apart from ($ document).on in CoffeeScript?

One of my buddies is incorporating ($ document).on into his CoffeeScript script. I'm curious to know if this differs from the typical $(document).on and, if it does, how so? ...

FireFox is unresponsive to OPTIONS requests

I have a webpage that is accessed through HTTP. The client-side code is making AJAX requests for authorization to the same domain, but using HTTPS, which causes CORS issues. When using FireFox, the request looks like this: // domains and cookies are chang ...

What is the most effective way to display a tooltip next to an image?

I've been exploring various jQuery plugins in search of a tooltip feature that displays text, description, and rating while using the same image inside the tooltip. However, I have not been successful in finding one that fits my criteria. Therefore, ...

What is the best way to align the logo image to the left side of the Material UI app bar without any gaps?

Currently, I am working on a material UI app bar to serve as my navigation bar. The issue I am encountering involves the logo image on the left side of the page having excessive spacing from the edge, as shown in the image below. I've tried adjusting ...

Disabling a button following a POST request

Is there a way to prevent multiple clicks on a button after a post request is made? I want the button to be disabled as soon as it is clicked, before the post request is executed. Below is an example of my code where the button is supposed to be disabled w ...

What are the necessary conditions for executing npm start?

I'm encountering issues running npm start in my project, which is linked to my package.json file below: { "name": "babek", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test speci ...

Customizing Attribute for Material UI TextField

I'm currently in the process of adding a custom data attribute to a TextField component like so: class TestTextField extends React.Component { componentDidMount() {console.log(this._input)} render() { return ( <TextField label= ...

What is the best way to show two icons next to each other within a React component?

For my school project, I am working on developing an app and encountering a challenge. I have been trying to display two icons side by side on a screen using React (I've been learning React for 5 months but still consider myself a beginner). However, ...

Vanilla JavaScript Troubleshooting: Top Scroll Button Issue

Attempting to develop a Scroll To Top Button utilizing Vanilla JS, encountering errors in the dev console. Existing jQuery code that needs conversion to vanilla js Uncaught TypeError: Cannot read property 'addEventListener' of null My Vanilla ...