使虚线在保持展开和折叠功能完整的同时变得动态。

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

Make the dotted lines dynamic while having expand-collapse intact

问题

以下是您提供的内容的翻译部分:

如何在保持其他功能(单击set1或set2的展开折叠按钮)完整的同时使虚线动态化。不希望虚线位于数据下方。因此,它会像数据之间的连接器一样开始和结束。演示示例:https://jsfiddle.net/vcjzn5hs/1/

感谢您的建议。

我能够使动态虚线和展开-折叠功能分别工作,但不能同时工作。供您参考:动态虚线应该如何工作的示例http://jsfiddle.net/bwpugnd3/11/。但是这个示例无法与展开折叠按钮一起使用。

代码片段:

<!-- 开始片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-css -->
.flex {
  display: flex;
  justify-content: space-between;
}

.lev1,
.lev2,
.lev3 {
  position: relative;
  margin-top: 5px;
  margin-left: 15px;
}

.lev1::after,
.lev2::after,
.lev3::after {
  content: '';
  position: absolute;
  width: 80%;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin: 0 6px;
  height: 2px;
  color: lightgrey;
  margin-bottom: 4px;
  background-image: linear-gradient(to right, currentColor 2px, transparent 1px);
  background-size: 4px 2px;
}

.DataRightAlign {
  word-wrap: break-word;
  white-space: break-spaces;
  margin-right: 20px;
  margin-left: 10px;
  color: black;
}

<!-- 语言: lang-html -->
<!--jquery and bootstrap-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css">
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="itemDiv">
  <div>
    <a class="accordion-button " data-toggle="collapse" data-target=".lev1"> Set1 </a>
    <div class="collapse multi-collapse lev1 show">
      <p class="flex">
        <span>item1data</span>
        <span class="DataRightAlign"> Set1val here</span>
      </p>
    </div>
    <div class="collapse multi-collapse lev1 show">
      <p class="flex">
        <span>item2</span>
        <span class="DataRightAlign">Set1</span>
      </p>
    </div>
  </div>

  <div>
    <a class="accordion-button " data-toggle="collapse" data-target=".lev3">Set2 </a>
    <div class="collapse multi-collapse lev3 show">
      <p class="flex">
        <span>item3</span>
        <span class="DataRightAlign">Set2val</span>
      </p>
    </div>
  </div>
</div>

<!-- 结束片段 -->

希望这能帮助您理解代码和问题的翻译。如果有其他问题,请随时提出。

英文:

How to make the dotted lines dynamic while having other functionalities (expand- collapse button on click of set1 or set2) intact. Do not want dotted line to go underneath data. So that it starts and ends like a connector between data. Fiddle showing the same: https://jsfiddle.net/vcjzn5hs/1/

Would appreciate your suggestion.

I am able to make dynamic dotted lines and expand-collapse work independently but not together. For your reference: Sample of how dynamic dotted lines should be http://jsfiddle.net/bwpugnd3/11/. But this example does not work along with expand collapse button.

Code snippet:

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

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

.flex {
display: flex;
justify-content: space-between;
}
.lev1,
.lev2,
.lev3 {
position: relative;
margin-top: 5px;
margin-left: 15px;
}
.lev1::after,
.lev2::after,
.lev3::after {
content: &#39;&#39;;
position: absolute;
width: 80%;
top: 100%;
left: 50%;
transform: translateX(-50%);
margin: 0 6px;
height: 2px;
color: lightgrey;
margin-bottom: 4px;
background-image: linear-gradient(to right, currentColor 2px, transparent 1px);
background-size: 4px 2px;
}
.DataRightAlign {
word-wrap: break-word;
white-space: break-spaces;
margin-right: 20px;
margin-left: 10px;
color: black;
}

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

&lt;!--jquery and bootstrap--&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css&quot;&gt;
&lt;link href=&quot;css/bootstrap/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;itemDiv&quot;&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev1&quot;&gt; Set1 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item1data&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt; Set1val here&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item2&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set1&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev3&quot;&gt;Set2 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev3 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item3&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set2val&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

一种方法,也是最简单的方法,如下所示,代码中附有解释性注释;请注意,我从您的演示中删除了所有 CSS,因为这样做会使创建演示变得稍微容易一些,但当然,您可以添加自己的 CSS 回去:

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->

/* 一个简单的CSS重置,以确保浏览器使用相同的大小调整算法来处理元素大小;
   包括在声明的大小内部和填充内部,还删除了默认的边距和填充: */
body {
  block-size: 100vh;
  padding-block: 1rem;
  padding-inline: 1.5rem;
}

.itemDiv {
  /* 设置元素的内联大小;内联大小是内联内容(如 <span> 元素)布局的轴,
     与块轴垂直。在英语中,内联轴是从左到右的水平轴。clamp() 函数
     将元素的首选大小设置为 90%,最小为 30rem,最大为 1100px: */
  inline-size: clamp(30rem, 90%, 1100px);
  /* 在内联轴上使元素居中: */
  margin-inline: auto;
}

.flex {
  display: flex;
  gap: 0.5rem;
  /* 将元素间隔开: */
  justify-content: space-between;
}

/* 使用第二个 span(在 HTML 中标记)来包含行前导: */
.flex > span:nth-child(2) {
  background-image:
    /* 使用重复线性渐变作为背景图像: */
    repeating-linear-gradient(
      /* 90度使渐变横向运行,从左边开始: */
      90deg,
      /* currentColor 是元素当前分配的颜色,这个范围是从 0 到 2px: */
      currentColor 0 2px,
      /* 然后切换到一个透明的“颜色”,它从 2px 开始,并在 5px 处结束: */
      transparent 2px 5px
    );
  /* 将重复线性渐变() 定位在内联轴的 0 位置,
     并在块轴上的 100% 减去 5px 的位置;5px 是背景图像的点大约对齐
     邻近元素文本的基线的魔术数: */
  background-position: 0 calc(100% - 5px);
  /* 防止背景重复: */
  background-repeat: no-repeat;
  /* 设置背景图像的大小为宽度/内联轴的 100%,高度/块轴的 2px: */
  background-size: 100% 2px;
  /* 这会使元素扩展以填充所有可用空间: */
  flex-grow: 1;
}

.accordion-button {
  /* 如果一个元素可以被点击,并且是交互式的,我倾向于分配以下属性和属性值来指示这种交互性: */
  cursor: pointer;
}

<!-- 语言: lang-html -->

<!--jquery 和 bootstrap-->

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css">
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>


<!-- HTML -->

<div class="itemDiv">

  <div>
    <a class="accordion-button " data-toggle="collapse" data-target=".lev1"> Set1 </a>
    <div class="collapse multi-collapse lev1 show">
      <p class="flex">
        <span>item1data</span>
        <!-- 我感到内疚,但最简单的方法是添加一个元素来容纳
             行前导: -->
        <span></span>
        <span class="DataRightAlign"> Set1val here</span>
      </p>
    </div>
    <div class="collapse multi-collapse lev1 show">
      <p class="flex">
        <span>item2</span>
        <span></span>
        <span class="DataRightAlign">Set1</span>
      </p>
    </div>
  </div>


  <div>
    <a class="accordion-button " data-toggle="collapse" data-target=".lev3">Set2 </a>
    <div class="collapse multi-collapse lev3 show">
      <p class="flex">
        <span>item3</span>
        <span></span>
        <span class="DataRightAlign">Set2val</span>
      </p>
    </div>
  </div>

</div>

<!-- 结束代码段 -->

JS Fiddle 示例

英文:

One approach, and frankly the easiest, is below with explanatory comments in the code; please note that I removed all CSS from your own demo because it made it slightly easier to create the demo, but &ndash; of course &ndash; there's nothing to stop you adding your own CSS back:

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

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

/* a simple CSS reset to ensure the browser uses the same sizing
algorithm for element sizes; including the width and padding
within the declared sizes, also removing default margins
and padding: */
body {
block-size: 100vh;
padding-block: 1rem;
padding-inline: 1.5rem;
}
.itemDiv {
/* setting the inline-size of the element; inline-size is the axis
on which inline-content (such as &lt;span&gt; elements) is laid out,
and is perpendicular to the block-axis. In English the inline-axis
is the horizontal axis running left-to-right. The clamp() function
sets the preferred size of the element to 90%, with a 30rem
minimum-size and 1100px maximum size: */
inline-size: clamp(30rem, 90%, 1100px);
/* centering the element on the inline-axis: */
margin-inline: auto;
}
.flex {
display: flex;
gap: 0.5rem;
/* spacing the elements out: */
justify-content: space-between;
}
/* using the second span (noted in the HTML) to contain the line leading: */
.flex &gt; span:nth-child(2) {
background-image:
/* using a repeating-linear-gradient() as the background-image: */
repeating-linear-gradient(
/* 90degrees causes the gradient to run horizontally, from an
origin on the left side: */
90deg,
/* currentColor is the currently-assigned color of the element, this
runs from 0 to 2px: */
currentColor 0 2px,
/* and we switch to a transparent &quot;color&quot; which starts at 2px and
runs for 3px until a point at 5px: */
transparent 2px 5px
);
/* positioning the repeating-linear-gradient() at 0 on the inline axis,
and at 100% minus 5px on the block-axis; 5px is something of a magic
number to line the dots of the background image to the approximate
baseline of the text in the adjacent elements: */
background-position: 0 calc(100% - 5px);
/* preventing the background from repeating: */
background-repeat: no-repeat;
/* setting the size of the background-image to be 100% of the width/inline-axis,
and 2px on the vertical/block-axis: */
background-size: 100% 2px;
/* this causes the element to grow to fill all available space: */
flex-grow: 1;
}
.accordion-button {
/* if an element can be clicked, and is interactive, I tend to assign the
following property and property-value to indicate that interactivity: */
cursor: pointer;
}

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

&lt;!--jquery and bootstrap--&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css&quot;&gt;
&lt;link href=&quot;css/bootstrap/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;!--html --&gt;
&lt;div class=&quot;itemDiv&quot;&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev1&quot;&gt; Set1 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item1data&lt;/span&gt;
&lt;!-- I feel bad about it, but the easiest way is to add an element to hold
the leading: --&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt; Set1val here&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item2&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set1&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev3&quot;&gt;Set2 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev3 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item3&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set2val&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

JS Fiddle demo.

An alternative is to use a CSS generated content to hold the leading (the dots that lie between the item-data and set-value), this has problems though; again, explanatory comments are in the code:

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

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

/* a simple CSS reset to ensure the browser uses the same sizing
algorithm for element sizes; including the width and padding
within the declared sizes, also removing default margins
and padding: */
body {
block-size: 100vh;
padding-block: 1rem;
padding-inline: 1.5rem;
}
.itemDiv {
/* setting the inline-size of the element; inline-size is the axis
on which inline-content (such as &lt;span&gt; elements) is laid out,
and is perpendicular to the block-axis. In English the inline-axis
is the horizontal axis running left-to-right. The clamp() function
sets the preferred size of the element to 90%, with a 30rem
minimum-size and 1100px maximum size: */
inline-size: clamp(30rem, 90%, 1100px);
/* centering the element on the inline-axis: */
margin-inline: auto;
}
.flex {
display: flex;
gap: 0.5rem;
/* spacing the elements out: */
justify-content: space-between;
}
.flex &gt; span:first-child {
/* here we use display: contents, which emulates &quot;unwrapping&quot; of the content
from the matched element, and placing it directly into its parent-element;
this means that both the &lt;span&gt; content and the ::after pseudo-element can
take part in the flex layout, acting as siblings: */
display: contents;
}
/* using the second span (noted in the HTML) to contain the line leading: */
.flex &gt; span:first-child::after {
background-image:
/* using a repeating-linear-gradient() as the background-image: */
repeating-linear-gradient(
/* 90degrees causes the gradient to run horizontally, from an
origin on the left side: */
90deg,
/* currentColor is the currently-assigned color of the element, this
runs from 0 to 2px: */
currentColor 0 2px,
/* and we switch to a transparent &quot;color&quot; which starts at 2px and
runs for 3px until a point at 5px: */
transparent 2px 5px
);
/* positioning the repeating-linear-gradient() at 0 on the inline axis,
and at 100% minus 5px on the block-axis; 5px is something of a magic
number to line the dots of the background image to the approximate
baseline of the text in the adjacent elements: */
background-position: 0 calc(100% - 5px);
/* preventing the background from repeating: */
background-repeat: no-repeat;
/* setting the size of the background-image to be 100% of the width/inline-axis,
and 2px on the vertical/block-axis: */
background-size: 100% 2px;
content: &#39;&#39;;
/* this causes the element to grow to fill all available space: */
flex-grow: 1;
}
.accordion-button {
/* if an element can be clicked, and is interactive, I tend to assign the
following property and property-value to indicate that interactivity: */
cursor: pointer;
}

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

&lt;!--jquery and bootstrap--&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css&quot;&gt;
&lt;link href=&quot;css/bootstrap/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;!--html --&gt;
&lt;div class=&quot;itemDiv&quot;&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev1&quot;&gt; Set1 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item1data&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt; Set1val here&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item2&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set1&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev3&quot;&gt;Set2 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev3 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item3&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set2val&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

JS Fiddle demo.

Unfortunately, the use of:

.flex &gt; span:first-child {
display: contents;
}

means that we can no longer style the element with background-color, or border and so on; this is an unavoidable consequence of this approach.

Finally, though, and thought of rather later than I'd like, we have the option of nesting flex-layouts as follows:

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

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

/* a simple CSS reset to ensure the browser uses the same sizing
algorithm for element sizes; including the width and padding
within the declared sizes, also removing default margins
and padding: */
body {
block-size: 100vh;
padding-block: 1rem;
padding-inline: 1.5rem;
}
.itemDiv {
/* setting the inline-size of the element; inline-size is the axis
on which inline-content (such as &lt;span&gt; elements) is laid out,
and is perpendicular to the block-axis. In English the inline-axis
is the horizontal axis running left-to-right. The clamp() function
sets the preferred size of the element to 90%, with a 30rem
minimum-size and 1100px maximum size: */
inline-size: clamp(30rem, 90%, 1100px);
/* centering the element on the inline-axis: */
margin-inline: auto;
}
.flex {
display: flex;
gap: 0.5rem;
/* spacing the elements out: */
justify-content: space-between;
}
.flex &gt; span:first-child {
/* we assign display: flex to the element, this causes it to lay its
own children out according to the flex layout properties: */
display: flex;
/* this causes the element to grow to fill all available space within
its parent: */
flex-grow: 1;
/* we inherit the gap property from the parent, so that the gaps here
are the same as the gaps between this element and its sibling(s): */
gap: inherit;
}
.flex &gt; span:first-child::after {
/* again, using the background image to create the leading: */
background-image:
/* using a repeating-linear-gradient() as the background-image: */
repeating-linear-gradient(
/* 90degrees causes the gradient to run horizontally, from an
origin on the left side: */
90deg,
/* currentColor is the currently-assigned color of the element, this
runs from 0 to 2px: */
currentColor 0 2px,
/* and we switch to a transparent &quot;color&quot; which starts at 2px and
runs for 3px until a point at 5px: */
transparent 2px 5px
);
/* positioning the repeating-linear-gradient() at 0 on the inline axis,
and at 100% minus 5px on the block-axis; 5px is something of a magic
number to line the dots of the background image to the approximate
baseline of the text in the adjacent elements: */
background-position: 0 calc(100% - 5px);
/* preventing the background from repeating: */
background-repeat: no-repeat;
/* setting the size of the background-image to be 100% of the width/inline-axis,
and 2px on the vertical/block-axis: */
background-size: 100% 2px;
content: &#39;&#39;;
/* as with the parent element, we use flex-grow to allow the pseudo-element
to grow to fill the available space: */
flex-grow: 1;
}
.accordion-button {
/* if an element can be clicked, and is interactive, I tend to assign the
following property and property-value to indicate that interactivity: */
cursor: pointer;
}

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

&lt;!--jquery and bootstrap--&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css&quot;&gt;
&lt;link href=&quot;css/bootstrap/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;!--html --&gt;
&lt;div class=&quot;itemDiv&quot;&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev1&quot;&gt; Set1 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item1data&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt; Set1val here&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;collapse multi-collapse lev1 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item2&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set1&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;a class=&quot;accordion-button &quot; data-toggle=&quot;collapse&quot; data-target=&quot;.lev3&quot;&gt;Set2 &lt;/a&gt;
&lt;div class=&quot;collapse multi-collapse lev3 show&quot;&gt;
&lt;p class=&quot;flex&quot;&gt;
&lt;span&gt;item3&lt;/span&gt;
&lt;span class=&quot;DataRightAlign&quot;&gt;Set2val&lt;/span&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

JS Fiddle demo.

References:

答案2

得分: 1

以下是要翻译的内容:

"这类似于目录的CSS样式设置

基本思想是将content: &#39;...设置得非常长,并将溢出隐藏,将左侧文本和右侧文本的背景设置为白色,这样在右侧文本或左侧文本之后或下方就不会看到这些点,但实际上这条虚线是为整行绘制的

height: 40pxtop: -42px修复了虚线稍微移动到左侧文本和右侧文本之下的问题

z-index: -1;将虚线隐藏在右侧文本和左侧文本之后


.toc {
overflow: hidden;
height: 40px;
}

.toc::after {
content: '................................................................................................................................................................';
overflow: hidden;
position: relative;
display: block;
left: 0;
top: -42px;
z-index: -1;
}
.toc-title {
background-color: #fff;
padding-right: 1%;
}
.toc-section {
background-color: #fff;
padding-left: 1%;
float: right;
}




单独折叠

第1章:某些第1章

某些标题1
部分1

某些标题2
部分2

某些标题3
部分3

第2章:某些第2章

某些标题1
部分1

某些标题2
部分2

某些标题3
部分3

两个折叠

第1章:某些第1章

某些标题1
部分1

某些标题2
部分2

某些标题3
部分3

第2章:某些第2章

某些标题1
部分1

某些标题2
部分2

某些标题3
部分3

英文:

This is something like table of contents CSS styling

The basic idea is put content: &#39;... very lengthy and make overflow hidden and make the div background of the left side text and right side text white, so those dots will not visible after or under the right side text or left side text, but actually the dotted line drawn for the entire row

the height: 40px and top: -42px fixes the dotted line move slightly below the the left side text and right side text

the z-index: -1; make the dotted line hide behind the right side text and left side text

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

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

.toc {
overflow: hidden;
height: 40px;
}

.toc::after {
content: '................................................................................................................................................................';
overflow: hidden;
position: relative;
display: block;
left: 0;
top: -42px;
z-index: -1;
}
.toc-title {
background-color: #fff;
padding-right: 1%;
}
.toc-section {
background-color: #fff;
padding-left: 1%;
float: right;
}

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

&lt;!--jquery and bootstrap--&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css&quot;&gt;
&lt;link href=&quot;css/bootstrap/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.2.1.slim.min.js&quot; integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js&quot; integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<h1> Individual accordion collapse </h1>
<br/>
<div style="line-height: 40px">
<h4 style="font-weight: 700" class="accordion-button" data-toggle="collapse" data-target=".lev1">Chapter 1: Some Chapter 1</h4>
<div style="font-size: 25px;" class="collapse multi-collapse lev1 show">
<div>
<div class="toc">
<span class="toc-title">Some Title 1</span>
<span class="toc-section">Section 1</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 2</span>
<span class="toc-section">Section 2</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 3</span>
<span class="toc-section">Section 3</span>
</div>
</div>
</div>
<h4 style="font-weight: 700" class="accordion-button" data-toggle="collapse" data-target=".lev2">Chapter 2: Some Chapter 2</h4>
<div style="font-size: 25px;" class="collapse multi-collapse lev2 show">
<div>
<div class="toc">
<span class="toc-title">Some Title 1</span>
<span class="toc-section">Section 1</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 2</span>
<span class="toc-section">Section 2</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 3</span>
<span class="toc-section">Section 3</span>
</div>
</div>
</div>
</div>
<br/>
<h1> Both accordion collapse </h1>
<br/>
<div style="line-height: 40px">
<h4 style="font-weight: 700" class="accordion-button" data-toggle="collapse" data-target=".lev3">Chapter 1: Some Chapter 1</h4>
<div style="font-size: 25px;" class="collapse multi-collapse lev3 show">
<div>
<div class="toc">
<span class="toc-title">Some Title 1</span>
<span class="toc-section">Section 1</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 2</span>
<span class="toc-section">Section 2</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 3</span>
<span class="toc-section">Section 3</span>
</div>
</div>
</div>
<h4 style="font-weight: 700" class="accordion-button" data-toggle="collapse" data-target=".lev3">Chapter 2: Some Chapter 2</h4>
<div style="font-size: 25px;" class="collapse multi-collapse lev3 show">
<div>
<div class="toc">
<span class="toc-title">Some Title 1</span>
<span class="toc-section">Section 1</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 2</span>
<span class="toc-section">Section 2</span>
</div>
</div>
<div>
<div class="toc">
<span class="toc-title">Some Title 3</span>
<span class="toc-section">Section 3</span>
</div>
</div>
</div>
</div>

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月7日 21:18:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194171.html
匿名

发表评论

匿名网友

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

确定