绝对定位的 div 在滚动容器内,只相对于未滚动的位置定位。

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

Absolute positioned div inside scroll container is positioned relative only to unscrolled position

问题

I am creating a table of contents sidebar where I want the text to be truncated normally, but expand on hover to show the full text of the list item. The TOC should also be scrollable separate from the main content (e.g., TOC is always visible and the "main" content scrolls on its own).

Using some wrappers and position: absolute I can make the text pop out of the TOC container. However, if the ancestor element of the list item is scrolled at all, then the element with absolute positioning shows up in the location as if the parent was not scrolled.

I tried a combination of different modifications such as adding top: 0 on the element or on the immediate parent, making any/all ancestors either absolute / relative. Resulting in one these outcomes:

  1. Nothing changes
  2. The element which should pop up no longer breaks out / visible x but is in the right position
  3. The element I want to break out goes to the top of the parent container.

I am using set of nested grids, but if there is a different way to create the same containers, I would consider it.

英文:

I am creating a table of contents sidebar where I want the text to be truncated normally, but expand on hover to show the full text of the list item. The TOC should also be scrollable separate from the main content (e.g., TOC is always visible and the "main" content scrolls on its own).

Using some wrappers and position: absolute I can make the text pop out of the TOC container. However, if the ancestor element of the list item is scrolled at all, then the element with absolute positioning shows up in the location as if the parent was not scrolled.

I tried a combination of different modifications such as adding top: 0 on the element or on the immediate parent, making any/all ancestors either absolute / relative. Resulting in one these outcomes:

  1. Nothing changes
  2. The element which should pop up no longer breaks out / visible x but is in the right position
  3. The element I want to break out goes to the top of the parent container.

I am using set of nested grids, but if there is a different way to create the same containers, I would consider it.

Here is a screenshot without scrolling, it accomplishes the goal:
绝对定位的 div 在滚动容器内,只相对于未滚动的位置定位。

However, when the element is scrolled, you can see the absolute position element seems to not take into consideration that the parent scrolled.

绝对定位的 div 在滚动容器内,只相对于未滚动的位置定位。

See code insert below. I created the break out affect on li:hover and on #testcase.

I am currently using the title attribute as a fallback since that will always show ontop, but I cannot style or control the timing, so the breakout effect in the right position would still be better.

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

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

/**reset.css*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}

/** layout.css */

#app {
  margin: 0;
  height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: 
    &#39;header&#39;
    &#39;content&#39;
    &#39;footer&#39;;
}

#content {
  grid-area: content;
  overflow: auto;
}

#content {
  display: grid;
  grid-template-columns: 15vw 1fr;
  grid-template-rows: 1fr; 
  grid-template-rows: 100%; 
  grid-column-gap: 2vw;
}


main {
  max-width: 900px;
  margin-right: 5vw;
}


#toc {

  margin-left: 5px;
  margin-right: 5px;
  align-self: start; 
  height: 100%;


}



#toc{
  display: grid;
  grid-template-rows: auto ;
}

#toc &gt; ul {
  overflow:hidden;
  overflow-y: auto;
  /* height: 100%; */
}



#toc:not(:hover) ul::-webkit-scrollbar{
  visibility: hidden;
}

#toc &gt; ul {

  margin-bottom: 0;
  margin-top:0;

}





#toc h3{
  padding-top: 1em;
  padding-bottom: 1em;
}



header, footer{
  background-color: rgb(241, 248, 255);
}

#toc{
  background-color: rgb(246, 242, 255);
}

#toc ul{
  outline: 1px solid pink;
}

main{
  background-color: rgb(238, 255, 250);
}

/* toc.css */

#toc {
  line-height: 1.3;
}

#toc a::before{
  font-family: &#39;Noto Emoji&#39;, serif;
}

#toc li .toc-item-wrapper {
  display: inline-block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width:100%;
  height:1.3em;
}


/* #toc li:has(a[data-indent-section=&quot;true&quot;])::before{ */
#toc li .toc-item-wrapper:has(a[data-indent-section=&quot;true&quot;])::before{
  content: &#39;↳&#39;;
  margin-left: .3em;
  margin-right: .1em;
}

/***/

#toc li:hover .toc-item-wrapper,
.toc-item-wrapper:has(a#testcase) {
  overflow: visible;
}

#toc li:hover a,
a#testcase
 {
  position:absolute;
  overflow: visible;
  outline: 3px solid #000;
  background-color: var(--jelly-25);
  padding-right:.5em;

}

#toc li{
  height:1.3em;
}

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

&lt;!DOCTYPE html&gt;
&lt;html  lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;title&gt;TEST&lt;/title&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;https://fonts.googleapis.com/css2?family=Noto+Emoji&quot; type=&quot;text/css&quot; media=&quot;all&quot; /&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;css/reset.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; /&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;css/layout.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; /&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;css/toc.css&quot; type=&quot;text/css&quot; media=&quot;all&quot; /&gt;

&lt;/head&gt;

&lt;body id=&quot;app&quot;&gt;
  &lt;header&gt;
    &lt;div&gt;Header, nav, etc.&lt;/div&gt;
  &lt;/header&gt;
  &lt;div id=&quot;content&quot;&gt;


    &lt;aside id=&quot;toc&quot;&gt;
      &lt;h3&gt;Your Progress&lt;/h3&gt;
      &lt;ul&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;AAAAAAA&quot;&gt;AAAAAAA 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a  href=&quot;#&quot;  title=&quot;BBBBBBB&quot;&gt;BBBBBBB 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot;  data-indent-section=&quot;true&quot;  title=&quot;CCCCCCC&quot;&gt;CCCCCCC 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a id=&quot;testcase&quot; href=&quot;#&quot; data-indent-section=&quot;true&quot; title=&quot;DDDDDDD&quot;&gt;DDDDDDD 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;EEEEEEE&quot;&gt;EEEEEEE 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;FFFFFFF&quot;&gt;FFFFFFF 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;GGGGGGG&quot;&gt;GGGGGGG 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;HHHHHHH&quot;&gt;HHHHHHH 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;IIIIIII&quot;&gt;IIIIIII 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;JJJJJJJ&quot;&gt;JJJJJJJ 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;KKKKKKK&quot;&gt;KKKKKKK 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;LLLLLLL&quot;&gt;LLLLLLL 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;MMMMMMM&quot;&gt;MMMMMMM 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;NNNNNNN&quot;&gt;NNNNNNN 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;OOOOOOO&quot;&gt;OOOOOOO 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;        

      &lt;/ul&gt;
    &lt;/aside&gt;
    &lt;main&gt;
      &lt;section&gt;Main Content&lt;/section&gt;
      &lt;section&gt;More Content&lt;/section&gt;
    &lt;/main&gt;
  &lt;/div&gt;
  &lt;footer&gt;Site Footer&lt;/footer&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 3

你可以创建一个自定义的工具提示,它不在带有 overflow: auto;position: fixed; 的块内,并且需要在 onscroll 事件之后更改其位置。

或者你可以使用工具提示库,比如 Tippy.js

例如,像这样的代码:

document.querySelectorAll('.toc-item-wrapper a').forEach(el => {
  tippy(el, {
    content: el.textContent,
    placement: 'top-start',
    arrow: false,
    maxWidth: 'none',
    offset: [0, 0],
    duration: [0, 0],
  });
});
英文:

You can create a custom tooltip that is not in a block with overflow: auto; and has position: fixed;. Then you need to change its position after the onscroll event.

Or you can use a tooltip library such as Tippy.js:

For example, something like this:

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

<!-- language: lang-js -->

document.querySelectorAll(&#39;.toc-item-wrapper a&#39;).forEach(el =&gt; {
  tippy(el, {
    content: el.textContent,
    placement: &#39;top-start&#39;,
    arrow: false,
    maxWidth: &#39;none&#39;,
    offset: [0, 0],
    duration: [0, 0],
  });
});

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

/**reset.css*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}

/** layout.css */

#app {
  margin: 0;
  height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: 
    &#39;header&#39;
    &#39;content&#39;
    &#39;footer&#39;;
}

#content {
  grid-area: content;
  overflow: auto;
}

#content {
  display: grid;
  grid-template-columns: 15vw 1fr;
  grid-template-rows: 1fr; 
  grid-template-rows: 100%; 
  grid-column-gap: 2vw;
}


main {
  max-width: 900px;
  margin-right: 5vw;
}


#toc {

  margin-left: 5px;
  margin-right: 5px;
  align-self: start; 
  height: 100%;


}



#toc{
  display: grid;
  grid-template-rows: auto ;
}

#toc &gt; ul {
  overflow:hidden;
  overflow-y: auto;
  /* height: 100%; */
}



#toc:not(:hover) ul::-webkit-scrollbar{
  visibility: hidden;
}

#toc &gt; ul {

  margin-bottom: 0;
  margin-top:0;

}





#toc h3{
  padding-top: 1em;
  padding-bottom: 1em;
}



header, footer{
  background-color: rgb(241, 248, 255);
}

#toc{
  background-color: rgb(246, 242, 255);
}

#toc ul{
  outline: 1px solid pink;
}

main{
  background-color: rgb(238, 255, 250);
}

/* toc.css */

#toc {
  line-height: 1.3;
}

#toc a::before{
  font-family: &#39;Noto Emoji&#39;, serif;
}

#toc li .toc-item-wrapper {
  display: inline-block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width:100%;
  height:1.3em;
}


/* #toc li:has(a[data-indent-section=&quot;true&quot;])::before{ */
#toc li .toc-item-wrapper:has(a[data-indent-section=&quot;true&quot;])::before{
  content: &#39;↳&#39;;
  margin-left: .3em;
  margin-right: .1em;
}

/***/

#toc li{
  height:1.3em;
}

#toc li a{
	color:blue;
}

/* tippy */

.tippy-box{
	transform:translateY(100%);
	background:none;
	color:inherit;
	outline: 3px solid #000;
	background-color: var(--jelly-25);
	padding:0;
	font:inherit;
}

.tippy-content{
	padding:0;
	background-color:rgb(246, 242, 255);
	line-height:inherit;
	color:blue;
	text-decoration:underline;
}

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

&lt;body id=&quot;app&quot;&gt;
  &lt;header&gt;
    &lt;div&gt;Header, nav, etc.&lt;/div&gt;
  &lt;/header&gt;
  &lt;div id=&quot;content&quot;&gt;


    &lt;aside id=&quot;toc&quot;&gt;
      &lt;h3&gt;Your Progress&lt;/h3&gt;
      &lt;ul&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;AAAAAAA&quot;&gt;AAAAAAA 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a  href=&quot;#&quot;  title=&quot;BBBBBBB&quot;&gt;BBBBBBB 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot;  data-indent-section=&quot;true&quot;  title=&quot;CCCCCCC&quot;&gt;CCCCCCC 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a id=&quot;testcase&quot; href=&quot;#&quot; data-indent-section=&quot;true&quot; title=&quot;DDDDDDD&quot;&gt;DDDDDDD 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;EEEEEEE&quot;&gt;EEEEEEE 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;FFFFFFF&quot;&gt;FFFFFFF 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;GGGGGGG&quot;&gt;GGGGGGG 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;HHHHHHH&quot;&gt;HHHHHHH 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;IIIIIII&quot;&gt;IIIIIII 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;JJJJJJJ&quot;&gt;JJJJJJJ 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;KKKKKKK&quot;&gt;KKKKKKK 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;LLLLLLL&quot;&gt;LLLLLLL 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;MMMMMMM&quot;&gt;MMMMMMM 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;NNNNNNN&quot;&gt;NNNNNNN 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;div class=&quot;toc-item-wrapper&quot;&gt;
            &lt;a href=&quot;#&quot; title=&quot;OOOOOOO&quot;&gt;OOOOOOO 0123456789 0123456789&lt;/a&gt;
          &lt;/div&gt;
        &lt;/li&gt;        

      &lt;/ul&gt;
    &lt;/aside&gt;
    &lt;main&gt;
      &lt;section&gt;Main Content&lt;/section&gt;
      &lt;section&gt;More Content&lt;/section&gt;
    &lt;/main&gt;
  &lt;/div&gt;
  &lt;footer&gt;Site Footer&lt;/footer&gt;
&lt;/body&gt;

&lt;script src=&quot;https://unpkg.com/@popperjs/core@2&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/tippy.js@6&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月20日 03:56:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058359.html
匿名

发表评论

匿名网友

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

确定