Discover the concealed by using overflow: hidden on the parent's children

I am facing an issue with my photo gallery where I want to hide some photos initially and display how many are omitted from the gallery using overflow: hidden;.

After reading through this particular response, I attempted the following solution:

const a = document.querySelectorAll('a')
const count = 0

a.forEach(el => {
  if (isHidden(el))
    count++
})

console.log('Hidden elements: ' + 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>

Unfortunately, the above approach does not seem to be effective.

In order to address this, I need a method to calculate the number of photos that are currently not visible on the screen due to the responsive design of the gallery element leading to varying sizes for different users.

Answer №1

To achieve this, you can utilize a class (I believe it's a more straightforward approach).

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

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

#gallery img {
  width: 176px;
}

#gallery a {
  color: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gallery">
  <a href="assets/img/image1.jpg" class="visible-item">
    <img src="https://picsum.photos/200/300/?random=1">
  </a>
  <a href="assets/img/image2.jpg" class="visible-item">
    <img src="https://picsum.photos/200/300/?random=2">
  </a>
  <a href="assets/img/image3.jpg" class="visible-item">
    <img src="https://picsum.photos/200/300/?random=3">
  </a>
  <a href="assets/img/image4.jpg" class="visible-item">
    <img src="https://picsum.photos/200/300/?random=4">
  </a>
  <a href="assets/img/image5.jpg" class="visible-item">
    <img src="https://picsum.photos/200/300/?random=5">
  </a>
  <a href="assets/img/image4.jpg" class="hidden-item">
    <img src="https://picsum.photos/200/300/?random=6">
  </a>
  <a href="assets/img/image5.jpg" class="hidden-item">
    <img src="https://picsum.photos/200/300/?random=7">
  </a>
</div>

You may also explore a different, more systematic methodology on this resource. However, keep in mind that setting explicit heights for the images may be necessary but effective.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Storing information using ajax and json technologies

I've inherited a project where I need to work on saving data to the database using an input. The function responsible for this (SaveData) is not functioning properly. Since I'm not very familiar with ajax and json, can someone please help me iden ...

Is it possible for the `position: fixed` property to interfere with mix-blend-mode, and if so, are there

Struggling to make box-shadows cooperate with different backgrounds? The traditional method involves using mix-blend-mode and adding a pseudo element behind the main one for the effect. You can see an example of this technique here (click the + icon in th ...

The select2 jQuery plugin is malfunctioning following an AJAX request to update the HTML content

I have implemented an AJAX function to change the content of a HTML div. Within this changed content, there is a select box with the 'select2' jQuery class. However, after the AJAX load, the select2 jQuery plugin does not work properly. Here is ...

When capturing photos using h5 on iOS devices, the browser may experience a flash back issue

I have been searching Baidu and Google for a while now, but I still haven't found a solution. Here is the specific issue: When using h5 to take photos on iOS systems, the browser flashes back. HTML Code <img src="xxx" onclick="sta ...

Displaying Google Picker results in an error due to an invalid origin value

After utilizing the same code for a period of 2 years, we are encountering an issue where the picker no longer displays. I have consulted resources on Google Picker API Invalid origin but haven't been able to successfully set the origin value. The cod ...

Tips on arranging my Bachelor's in Computer Science degree certificate and academic awards at the bottom

I am trying to make the BS-CS and stars section stick to the bottom of my container, but I am facing difficulties in achieving that. Can someone help me with this? Here is my Bootstrap code: <div className="item1 col-12 col-md-6 col-lg-4 col-xl-4 my- ...

Error in Javascript when attempting to embed Redbubble

Hello, I am currently working on embedding the redbubble code into a Joomla site for a client who has a Redbubble store. However, I seem to be encountering some issues with the script. <script type="text/javascript" src="http://www.redbubble.com/asse ...

Express app encountering difficulty retrieving hidden input values from HTML sources

I am currently attempting to retrieve hidden form values in the post route of my express application. However, I am encountering undefined objects in the post route. Below is a snippet of the code that illustrates this issue: const express = require("ex ...

Improving JSON Structure in a New Post

When attempting to send the id and quantity as integers, I encountered an issue where the id was being posted as a string instead. Despite defining them as integers in my code, this problem persisted. I know I must be missing something simple here, but I ...

What could be the reason behind my code functioning properly on CodePen but not in my editor?

I'm currently using Sublime 3 for coding in HTML and CSS. After saving the files with the proper extensions, I included the Google Fonts to use the "Lobster" font. However, upon opening the .html file on Google Chrome, the Lobster font doesn't d ...

What is the best way to hide the div when scrolling to the top?

Is there a way to hide the circle once it's scrolled to the top? Currently, I can scroll the circle to the top but it remains visible. Upon further exploration, I found that clicking on the circle scrolls it to the top and then on a second click, it f ...

Error in VUE: Module 'lru-cache' not located

Greetings, I would like to start by apologizing for any language errors in my communication. I am currently facing an issue while trying to set up a project to work on, following these steps. STEP 1: npm install @vue/cli STEP 2: vue create myproject En ...

Strange CSS problem occurring specifically on Firefox and Opera browsers

I have set up sharing buttons on my website, but they appear distorted on Firefox and Opera browsers. Interestingly, they render perfectly on Chrome and even Internet Explorer. Here is a preview of how it currently looks: If you are using Firefox or Opera ...

Combining Bootstrap columns in a vertical orientation

I am looking to vertically stack two columns in Bootstrap. Below is the code snippet that I have written: <div class="container"> <div class="row"> <div class="col-md-6"> </div> ...

Ways to extract the ASP.net variable value and place it inside a JavaScript textbox

Currently, I'm in the process of developing Javascript code for an ASP.net page. Within my coding framework, the string "foo" is linked to a string variable called myString. In order to transfer the value of myString to a JavaScript variable, I incl ...

Troubleshooting Typescript compilation using tsc with a locally linked node package

In our project using React/React Native with Typescript, we have a mobile app built with React Native and a shared private package that is utilized by both the React Native client and the React frontend. Due to frequent changes in the shared package, we a ...

Implementing a search filter for special characters in AngularJS

Looking to filter an array of players by their names, but facing a challenge with special characters in the names. Found a code snippet on Google that looks like this: $scope.modelFilterNormalized = function(){ if($scope.modelFilter) return $sco ...

Does running npm install automatically compile the library code as well?

I have a query regarding npm and its functionality. I also posted the same question on Reddit, but haven't received a satisfying answer yet. Let's use the jQuery npm package as a case study. Upon running the command npm install jquery, I notic ...

Internet Explorer is failing to render images according to the CSS file

I am currently working on a web application, and I have encountered an issue with my CSS file. body.login { background: url('abc.jpg') } body.loginSuccess { background: url('xyz.jpg') } When testing in Chrome, everything appea ...

Error: jsonFlickrFeed has not been declared

My request is as follows: const flickrApiPoint = "https://api.flickr.com/services/feeds/photos_public.gne"; try { $.ajax({ url: flickrApiPoint, dataType: 'jsonp', data: { "format": "json" }, success: function (data) { c ...