0

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.

1 Answers1

2

You can use a class to do that (it's much simpler to do in this way, I think).

const a = document.querySelectorAll('.img-hidden')

console.log(a.length);
#lightgallery {
  overflow: hidden;
  margin-top: 30px;
  height: 122px;
  position: relative; /* important */
}

#lightgallery a {
  width: 176px;
  display: inline-block;
}

#lightgallery img {
  width: 176px;
}

#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" class="img-visible">
    <img src="https://picsum.photos/200/300/?random=1">
  </a>
  <a href="assets/img/light2.jpg" class="img-visible">
    <img src="https://picsum.photos/200/300/?random=2">
  </a>
  <a href="assets/img/light3.jpg" class="img-visible">
    <img src="https://picsum.photos/200/300/?random=3">
  </a>
  <a href="assets/img/light4.jpg" class="img-visible">
    <img src="https://picsum.photos/200/300/?random=4">
  </a>
  <a href="assets/img/light5.jpg" class="img-visible">
    <img src="https://picsum.photos/200/300/?random=5">
  </a>
  <a href="assets/img/light4.jpg" class="img-hidden">
    <img src="https://picsum.photos/200/300/?random=6">
  </a>
  <a href="assets/img/light5.jpg" class="img-hidden">
    <img src="https://picsum.photos/200/300/?random=7">
  </a>
</div>

A different, more programmatic approach can be found on this link. For this, you should explicitly set images' height, which may be not desirable for the given use-case, but does the trick, indeed.

fabrik
  • 14,094
  • 8
  • 55
  • 71