gpt4 book ai didi

html - 使用 flexbox 的计算器键盘布局

转载 作者:太空狗 更新时间:2023-10-29 12:31:52 24 4
gpt4 key购买 nike

我正在用 flexbox 构建一个计算器。我想要它的一个键是高度的两倍,另一个键是宽度的两倍。

我在谷歌上搜索了很多,但没能同时找到这两种情况。

对于两倍高度的键,我找到的唯一答案是将 flex-direction 设置为 column。但在那种情况下,我将无法制作双宽度键。

这里是 my code (在 codepen.io 上)。请帮忙。

$(function() {
var curr = "",
prev = "";
var updateView = function() {
$('#curr').html(curr);
$('#prev').html(prev);
};
$('.keysNum').on('click', function(e) {
curr += $(this).html();
console.log(this);
updateView();
});
$('.keysOp').on('click', function(e) {

});
});
.flexBoxContainer {
-webkit-touch-callout: none;
-webkit-user-select: none;
user-select: none;
display: flex;
justify-content: space-around;
align-items: center;
width: 100%;
min-height: 100vh;
}

.calculator {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: center;
width: 100%;
min-height: 100vh;
}

@media (min-width: 321px) {
.calculator {
width: 320px;
}
}

.calculator .keys {
border: #d3d2cb 0.5px solid;
background: #fefdff;
color: #33393d;
height: 50px;
height: 14.2857142857vh;
width: 25%;
line-height: 14.2857142857vh;
text-align: center;
font-size: 1.4rem;
font-weight: bold;
transition: background 0.2s linear;
}

.calculator .keysOp {
background: #f1f1ef;
}

.calculator .keysC {
color: #f94913;
}

.calculator .keys:hover {
background: #d3d2cb;
transition: background 0s linear;
}

.calculator .keys:focus {
outline: none;
}

.calculator .keys:active {
background: #93938E;
}

.calculator .screen {
background: #e9e8e5;
height: 14.2857142857vh;
width: 100%;
line-height: 14.2857142857vh;
direction: rtl;
}

.calculator .screen:last-child {
font-size: 4rem;
}

.calculator #anomaly-keys-wrapper {
display: flex;
width: 100%;
}

.calculator #anomaly-keys-wrapper>section:first-child {
display: flex;
flex-wrap: wrap;
width: 75%;
}

.calculator #anomaly-keys-wrapper>section:first-child>div.keys {
flex: 1 0 33.33%;
}

.calculator #anomaly-keys-wrapper>section:first-child>div.long {
flex-basis: 66.67%;
}

.calculator #anomaly-keys-wrapper>section:last-child {
width: 25%;
display: flex;
flex-direction: column;
}

.calculator #anomaly-keys-wrapper>section:last-child>.tall {
background: #f94913;
color: #fefdff;
width: 100%;
line-height: 28.5714285714vh;
flex: 1;
}

.calculator #anomaly-keys-wrapper>section:last-child>.tall:hover {
background: #c73a0f;
}

.calculator #anomaly-keys-wrapper>section:last-child>.tall:focus {
outline: none;
}

.calculator #anomaly-keys-wrapper>section:last-child>.tall:active {
background: #8b280a;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="flexBoxContainer">
<div class="calculator">
<div class="screen" id="prev"></div>
<div class="screen" id="curr"></div>
<!-- <div class="keys keysC keysOp" tabindex="2">C</div> -->
<div class="keys keysC keysOp" tabindex="2">C</div>
<div class="keys keysOp" tabindex="3"><i class="icon ion-backspace-outline"></i></div>
<div class="keys keysOp" tabindex="4">&divide</div>
<div class="keys keysOp" tabindex="5">&times</div>
<div class="keys keysNum" tabindex="6">7</div>
<div class="keys keysNum" tabindex="7">8</div>
<div class="keys keysNum" tabindex="8">9</div>
<div class="keys keysOp" tabindex="9">-</div>
<div class="keys keysNum" tabindex="10">4</div>
<div class="keys keysNum" tabindex="11">5</div>
<div class="keys keysNum" tabindex="12">6</div>
<div class="keys keysOp" tabindex="13">+</div>
<section id="anomaly-keys-wrapper">
<section>
<div class="keys keysNum" tabindex="14">1</div>
<div class="keys keysNum" tabindex="15">2</div>
<div class="keys keysNum" tabindex="16">3</div>
<div class="keys long keysNum" tabindex="17">0</div>
<div class="keys" tabindex="18">.</div>
</section>
<section>
<div class="keys tall" tabindex="19">=</div>
</section>
</section>
</div>
</div>

最佳答案

将不均匀的键包裹在它们自己的 flex 容器中,然后从那里开始......

* { box-sizing: border-box; }                                      /* 1 */

.flexBoxContainer {
display: flex;
justify-content: space-around;
align-items: center;
width: 100%;
}

.calculator {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: center;
width: 100%;
}

.calculator .keys {
border: red 1px solid;
height: 50px;
width: 25%;
break-inside: avoid;
}

.calculator input {
height: 100px;
width: 100%;
direction: rtl;
}

#anomaly-keys-wrapper { /* 2 */
display: flex;
width: 100%;
}

#anomaly-keys-wrapper > section:first-child { /* 3 */
display: flex;
flex-wrap: wrap;
width: 75%;
}

#anomaly-keys-wrapper > section:first-child > div { /* 4 */
flex: 1 0 33.33%;
}

#anomaly-keys-wrapper > section:first-child > div:nth-child(4) { /* 5 */
flex-basis: 66.67%;
}

#anomaly-keys-wrapper > section:last-child { /* 6 */
width: 25%;
display: flex;
flex-direction: column;
}

#anomaly-keys-wrapper .tall { /* 7 */
width: 100%;
flex: 1;
}

@media (min-width: 321px) {
.calculator {
width: 320px;
}
}
<div class="flexBoxContainer">
<div class="calculator">
<input />
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<section id="anomaly-keys-wrapper">
<section>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys"></div>
<div class="keys long"></div>
<div class="keys"></div>
</section>
<section>
<div class="keys tall"></div>
</section>
</section>
</div>
</div>

Revised Codepen (编译后的 CSS)

注意事项:

  1. width 中包含填充和边框/height计算。
  2. 将不均匀的键包装在一个单独的 flex 容器中(默认为 flex-direction: rowflex-wrap: nowrap )
  3. long 键包装在一个单独的 flex 容器中并启用包装(并使用足够多的兄弟来创建与 tall 键相同的高度)。
  4. 每行最多强制三个键。
  5. 键的宽度是同级键的两倍。 (由于特异性较弱,没有使用更简单的 long 类选择器。)
  6. tall 键包装在一个垂直方向的单独 flex 容器中。
  7. 使 tall 键占用容器的所有可用宽度和高度。

更新

来自评论:

Hi, 1. Can you explain me how flex basis works? and why did you use it instead of giving width to long button. 2. Why is it necessary to give flex: 1; to tall button, as i have read it is the default value.

问题 #1:

第一个子部分容器(包含 .long )中的键的大小为 flex: 1 0 33.33% .

这是 flex-grow: 1 的简写, flex-shrink: 0 , 和 flex-basis: 33.33% .

对于 .long key 我们只是简单地覆盖了 flex-basis组件 66.67% . (无需重新声明其他两个组件)。

此外,在这种情况下 width 之间确实没有区别和 flex-basis , 但由于我们重写了 flex-basis , 我用过 flex-basis .

使用 width会留下原来的 flex-basis: 33.33%完好无损,创造两个 width因此,可能无法扩展 .long 的规则键,取决于级联中的规则。

关于 flex-basis 的完整解释与 width , 请参阅 What are the differences between flex-basis and width?

问题 #2:

因为flex-grow的初始值组件是 0 (source)。

关于html - 使用 flexbox 的计算器键盘布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39079773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com