如何找到容器外的第一个列表项的位置?

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

How to find the position of the first list item outside a container?

问题

我有一个div容器,在它里面有一个项目列表。列表比容器的高度要大,而容器设置了overflow:hidden。我如何找到第一个在容器中没有显示的列表项的位置?

英文:

I have a div container and inside it a list of items. The list is bigger than container's height and the container has overflow:hidden. How can I find the position of the first list item that isn't displayed by the container?

答案1

得分: 1

你可以使用 getBoundingClientRect() 来比较元素的边界与容器底部位置。

唯一的问题是水平滚动条,但这可能是 Web UI 中很少见的特殊情况。

const container = document.querySelector('.container');

const borderWidth = parseInt(getComputedStyle(container).borderBottomWidth);
const containerBottom = container.getBoundingClientRect().bottom - borderWidth;

let hiddenPartial;

container.addEventListener('scroll', checkHidden);
checkHidden();

function checkHidden() {

  hiddenPartial?.classList.remove('partial');

  hiddenPartial = [...container.children].find(elem => {
    const {top, bottom } = elem.getBoundingClientRect();
    return top < containerBottom && bottom > containerBottom; 
  });
  
  hiddenPartial?.classList.add('partial');

  $partial.textContent = 'The first child hidden partially: ' + hiddenPartial?.textContent;

  const hiddenFull = [...container.children].find(elem => elem.getBoundingClientRect().top > containerBottom);

  $full.textContent = 'The first child hidden fully: ' + hiddenFull?.textContent;

};
.container {
  border: 15px solid gray;
  height:70px;
  overflow:auto;
  padding: 20px;
  border-radius: 4px;
}
p{
  border: 1px solid lightgray;
  border-radius: 4px;
  padding: 5px 10px;
}

.partial{
  background: yellow;
  border-color:red;
}
<div class="container">
  <p>item 1</p>
  <p>item 2</p>
  <p>item 3</p>
  <p>item 4</p>
  <p>item 5</p>
  <p>item 6</p>
</div>
<div id="$partial"></div>
<div id="$full"></div>
英文:

You could use getBoundingClientRect() to compare elements' bounds with the container's bottom position.

The only problem is the horizontal scrollbar, but that could be a very special case which is rare in web UI.

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

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

const container = document.querySelector(&#39;.container&#39;);

const borderWidth = parseInt(getComputedStyle(container).borderBottomWidth);
const containerBottom = container.getBoundingClientRect().bottom - borderWidth;

let hiddenPartial;

container.addEventListener(&#39;scroll&#39;, checkHidden);
checkHidden();

function checkHidden() {

  hiddenPartial?.classList.remove(&#39;partial&#39;);

  hiddenPartial = [...container.children].find(elem =&gt; {
    const {top, bottom } = elem.getBoundingClientRect();
    return top &lt; containerBottom &amp;&amp; bottom &gt; containerBottom; 
  });
  
  hiddenPartial?.classList.add(&#39;partial&#39;);

  $partial.textContent = &#39;The first child hidden partially: &#39; + hiddenPartial?.textContent;

  const hiddenFull = [...container.children].find(elem =&gt; elem.getBoundingClientRect().top &gt; containerBottom);

  $full.textContent = &#39;The first child hidden fully: &#39; + hiddenFull?.textContent;

};

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

.container {
  border: 15px solid gray;
  height:70px;
  overflow:auto;
  padding: 20px;
  border-radius: 4px;
}
p{
  border: 1px solid lightgray;
  border-radius: 4px;
  padding: 5px 10px;
}

.partial{
  background: yellow;
  border-color:red;
}

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

&lt;div class=&quot;container&quot;&gt;
&lt;p&gt;item 1&lt;/p&gt;
&lt;p&gt;item 2&lt;/p&gt;
&lt;p&gt;item 3&lt;/p&gt;
&lt;p&gt;item 4&lt;/p&gt;
&lt;p&gt;item 5&lt;/p&gt;
&lt;p&gt;item 6&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&quot;$partial&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;$full&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定