Masonry grid not showing properly

我怕爱的太早我们不能终老 提交于 2019-12-24 14:02:22

问题


here i am trying to get the masonry grid to my bootstrap row values. but when i initiate the masonry grid i am getting the grid with lots of space between the items in below rows. how can i resolve this?

FULL HTML:

<!DOCTYPE html>
<html>
<head>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .grid-item {width:400px;}
    </style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="grid">
    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=17"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=17" class="productname">Campingaz Expert Plus Gas Barbecue</a><br><br><span class="price">₹7995/-</span><h4><span>₹<strike class="maxprice">17800</strike></span></h4><span class="owners">0 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=15"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=15" class="productname">Milagrow Swimming Pool Robot : RoboPhelps</a><br><br><span class="price">₹29709/-</span><h4><span>₹<strike class="maxprice">150000</strike></span></h4><span class="owners">0 of 8 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=33"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=33" class="productname">Stanley Socket Set</a><br><br><span class="price">₹0/-</span><h4><span>₹<strike class="maxprice">3</strike></span></h4><span class="owners">0 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=36"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=36" class="productname">Deemark Portable Home Spa Havvy Massager</a><br><br><span class="price">₹0/-</span><h4><span>₹<strike class="maxprice">3</strike></span></h4><span class="owners">1 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=4"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=4" class="productname">Yamaha 01V96i Digital Mixer</a><br><br><span class="price">₹88630/-</span><h4><span>₹<strike class="maxprice">227900</strike></span></h4><span class="owners">0 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=17"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=17" class="productname">Campingaz Expert Plus Gas Barbecue</a><br><br><span class="price">₹7995/-</span><h4><span>₹<strike class="maxprice">17800</strike></span></h4><span class="owners">0 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=15"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=15" class="productname">Milagrow Swimming Pool Robot : RoboPhelps</a><br><br><span class="price">₹29709/-</span><h4><span>₹<strike class="maxprice">150000</strike></span></h4><span class="owners">0 of 8 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=33"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=33" class="productname">Stanley Socket Set</a><br><br><span class="price">₹0/-</span><h4><span>₹<strike class="maxprice">3</strike></span></h4><span class="owners">0 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=36"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=36" class="productname">Deemark Portable Home Spa Havvy Massager</a><br><br><span class="price">₹0/-</span><h4><span>₹<strike class="maxprice">3</strike></span></h4><span class="owners">1 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>


    <div class="col-xs-10 col-sm-4 col-md-4 col-lg-2 item"><div class="panel panel-default"><div class="panel-body"><a href="product-description.php?product_id=4"><br><br><img class="product_listing_img img-responsive" src="http://www.arredatutto.com/en/images/icecat/21892178-52336.jpg"><br></a><a href="product-description.php?product_id=4" class="productname">Yamaha 01V96i Digital Mixer</a><br><br><span class="price">₹88630/-</span><h4><span>₹<strike class="maxprice">227900</strike></span></h4><span class="owners">0 of 4 owners reached</span><span data-toggle="tooltip" data-placement="right" title="proposed by ashissin" class="proposer_icon"><img class="img-responsive" src="libraries/images/avatars/avatar-1.png"></span><a href="mycart.php" class="buy-button btn btn-warning col-xs-12 col-sm-12 col-md-12 col-lg-12"><span class="glyphicon glyphicon-shopping-cart"></span> Add to cart</a></div></div></div>
</div>
</div>
</div>

    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.2/masonry.pkgd.min.js"></script>
    <script>
        $(document).ready(function(){   
            $('.grid').masonry({
              itemSelector: '.grid-item',
              columnWidth: 400
            });
        });
    </script>
</body>
</html>

this is the output i am getting right now:


回答1:


Try this https://jsfiddle.net/2Lzo9vfc/132/

JS

$(document).ready(function(){   
  $('.grid').masonry({
    itemSelector: '.item',
  });
});

EDIT Use latest version of JQuery




回答2:


Masonry is not initialized. Also, itemSelector cannot be .grid-item, but only .item. Fix it like this:

$(document).ready(function(){   
    var container = document.querySelector('.grid');
    var msnry = new Masonry( container, {
        // options
        columnWidth: 400,
        itemSelector: '.item'
    });
});

Here's JSFiddle. I hope it helps.



来源:https://stackoverflow.com/questions/33821404/masonry-grid-not-showing-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!