How to make a column layout responsive with js?

Asked

Viewed 608 times

3

I need to create a column layout (masonry) responsive with pure js. I’ve tried several ways, but most columns are not responsive, and CSS organizes the numbering from top to bottom.

Um exemplo do que eu quero fazer

The responsiveness would be:

  • On cell phones: have a horizontal column.
  • On tablets: have two columns horizontally.
  • On computers: have three columns horizontally.

I’m using this code, but I can’t make the layout responsive or change the widths without destroying the layout.

function renderGrid() {
    var blocks = document.getElementById("grid_container").children;
    var pad = 10,
        cols = 3,
        newleft, newtop;
    for (var i = 1; i < blocks.length; i++) {
        if (i % cols == 0) {
            newtop = (blocks[i - cols].offsetTop + blocks[i - cols].offsetHeight) + pad;
            blocks[i].style.top = newtop + "px";
        } else {
            if (blocks[i - cols]) {
                newtop = (blocks[i - cols].offsetTop + blocks[i - cols].offsetHeight) + pad;
                blocks[i].style.top = newtop + "px";
            }
            newleft = (blocks[i - 1].offsetLeft + blocks[i - 1].offsetWidth) + pad;
            blocks[i].style.left = newleft + "px";
        }
    }
}
window.addEventListener("load", renderGrid, false);
window.addEventListener("resize", renderGrid, false);
div#grid_container {
    width: 900px;
    margin: 0px auto;
    height: 860px;
    border: #999 1px dashed;
}
div#grid_container > div {
    position: absolute;
    width: 291px;
    border: #000 1px solid;
}
div#grid_container > div:nth-child(2n+0) {
    background: #FFDC64;
}
div#grid_container > div:nth-child(2n+1) {
    background: #FEC910;
}
div#grid_container > div > div {
    padding: 20px;
    font-size: 27px;
    color: #D9A800;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head> 
<body>
<div id="grid_container">
  <div style="height:140px;"> <div>1</div> </div>
  <div style="height:200px;"> <div>2</div> </div>
  <div style="height:120px;"> <div>3</div> </div>
  <div style="height:180px;"> <div>4</div> </div>
  <div style="height:150px;"> <div>5</div> </div>
  <div style="height:160px;"> <div>6</div> </div>
  <div style="height:180px;"> <div>7</div> </div>
  <div style="height:170px;"> <div>8</div> </div>
  <div style="height:160px;"> <div>9</div> </div>
  <div style="height:180px;"> <div>10</div> </div>
  <div style="height:150px;"> <div>11</div> </div>
  <div style="height:160px;"> <div>12</div> </div>
  <div style="height:130px;"> <div>13</div> </div>
  <div style="height:140px;"> <div>14</div> </div>
  <div style="height:210px;"> <div>15</div> </div>
</div>
</body> 
</html>

  • @Phanpy I will edit to be clearer!

  • 1

    @Phanpy But my big question is how to make the code to stay in that layout!

  • Something keeps you from using one plugin? It’s boring to have to do it from scratch, if it’s for studies I think it’s worth it.

  • @Renan It is for both, I do not like to use plugins and is also for study!

  • Flexbox will help you a lot, take a look.

  • @Phanpy I want by percentage, but the code I got is static!

  • @Phanpy Pus

  • @Brunowego I’ll look into it!

  • I ended up creating a draft before Edit. I’m trying to make it work yet. I totally forgot about HTML and CSS :/

  • @Phanpy already I am a beginner in js, I saw this code you made and understood little.

  • I advise you to use only CSS to do this, I use this topic as a base: https://medium.com/@_jh3y/how-to-Pure-css-masonry-layouts-a8ede07ba31a#. bb64irsui

  • I recommend you use bootstrap to solve responsiveness, make it automatically for you.

Show 7 more comments

1 answer

0

You can do this only with CSS:

.grid_container{
  width: 100%;
  column-count: 4;
}

.child{
  display:block;
  min-height: 110px;
  width: 100%;
  -webkit-column-break-inside: avoid;
  -moz-column-break-inside: avoid;
  column-break-inside: avoid;
}

JSFIDDLE: https://jsfiddle.net/lucassilvax/bqb3a3hk/

  • A problem, if the window size is small (or mobile) should be shown only one column per row, as in the question, and in tablets would be 2 columns per row.

  • I tried, but the order is not correct, what should be in the first horizontal lines for example, goes to the end of the page! I need something like it’s in the image I sent, because it will be a dynamic gallery!

  • You see that property over there column-count ? You can control the amount of colunascom it, so use the CSS media queries to control the amount of columns according to the device resolution.

  • @media screen and (min-width: 768px) {&#xA; .grid_container{&#xA; column-count: 2;&#xA; }&#xA;}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.