1

我正在尝试创建一个钢琴键盘,该键盘将使用弹性框保持其元素比例,但一旦我开始更改窗口大小,似乎无法使黑色音符保持相同的宽度或高度。

这是一个小提琴

body{
width: 800px;
height: 200px;
display: flex;
}

#kbd {

padding: 1%;
flex-flow: column;
display: flex;
flex: 4;
}

#keys {
display: flex;
flex: 8;
justify-content: center;
}


.note {
flex: 1;
display: inline-flex;
align-items: center;
}

.black {
justify-content: center;
position: absolute;
height: 45%;
background-color: #474747;
color: white;
width: 8%;
margin: 0 -4%; 
}

.white {
flex-flow: column;
justify-content: flex-end;
outline: 2px solid #474747;
color: #474747;
background-color: #ffffff;
padding-bottom: 1%;
}
4

4 回答 4

3

百分比高度是根据第一个定位的父级的高度计算的。在这种情况下,#keys#kbddiv 没有positionCSS 规则。这意味着黑键是根据主体的宽度而不是它们的直接父级来缩放的。

添加position: relative;#keysdiv 以使其正常工作。

于 2015-04-15T17:12:27.960 回答
3

演示@Codepen


这不需要 Flexbox。响应式键盘布局可以通过以下方式实现:
 

  • 与每个键的注释对应的附加类选择器:
<div class="key white c"      </div>
<div class="key black c_sharp"</div>
<div class="key white d"      </div>
<div class="key black d_sharp"</div>
<div class="key white e"      </div>
<div class="key white f"      </div>
<div class="key black f_sharp"</div>
<div class="key white g"      </div>
<div class="key black g_sharp"</div>
<div class="key white a"      </div>
<div class="key black a_sharp"</div>
<div class="key white b"      </div>

 

  • 所有黑键和黑键之前的每个白键的边距,使用基于黑键大小的偏移量:
.a, .b, .d, .e, .g, .black{
  margin: 0 0 0 (-($blackKey_Width / 2) - $blackKey_BorderWidth);
}

 

  • 以下关键放置属性:
.key{
  float:    left;
  position: relative;
}

 

  • z-index黑键更高:
.white{
  z-index: 1;
}
.black{
  z-index: 2;
}

 

  • ( &&) 属性的视口单位[vw,vh,vmin,vmax]。您可以通过将黑键大小设置为白键的百分比来使其更具动态性。 heightwidth.white.black
于 2018-04-30T17:12:03.043 回答
2

位置应该是相对的而不是绝对的。此外,您应该将包含 div 的高度都设置为 100%。

参考小提琴链接:http: //jsfiddle.net/924sefae/6/

#kbd {
  height: 100%;
  ...
}
#kbd {
  height: 100%;
  ...
}

.black {
  position: relative;
  ...
}
于 2015-04-15T17:14:42.997 回答
0

不确定您是否需要将黑键作为独立块,但这是一个示例

<body>
 <div class="note-container">
 <div class="white-note black-note"></div> 
 <div class="white-note black-note"></div>
 <div class="white-note"></div> 
 <div class="white-note black-note"></div>
 <div class="white-note black-note"></div> 
 <div class="white-note black-note"></div>
 <div class="white-note"></div> 
 <div class="white-note black-note"></div>
 <div class="white-note black-note"></div> 
 <div class="white-note"></div>
</div>  
body {
  background: #000;
}

.note-container {
  width: 100%;
  display: flex;
  flex-flow: row nowrap;
}

.white-note {
  position: relative;
  width: 10%;
  height: 400px;
  background: #fff;
  margin: 0 2px;
}

.white-note.black-note:after {
  content: "";
  position: absolute;
  top: 0;
  right: -25%;
  width: 50%;
  height: 50%;
  background: #000;
  z-index: 1;
}
于 2020-07-19T22:53:29.760 回答