admin管理员组文章数量:1406156
I have a full width div with a repeating element. On overflow, I hide the overflown content and don't display whitepace. My problem is I want to count the number of hidden items (or displayed items I guess) but cannot figure out how to.
You can see a basic example of this at
In this case the css governing the hiding is
div {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
white-space: nowrap;
overflow: hidden;
}
If you remove the overflow line you get a scroll bar, put it back in, no scroll bar.
What I need is a way to count the items that are hidden in the lost overflow section, when overflow is set to hidden.
The only things I've found are trying to count which elements have hidden, but that doesn't apply to overflow items. I can't find any differing qualifiers that I can search against.
I have a full width div with a repeating element. On overflow, I hide the overflown content and don't display whitepace. My problem is I want to count the number of hidden items (or displayed items I guess) but cannot figure out how to.
You can see a basic example of this at https://codepen.io/joshuaohana/pen/aqPJMr
In this case the css governing the hiding is
div {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
white-space: nowrap;
overflow: hidden;
}
If you remove the overflow line you get a scroll bar, put it back in, no scroll bar.
What I need is a way to count the items that are hidden in the lost overflow section, when overflow is set to hidden.
The only things I've found are trying to count which elements have hidden, but that doesn't apply to overflow items. I can't find any differing qualifiers that I can search against.
Share Improve this question asked Feb 28, 2018 at 2:29 Joshua OhanaJoshua Ohana 6,14115 gold badges64 silver badges121 bronze badges 2- Can you use jQuery? – sol Commented Feb 28, 2018 at 2:37
- @sol Yes jQuery is fine – Joshua Ohana Commented Feb 28, 2018 at 2:39
1 Answer
Reset to default 6This is an approach with jQuery. Detect the width of the containing div
. Then use the offset()
method to count the number of elements who have a left
coordinate outside of this width.
var parentContainerWidth = $("div").width();
var elementCount = $('span').filter(function() {
return $(this).offset().left >= parentContainerWidth;
}).length;
alert(elementCount);
div {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
white-space: nowrap;
overflow: hidden;
}
span {
background: pink;
margin: 2px;
padding: 1rem;
font-family: sans-serif;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span>here is an item 1</span>
<span>here is an item 2</span>
<span>here is an item 3</span>
<span>here is an item 4</span>
<span>here is an item 5</span>
<span>here is an item 6</span>
<span>here is an item 7</span>
<span>here is an item 8</span>
<span>here is an item 9</span>
<span>here is an item 10</span>
<span>here is an item 11</span>
<span>here is an item 12</span>
<span>here is an item 13</span>
<span>here is an item 14</span>
<span>here is an item 15</span>
<span>here is an item 16</span>
<span>here is an item 17</span>
<span>here is an item 18</span>
<span>here is an item 19</span>
<span>here is an item 20</span>
<span>here is an item 21</span>
<span>here is an item 22</span>
<span>here is an item 23</span>
<span>here is an item 24</span>
</div>
本文标签: javascriptCount number of hidden elements by overflow hiddenStack Overflow
版权声明:本文标题:javascript - Count number of hidden elements by overflow hidden - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744385136a2603702.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论