【问题标题】:Draw dynamic Circle with connected line responsive绘制具有连接线响应的动态圆
【发布时间】:2022-02-09 17:01:57
【问题描述】:

我想用连接线响应式设计创建一个圆圈

当我调整屏幕大小时

但我的期望是这样的,有人可以帮助我吗?

这是我的 HTML 和 CSS 代码:

<html>
<style>
li {
  width: 4em;
  height: 4em;
  text-align: center;
  line-height: 4em;
  border-radius: 3em;
  background: #1F3864;
  margin: 0 2em;
  display: inline-block;
  color: white;
  position: relative;
  margin-bottom: 2rem;
}

li::before{
  content: "";
  position: absolute;
  top: 2em;
  left: -4em;
  width: 4em;
  height: 0.2em;
  background: #1F3864;
  z-index: -1;
}
li:first-child::before {
  display: none;
}
</style>
<ul>
  <li> M1 </li>
  <li> M2 </li>
  <li> M3 </li>
  <li> M4 </li>
  <li> M5 </li>
  <li> M6 </li>
  <li> M7 </li>
  <li> M8 </li>
  <li> M9 </li>
  <li> M10 </li>
  <li> M11 </li>
</ul>
</html>

它使用角度的动态数据。 举例

<ul>
      <li *ngFor="let a of listOfCircle"> {{a.code}} </li>
  </ul>

【问题讨论】:

  • 我觉得你想要实现的布局单靠CSS是做不到的。
  • 你能说一下需要什么断点吗?例如,如果视口是 600px 宽,那么每行需要多少个圆圈?
  • @Terry 然后可以建议或举个例子
  • @AHaworth 每行最大 10 用于大屏幕每行 5 用于小屏幕。但圈出一些时间 13 有时我可以是 20
  • 您可以只使用 CSS 来执行此操作,因为您知道断点。您问题中的图像连续显示 4 个,这是否意味着您有多个媒体宽度断点?

标签: javascript html css angular


【解决方案1】:

如果你知道你想要什么断点(即你想要多少列的宽度),这可以在纯 CSS 中完成。

这个 sn-p 有两个集合。第一个用于宽度 >600px,第二个用于宽度

它使用网格并使用 CSS nth-child 工具反转由交替行占用的列的顺序。

<!doctype html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
    body {
      width: 100vw;
    }
    
    .container {
      display: flex;
      justify-content: center;
      width: 90vw;
      margin: 0 auto;
    }
    
    .circles {
      width: 100%;
      display: grid;
      grid-template-columns: repeat(var(--cols), 1fr);
      --gap: 4vw;
      gap: var(--gap);
      grid-auto-flow: dense;
    }
    
    .circles>* {
      width: 100%;
      background-color: navy;
      color: white;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      aspect-ratio: 1 / 1;
      display inline-block;
      position: relative;
    }
    
    .circles>*::after {
      content: '';
      position: absolute;
      width: var(--gap);
      height: 1px;
      background-color: navy;
      top: 50%;
      transform: translateY(-50%);
      left: 100%;
    }
    
    @media (min-width: 600px) {
      .circles {
        --cols: 10;
      }
      .circles>*:nth-child(20n-10)::after {
        top: 100%;
        left: 50%;
        transform: translateX(-50%);
        height: var(--gap);
        width: 1px;
      }
      .circles>*:nth-child(20n-9) {
        grid-column: 10;
      }
      .circles>*:nth-child(20n-9)::after {
        display: none;
      }
      .circles>*:nth-child(20n-8) {
        grid-column: 9;
      }
      .circles>*:nth-child(20n-7) {
        grid-column: 8;
      }
      .circles>*:nth-child(20n-6) {
        grid-column: 7;
      }
      .circles>*:nth-child(20n-5) {
        grid-column: 6;
      }
      .circles>*:nth-child(20n-4) {
        grid-column: 5;
      }
      .circles>*:nth-child(20n-3) {
        grid-column: 4;
      }
      .circles>*:nth-child(20n-2) {
        grid-column: 3;
      }
      .circles>*:nth-child(20n-1) {
        grid-column: 2;
      }
      .circles>*:nth-child(20n) {
        grid-column: 1;
      }
      .circles>*:nth-child(20n-8):last-child::after,
      .circles>*:nth-child(20n-7):last-child::after,
      .circles>*:nth-child(20n-6):last-child::after,
      .circles>*:nth-child(20n-5):last-child::after,
      .circles>*:nth-child(20n-4):last-child::after,
      .circles>*:nth-child(20n-3):last-child::after,
      .circles>*:nth-child(20n-2):last-child::after,
      .circles>*:nth-child(20n-1):last-child::after,
      .circles>*:nth-child(20n):last-child::after {
        display: inline-block;
      }
      .circles>*:nth-child(20n+1)::before {
        display: inline-block;
        left: 50%;
        transform: translate(-50%, -100%);
        width: 1px;
        height: var(--gap);
        top: 0;
        content: '';
        position: absolute;
        background-color: navy;
      }
    }
    
    @media (max-width: 600px) {
      .circles {
        --cols: 5;
      }
      .circles>*:nth-child(10n-5)::after {
        top: 100%;
        left: 50%;
        transform: translateX(-50%);
        height: var(--gap);
        width: 1px;
      }
      .circles>*:nth-child(10n-4) {
        grid-column: 5;
      }
      .circles>*:nth-child(10n-4)::after {
        display: none;
      }
      .circles>*:nth-child(10n-3) {
        grid-column: 4;
      }
      .circles>*:nth-child(10n-2) {
        grid-column: 3;
      }
      .circles>*:nth-child(10n-1) {
        grid-column: 2;
      }
      .circles>*:nth-child(10n) {
        grid-column: 1;
      }
      .circles>*:nth-child(10n-3):last-child::after,
      .circles>*:nth-child(10n-2):last-child::after,
      .circles>*:nth-child(10n-1):last-child::after,
      .circles>*:nth-child(10n):last-child::after {
        display: inline-block;
      }
      .circles>*:nth-child(10n+1)::before {
        display: inline-block;
        left: 50%;
        transform: translate(-50%, -100%);
        width: 1px;
        height: var(--gap);
        top: 0;
        content: '';
        position: absolute;
        background-color: navy;
      }
    }
    
    .circles>*:first-child::before,
    .circles>*:last-child::after {
      display: none;
    }
  </style>
</head>

<body>
  <div class="container">
    <ul class="circles">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
      <li>11</li>
      <li>12</li>
      <li>13</li>
      <li>14</li>
      <li>15</li>
      <li>16</li>
      <li>17</li>
      <li>18</li>
      <li>19</li>
      <li>20</li>
      <li>21</li>
      <li>22</li>
      <li>23</li>
    </ul>
  </div>
</body>

</html>

注意:此答案建立在并部分概括了Draw Circle with connected line responsive 问题的答案,该答案具有更具体的条件,即限制条件。

【讨论】:

  • 感谢您的回答。它工作正常。但是当我们只有 2 行时,最后一个圆圈没有连接。请检查一下,你能纠正一下吗
  • 问题是当有确切数量的完整行时,删除最后一个元素上的 after 伪元素。在 CSS 的末尾添加了对此的测试。现在应该可以正常工作了。
  • 我想你不明白。请删除最后 4 li 元素然后查看问题。Bcz 最后一个圆圈没有连接
  • 我添加了更多的条件来处理最后一个元素在反向行中。不得不说,虽然逻辑并不难,但 CSS 会变得冗长,所以如果它是我的系统,我可能会使用 JS 来创建样式表,而不是手动编写 CSS 代码。但是,对于这两个断点的情况,它是可控的。
  • 谢谢。它的工作正常。我能理解你的感受。如果您发布“如何在 JavaScript 中实现?”,那对我和其他人都会更有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
相关资源
最近更新 更多