zhangxinxu/quiz

CSS基础测试20期

Opened this issue · 7 comments

实现一个有些挑战的布局:

图片无法访问点击这里

本题不需要考虑视觉还原,主要是布局效果实现。

请附上对应的CSS代码,注意缩进和代码高亮,可以使用下面语法进行高亮:

```html
你的代码在这里
```
```css
你的代码在这里
```

本期小测需要提供在线demo,不要参考别人的回答,诚实守信。

本期小测无直播答疑,但是会总结要点。

感谢您的参与!

demo

实力眼瞎……原题是要保证中间的内容处于一直显示的状态。

稍作修改,核心思路还是利用 flex 多行显示,修改了 html 结构来保证中间的内容处于一直显示的状态。

html

<div class="box">
  <div class="reverse">
    <span>222</span>
    <span>111</span>
  </div>
  <span>333</span>
</div>

css

.box,
.reverse  {
  display: flex;
  flex-wrap: wrap;
  height: 30px;
  overflow: hidden;
}

.reverse {
  flex-direction: row-reverse;
  justify-content: flex-end;
}

span {
  flex-shrink: 1;
  width: 98px;
  height: 28px;
  border: 1px solid black;
}

核心思路:指定 flex 元素多行显示,通过overflow隐藏第一行下面的内容。

html

<div class="box limit">
  <span>111</span>
  <span>222</span>
  <span>333</span>
  <span>444</span>
  <span>555</span>
</div>

css

.limit {
  width: 150px;
}

.box {
  display: flex;
  flex-wrap: wrap;
  height: 30px;
  overflow: hidden;
}

.box span {
  flex-shrink: 0;
  width: 98px;
  height: 28px;
  border: 1px solid black;
}

看了下 @ livetune 大佬的实现,才发现自己根本没理解题意。思考了很久并没有想到别的解法,只能模仿一下大佬了,原理就是利用一个元素占位,当宽度缩小时脱离占位的位置

Demo

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <main class="date-picker-wrapper">
    <div class="date-picker-col-wrapper">
        <i class="date-picker-col-padding"></i>
        <ul class="date-picker">
            <li>2020年4月-6月</li>
            <li>2020年1月-3月</li>
            <li>2020 年度 OKR</li>
        </ul>
    </div>
</main>
</body>
</html>
.date-picker-wrapper {
  display: flex;
  align-items: center;
  height: 30px;
  overflow: hidden;

}
.date-picker-col-wrapper {
  display: flex;
  flex: 1;
  flex-direction: column;
  height: 90px;
}
.date-picker-col-padding {
  flex: 1;
  max-height: calc(100%  / 3)
}

.date-picker {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    list-style: none;
    white-space: nowrap;
    padding: 0;
    margin: 0;
    position: relative;
}

li {
    flex: 1;
    text-align: center;
    padding: 0 40px;
    height: 30px;
    border: 1px solid;
    box-sizing: border-box;
}

.date-picker-col-wrapper::before {
  content: '<';
  position: absolute;
  left: 0px;
  top: 10px;
}

.date-picker-col-wrapper::after {
  content: '>';
  position: absolute;
  right: 0px;
  top:10px;
}

DEMO
虽然是实现的,但是html嵌套的有点深。。

<section class="date-picker-wrapper">
    <icon class="arrow left-icon"></icon>
    <div class="date-picker-col-wrapper">
        <i class="date-picker-col-padding"></i>
        <ul class="date-picker">
            <li>2020年4月-6月</li>
            <li>2020年1月-3月</li>
            <li>2020 年度 OKR</li>
        </ul>
    </div>
    <icon class="arrow right-icon"></icon>
</section>
.arrow {
    display: inline-block;
    width: 30px;
    height: 30px;
    background: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzMjAgNTEyIj4gPHBhdGggZD0iTTM0LjUyIDIzOS4wM0wyMjguODcgNDQuNjljOS4zNy05LjM3IDI0LjU3LTkuMzcgMzMuOTQgMGwyMi42NyAyMi42N2M5LjM2IDkuMzYgOS4zNyAyNC41Mi4wNCAzMy45TDEzMS40OSAyNTZsMTU0LjAyIDE1NC43NWM5LjM0IDkuMzggOS4zMiAyNC41NC0uMDQgMzMuOWwtMjIuNjcgMjIuNjdjLTkuMzcgOS4zNy0yNC41NyA5LjM3LTMzLjk0IDBMMzQuNTIgMjcyLjk3Yy05LjM3LTkuMzctOS4zNy0yNC41NyAwLTMzLjk0eiI+PC9wYXRoPiA8L3N2Zz4=  ) center no-repeat;
    background-size: 10px;
}

.right-icon {
    border-right: 1px solid #ddd;
    transform: rotateZ(180deg);
}

body {
    background: #f1f1f1;

}

.arrow {
    flex: 0 0 30px;
}

.date-picker-wrapper {
    display: flex;
    width: 100%;
    background: #fff;
    justify-content: space-between;
    align-items: center;
    height: 30px;
    margin-top: 40px;
    overflow: hidden;

}

.date-picker {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    list-style: none;
    white-space: nowrap;
    padding: 0;
    margin: 0;
}

li {
    line-height: 30px;
    flex: 1;
    text-align: center;
    padding: 0 40px;
    height: 30px;
    border-left: 1px solid #ddd;

}

li:nth-child(2) {
    color: skyblue;
}

.date-picker-col-wrapper {
    display: flex;
    flex: 1;
    flex-direction: column;
    height: 90px;
}

.date-picker-col-padding {
    flex: 1;
    max-height: calc(100% / 3);
}

我思考了好久,我的想法跟@livetune的实现是一样的,但一直没有解决掉只有两行的情况下如何还能保证中间行还是在中间的问题


勉强实现了一下😊
jsbin

.container {
  display: flex;
  resize: horizontal;
  overflow: hidden;
  border: 1px solid;
  width: 200px;
  height: 21px;
}

.arrow {
  flex: 0
}

.content {
  flex: 1;
  display: inline-flex;
  flex-wrap: wrap;
}

.item-wrap {
  flex: 1 1 66.66%;
  display: inline-flex;
  flex-wrap: wrap-reverse;
}

.item {
  flex: 1 1 auto;
  white-space: nowrap;
  text-align: center;
}

.item-1 {
  color: #6593ff;
}

.item-3 {
  flex: 1 1 33.33%;
}
<div class="container">
  <span class="arrow"> &#60; </span>
  <span class="content">
    <span class="item-wrap">
      <span class="item item-2">2020年4月-6月</span>
      <span class="item item-1">2020年1月-3月</span>
    </span>
    <span class="item item-3">&nbsp;2020年度OKR&nbsp;</span>
  </span>
  <span class="arrow">&#62;</span>
</div>

demo

用的是grid布局的auto-fill,每项宽度目前是固定的;不知道需不需要支持按特定顺序逐渐显示?目前只是按元素的DOM顺序逐渐显示的。

.container {
  display: grid;
  grid-template-columns: repeat(auto-fill, 200px);
  height: 30px;
  overflow-y: hidden;
}
.item {
  height: 30px;
  line-height: 30px;
  text-align: center;
}
.item:nth-of-type(1) {
  background-color: rgba(255, 0, 0, 0.1);
}
.item:nth-of-type(2) {
  background-color: rgba(0, 255, 0, 0.1);
}
.item:nth-of-type(3) {
  background-color: rgba(0, 0, 255, 0.1);
}
<div class="container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

在线 Demo CodePen

  1. inline-block 方案,最后一个符号的宽度需要定死
  2. flex 方案,需要额外嵌套一层

两个方案逻辑其实是一致的,就是让最后一个元素先脱离文档流,然后前面的内容利用正常文档流换行,隐藏超出部分。

/* 基础样式 */
.item {
  height: 16px;
  display: inline-block;
  text-align: center;
  border: 1px solid #ddd;
  padding: 7px 1em;
  margin-right: -1px;
  text-decoration: none;
  color: #333;
  background-color: #ffffff;
}
.item._f {
  border-radius: 3px 0 0 3px;
}
.item._l {
  border-radius: 0 3px 3px 0;
}
a.item:hover {
  background-color: #f5f5f5;
}
.item._on {
  color: #4c5fe2;
}
/* inline-block */
.box {
  font-size: 0;
  font-family: sans-serif;
  text-align: right;
  padding-right: 30px;
  position: relative;
  overflow: hidden;
  height: 32px;
}
.box .item {
  font-size: 12px;
}
.box .item._l {
  position: absolute;
  top: 0;
  right: 1px;
  width: 7px;
}

/* flex */
.box-flex {
  font-family: sans-serif;
  display: flex;
  justify-content: flex-end;
  white-space: nowrap;
  font-size: 12px;
}
.inner {
  display: flex;
  justify-content: flex-end;
  overflow: hidden;
  height: 32px;
  flex-wrap: wrap;
}
<p>拖拽右侧滑块缩小屏幕查看效果</p>

<h3>inline-block + absolute</h3>
<section class="box">
  <a class="item _f" href="##" title="prev">&lt;</a>
  <a class="item" href="##" title="2020年7月-9月">2020年7月-9月</a>
  <a class="item" href="##" title="2020年4月-6月">2020年4月-6月</a>
  <strong class="item _on">2020年1月-3月</strong>
  <a class="item" href="##" title="2020年度OKR">2020年度OKR</a>
  <a class="item" href="##" title="2020年度OKR">2020年度OKR2</a>
  <a class="item _l" href="##" title="next">&gt;</a>
</section>

<h3>Flex</h3>
<section class="box-flex">
  <span class="inner">
    <a class="item _f" href="##" title="prev">&lt;</a>
    <a class="item" href="##" title="2020年7月-9月">2020年7月-9月</a>
    <a class="item" href="##" title="2020年4月-6月">2020年4月-6月</a>
    <strong class="item _on">2020年1月-3月</strong>
    <a class="item" href="##" title="2020年度OKR">2020年度OKR</a>
    <a class="item" href="##" title="2020年度OKR">2020年度OKR2</a>
  </span>
  <a class="item _l" href="##" title="next">&gt;</a>
</section>

demo

.flex-wrapper {
    display: flex;
    flex-wrap: nowrap;

}

.flex-content {
    display: flex;
    flex-wrap: wrap;
    flex-shrink: 1;
    height: 33px;
    overflow: hidden;
}

span {
    padding: 5px 10px;
    border: 1px solid #ddd;
}

span.item {
    flex-grow: 1;
    display: inline-block;
    text-align: center;
    white-space: nowrap;
}
<div class="flex-wrapper">
    <span class="before">&lt;</span>
    <div class="flex-content">
        <span class="item">2020年4月-6月</span>
        <span class="item">2020年1月-3月</span>
        <span class="item">2020年度OKR</span>
    </div>
    <span class="next">&gt;</span>
</div>