1

Good evening,

I'm having an issue with Masonry.

This is all my relevant code:

var $test;
var $iterator = 0; 
$(document).ready(function() {
    $("#demo-input").tokenInput("php-example.php" ,{
        classes: {
            tokenList: "token-input-list-facebook",
            token: "token-input-token-facebook",
            tokenDelete: "token-input-delete-token-facebook",
            selectedToken: "token-input-selected-token-facebook",
            highlightedToken: "token-input-highlighted-token-facebook",
            dropdown: "token-input-dropdown-facebook",
            dropdownItem: "token-input-dropdown-item-facebook",
            dropdownItem2: "token-input-dropdown-item2-facebook",
            selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
            inputToken: "token-input-input-token-facebook"
        },
        hintText: "Type in the names of your Tags",
        noResultsText: "No results",
        searchingText: "Searching..."
    });
});

var $container = $('#container');

$container.imagesLoaded( function(){
    $container.masonry({
        itemSelector: '.box',
        columnWidth: 1,
        isAnimated: !Modernizr.csstransitions
    });
});

$(document).ready(function() {
    $("input[type=button]").click(function () {
        $.ajax({ url: "generatehtml.php",
        data: {action: $(this).siblings("input[type=text]").val()},
        type: 'post',
        dataType: "json",
        success: 
            function(response){
                $test=response;
                $iterator =  $test.length;

                for (i=0; i<10; i++){
                    var $boxes = $(balls($iterator));
                    $container.append( $boxes ).imagesLoaded( function(){$container.masonry( 'appended', $boxes);});            
                }
                var $boxes = $( '<div class="box" STYLE="color: rgba(255, 255, 255, 1);">These results are ranked from<BR> most similar, to least similar.<BR>The percentage below each game <BR>indicates how similar to the <BR>original input that game is.<BR></div>' );
                $container.prepend( $boxes ).masonry( 'reload' );       
            }
        });
    });
});


window.onscroll = scroll;

function scroll() {  
    var $boxes = $(balls($iterator));
    $container.append( $boxes ).imagesLoaded( function(){$container.masonry( 'appended', $boxes, true);});
}  
//+'<img src="timthumb.php?src='+$test[$iterator][2]+'&q=100&w=300"/>' Replace this with the one below when timthumb is whitelisted
function balls($i){
    $iterator -= 1;
    var $width = 7.5;
    return ('<div class="box">'
    +''+$test[$iterator][1][2]+''
    +'<img src="'+$test[$iterator][2]+'"  width="280" height="160"/>'
    +'<div id=boxBottom>'+Math.floor($test[$iterator][0]*100)+'%</div>'
    +'</div>');
}

The behaviour I expect: Load page, select game from second search bar, hit 'submit'.
Populate with 10 results, upon scrolling, populate with more results.

But the formatting is all over the place, and I know you're supposed to use the imagesLoaded() plugin, but I do.

Anyway, here is the site in question:

http://newgameplus.nikuai.net/TEST/ (First search bar doesn't work btw, so ignore that)

Any resolution would be greatly appreciated. Thanks so very much.

Adola
  • 588
  • 7
  • 21

1 Answers1

2

Try changing your columnWidth to the same as the image in the box:

$container.imagesLoaded( function(){
    $container.masonry({
        itemSelector: '.box',
        columnWidth: 280,
        isAnimated: !Modernizr.csstransitions
    });
});

Update the line:

$container.append( $boxes ).imagesLoaded( function(){$container.masonry( 'appended', $boxes);});

to:

$container.append( $boxes ).masonry('appended', $boxes);

I also think you should revise your $(document).ready sections. You currently have 2 of them and the #container element is retrieved between the two. I think you should only have the one and make sure the $container is fetched in the $(document).ready.

Updated code:

var $test;
var $iterator = 0; 
$(document).ready(function() {
    $("#demo-input").tokenInput("php-example.php" ,{
        classes: {
            tokenList: "token-input-list-facebook",
            token: "token-input-token-facebook",
            tokenDelete: "token-input-delete-token-facebook",
            selectedToken: "token-input-selected-token-facebook",
            highlightedToken: "token-input-highlighted-token-facebook",
            dropdown: "token-input-dropdown-facebook",
            dropdownItem: "token-input-dropdown-item-facebook",
            dropdownItem2: "token-input-dropdown-item2-facebook",
            selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
            inputToken: "token-input-input-token-facebook"
        },
        hintText: "Type in the names of your Tags",
        noResultsText: "No results",
        searchingText: "Searching..."
    });

    var $container = $('#container');

    $container.imagesLoaded( function(){
        $container.masonry({
            itemSelector: '.box',
            columnWidth: 280,
            isAnimated: !Modernizr.csstransitions
        });
    });

    $("input[type=button]").click(function () {
        $.ajax({ url: "generatehtml.php",
        data: {action: $(this).siblings("input[type=text]").val()},
        type: 'post',
        dataType: "json",
        success: 
            function(response){
                $test=response;
                $iterator =  $test.length;

                for (i=0; i<10; i++){
                    var $boxes = $(balls($iterator));
                    $container.append( $boxes ).masonry('appended', $boxes);            
                }
                var $boxes = $( '<div class="box" STYLE="color: rgba(255, 255, 255, 1);">These results are ranked from<BR> most similar, to least similar.<BR>The percentage below each game <BR>indicates how similar to the <BR>original input that game is.<BR></div>' );
                $container.prepend( $boxes ).masonry( 'reload' );       
            }
        });
    });
});
JonWarnerNet
  • 1,112
  • 9
  • 19
  • There's still some overlapping in say, FireFox. I don't think I'm using that plugin correctly, mostly because I don't really know how it works. – Adola Jun 23 '12 at 14:52
  • your test code has 2 columnWidth's, please update with just the one with a value of 280. – JonWarnerNet Jun 23 '12 at 14:53
  • After updating, Chrome still seems to function fine, but firefox still exhibits issues. And truthfully, my images won't be the same width everytime, some of them ill be different sizes, so I'm not sure it that will work. Is there any point in calling imagesLoad() again after I already called it in the script? I'm talking about here: $container.append( $boxes ).imagesLoaded( function(){$container.masonry( 'appended', $boxes);}); – Adola Jun 23 '12 at 14:59
  • see updated answer, I think it's your 2nd call to imagesLoaded on append. – JonWarnerNet Jun 23 '12 at 15:01
  • Ah-ha! That actually works now! I'd call you a gentleman and a scholar if you could help me figure out how to instead to get rid of the height and width statments in the imgsrc. (I ask, because those images look a bit squished.) – Adola Jun 23 '12 at 15:09
  • Please mark the answer as correct if this answered your question. Could you please post another question for the width/height, but typically if you know the images are larger you can set a max-width and max-height via css - however positioning is still a problem for which you might need some JS. – JonWarnerNet Jun 23 '12 at 15:20
  • http://stackoverflow.com/questions/11170735/jquery-masonry-appending-images-with-custom-sizes For the second part of my question. – Adola Jun 23 '12 at 15:34