使用纯CSS重新排序flexbox

huangapple go评论59阅读模式
英文:

Reorder flexbox with pure css

问题

我想将不同的顺序编码到flex-items样式的css变量中,然后使用:checked伪类来切换编码顺序。

但是,我不知道如何解决作用域问题。有人可以帮我解决这个问题吗?

.expandable {
  order: var(--oldest);
  background-color: lightblue;
}

#expand-toggle:checked ~ * .expandable {
  order: var(--newest);
  background-color: darkseagreen;
}
<input type="checkbox" id="expand-toggle" />

<div style="display: flex; flex-direction: column">
  <div class="expandable" style="--newest: 1; --oldest: 4; order: var(--oldest);">A</div>
  <div class="expandable" style="--newest: 2; --oldest: 3; order: var(--oldest);">B</div>
  <div class="expandable" style="--newest: 3; --oldest: 2; order: var(--oldest);">C</div>
  <div class="expandable" style="--newest: 4; --oldest: 1; order: var(--oldest);">D</div>
</div>

我想象中可以使用order: calc(b*var(--oldest) + (1-b)*var(--newest))来实现,但这似乎是一种不正规的方法。

英文:

I'd like to encode different orders into css variables in the flex-items style, and then switch the encoded order using the :checked psudo-class.

However, I don't know how to get around the scoping problems. Can someone fix this for me.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.expandable {
  order: var(--oldest);
  background-color:lightblue;
}

#expand-toggle:checked ~ * .expandable {
  order: var(--newest);
  background-color:darkseagreen;
}

<!-- language: lang-html -->

&lt;input type=&quot;checkbox&quot; id=&quot;expand-toggle&quot; /&gt;

&lt;div style=&quot;display: flex; flex-direction: column&quot;&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 1; --oldest: 4; order: var(--oldest);&quot; &gt;A&lt;/div&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 2; --oldest: 3; order: var(--oldest);&quot; &gt;B&lt;/div&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 3; --oldest: 2; order: var(--oldest);&quot; &gt;C&lt;/div&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 4; --oldest: 1; order: var(--oldest);&quot; &gt;D&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I imagine its possible to do order: calc(b*var(--oldest) + (1-b)*var(--newest)) but this seems like a hack.

答案1

得分: 0

.expandable {
  order: var(--oldest);
  background-color: lightblue;
}

#expand-toggle:checked ~ * .expandable {
  order: var(--newest);
  background-color: darkseagreen;
}
<input type="checkbox" id="expand-toggle" />

<div style="display: flex; flex-direction: column">
  <div class="expandable" style="--newest: 1; --oldest: 4;">A</div>
  <div class="expandable" style="--newest: 2; --oldest: 3;">B</div>
  <div class="expandable" style="--newest: 3; --oldest: 2;">C</div>
  <div class="expandable" style="--newest: 4; --oldest: 1;">D</div>
</div>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.expandable {
  order: var(--oldest);
  background-color:lightblue;
}

#expand-toggle:checked ~ * .expandable {
  order: var(--newest);
  background-color:darkseagreen;
}

<!-- language: lang-html -->

&lt;input type=&quot;checkbox&quot; id=&quot;expand-toggle&quot; /&gt;

&lt;div style=&quot;display: flex; flex-direction: column&quot;&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 1; --oldest: 4;&quot; &gt;A&lt;/div&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 2; --oldest: 3;&quot; &gt;B&lt;/div&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 3; --oldest: 2;&quot; &gt;C&lt;/div&gt;
  &lt;div class=&quot;expandable&quot; style=&quot;--newest: 4; --oldest: 1;&quot; &gt;D&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 07:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定