admin管理员组

文章数量:1291236

I try setting up a CSS grid layout as follows

.wrapper {
  width: 800px;
  display: grid;
  grid-template-columns: repeat(3, 200px);
  grid-template-rows: repeat(5, 200px);
}

Is it possible to locate grid-template-columns in JS then re-size the columns? I came across a situation where I could find it (lost the code) but when I tried changing it, Chrome DevTools say the value is puted and cannot be changed.

Any help pointing me in the right direction (or other ways to do it, but use of grid is a must) is highly appreciated.

Thanks.

I try setting up a CSS grid layout as follows

.wrapper {
  width: 800px;
  display: grid;
  grid-template-columns: repeat(3, 200px);
  grid-template-rows: repeat(5, 200px);
}

Is it possible to locate grid-template-columns in JS then re-size the columns? I came across a situation where I could find it (lost the code) but when I tried changing it, Chrome DevTools say the value is puted and cannot be changed.

Any help pointing me in the right direction (or other ways to do it, but use of grid is a must) is highly appreciated.

Thanks.

Share Improve this question edited Sep 11, 2017 at 16:25 Michael Benjamin 372k110 gold badges613 silver badges730 bronze badges asked Sep 11, 2017 at 2:46 MikeCMikeC 2231 gold badge4 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

@MikeC, if you're not opposed to using jQuery, you can change the column width using jQuery's .css() function, which

Get the value of a puted style property for the first element in the set of matched elements or set one or more CSS properties for every matched element.

You can read more on it in jQuery's documentation for the function here.

Also, just so you can see it in action, I put together a codeply project that you can see it in action. If you click anywhere on the grid, it will resize (only once though). It's a primitive example.

Here's the jQuery code it uses.

$( "#grid" ).one( "click", function() {
  $( this ).css( "grid-template-columns", "repeat(2, 400px)" );
});

@MikeC You didn't tell anybody how you got it to work. Assuming you have a button or some event that calls a toolExpandToggle() and assume that your class name for your grid CSS definition is called "canvas" then you can do something like this. In the CSS I have:

@media only screen and (min-width: 768px)   {
.canvas {
    grid-gap: .0em;
    grid-template-columns: 50px auto;      /* the values are the toolbar, content */
    grid-template-rows: 50px auto 25px;     /* The values are o365Nav (O365 stardard), content, statusbar. Note: o365Nav heigth is the padding value used by the modal menu. */
    grid-template-areas:
    "o365Nav  o365Nav"
    "tool content"
    "statusbar  statusbar";
}

}

In the HTML I have a button. Notice that I have my widths defined as data items where 50 matches the CSS. In any case these values will replace the CSS when used.

    <div class="tool" id="pl-tool" data-collasped="50"; data-expanded="200";>
    <div class="spacer"></div>
    <button class="tool-ms-button"><span class="ms-Icon ms-Icon--GlobalNavButton tool-ms-icon" aria-hidden="true" onclick="toolExpandToggle('pl-tool')"></span></button>
    <!-- now the tool bar buttons -->
    <div><button class="tool-button">item-a</button></div>
    <div><button class="tool-button">item-a</button></div>
</div>

Then I have a javascript function:

function toolExpandToggle(target) {
    let id = document.getElementById(target);
    let c = id.dataset.collasped;
    let e = id.dataset.expanded;

    let w = document.getElementsByClassName('tool')[0].offsetWidth;
    if(w < e) {
       document.getElementsByClassName('canvas')[0].style.gridTemplateColumns = e + 'px auto';
    }
    else {
        document.getElementsByClassName('canvas')[0].style.gridTemplateColumns = c + 'px auto';
    }

}

In my case I only had two columns. When I click a button I call the toggle method and change it to the data value. I hope this gets someone else a little further down the track.

This is significantly easier these days now that we have css vars and can change them with javascript:

var onclick = function() {
  if (getComputedStyle(document.querySelector(':root')).getPropertyValue('--column-width') === '100px') {
    document.querySelector(':root').style.setProperty('--column-width', '200px');
  } else {
    document.querySelector(':root').style.setProperty('--column-width', '100px');
  }
}
:root {
  --column-width: 100px;
}

.wrapper {
  width: 800px;
  display: grid;
  grid-template-columns: repeat(3, var(--column-width));
  grid-template-rows: repeat(1, 200px);
}

.one {
  background: green;
}

.three {
  background: blue;
}
<div class="wrapper">
  <div class="one">Column one</div>
  <button onclick="onclick">Click me</button>
  <div class="three">Column 3</div>
</div>

The js above will toggle the css var back and forth from 100 to 200 px. As long as you use the css var in the css, it'll update accordingly.

本文标签: CSS grid layout changing column width with javascriptStack Overflow