I have a photo gallery that should not display all the photos at the beginning and show how many photos were not included in the gallery (I use
overflow: hidden;
).
After looking at this answer, I decided to do so:
const a = document.querySelectorAll('a')
const count = 0
a.forEach(el => {
if (isHidden(el))
count++
})
console.log('Hidden elemenets: ' + count)
function isHidden (el) {
return el.offsetParent === null
}
#lightgallery {
overflow: hidden;
margin-top: 30px;
height: 122px;
position: relative; /* important */
}
#lightgallery a {
width: 176px;
display: inline-block;
}
#lightgallery img {
width: 100%;
}
#lightgallery a {
color: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lightgallery">
<a href="assets/img/light1.jpg">
<img src="https://picsum.photos/200/300/?random=1">
</a>
<a href="assets/img/light2.jpg">
<img src="https://picsum.photos/200/300/?random=2">
</a>
<a href="assets/img/light3.jpg">
<img src="https://picsum.photos/200/300/?random=3">
</a>
<a href="assets/img/light4.jpg">
<img src="https://picsum.photos/200/300/?random=4">
</a>
<a href="assets/img/light5.jpg">
<img src="https://picsum.photos/200/300/?random=5">
</a>
<a href="assets/img/light4.jpg">
<img src="https://picsum.photos/200/300/?random=6">
</a>
<a href="assets/img/light5.jpg">
<img src="https://picsum.photos/200/300/?random=7">
</a>
</div>
But it does not work.
I need to count photos that are not visible on the screen. I have a responsive design, so the gallery element can have different sizes and accordingly different users can have different number of photos.