Tips for adjusting the height of an outer div based on the visible inner div heights while scrolling

Here is an example of HTML code with a simple array of integers called Files: [1,2,3,4,5,6]

<!doctype html>
<html ng-app="Application">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>

    <script>
      var app = angular.module('Application', []);
      var applicationCtrl = function ($scope) {
          $scope.files = [1,2,3,4,5,6];
          $scope.showAll = false;
       };

      app.controller('vm', applicationCtrl);
    </script>
    <style>
        .J1 {
            overflow-y: scroll;
            height: 70px;
        }
    </style>
</head>
<body ng-controller="vm">
    <div style="width: 100px;" ng-class="{'J1': (files.length > 3) && !showAll}">
        <div ng-repeat="file in files" style="border-width: 1px; border-style: solid;">
            <span>{{file}}</span>
            <input type="button" value="{{'btn' + file}}"></input>
        </div>
    </div>
    <br />
    <div>
    <a href ng-click="showAll = !showAll;">Show {{ showAll ? 'less':'more'}}</a>
    </div>
</body>
</html>

If the length of the files array exceeds 3, it applies the CSS overflow property to the div element while also setting a fixed height value. To dynamically adjust the height of the div based on the heights of the first 3 divs without a fixed value, you can achieve this by implementing a solution using AngularJS.

Answer №1

Feel free to give this a shot. I've included the jquery CDN here, although you could achieve the same outcome using raw javascript.

<!doctype html>
<html ng-app="Application">
<head>
    <title></title>
   <script src="https://code.jquery.com/jquery-2.2.3.min.js"   integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="   crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
    <script>
  var app = angular.module('Application', []);
  var applicationCtrl = function ($scope) {
      $scope.files = [1,2,3,4,5,6];
     
     /* Adjust the height of the parent div */
      $(function(){
          $('.J1').height($('.each-button').outerHeight() * 3);
      });
    
   };

  app.controller('vm', applicationCtrl);
</script>
    <style>
        .J1 {
            overflow-y: scroll;
        }
    </style>
</head>
<body ng-controller="vm">
    <div style="width: 100px;" class="J1">
      <!-- a new class has been introduced -->       
      <div class="each-button" ng-repeat="file in files" style="border-width: 1px; border-style: solid;">
            <span>{{file}}</span>
            <input type="button" value="{{'btn' + file}}"></input>
        </div>
    </div>
    <br />
    <div>
        <a href>Show more</a>
    </div>
</body>
</html>

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

Questions on Ionic 2 starter tutorials

Hey there, I'm a beginner when it comes to using Ionic 2 and I have some doubts about the code in my .html file within my (page). <ion-content padding class="getting-started"> //Question 1 <h3>Hello World!</h3> <p> ...

Extract information from the webpage

Having my own external website, I am looking to extract data from it. Initially, I used CURL to retrieve the website's content but now I specifically need to extract the TimeStamp of a Facebook page. By inspecting the element on the page, you can find ...

What could be causing my bootstrap dropdown button to malfunction?

Even after selecting an option from the dropdown menu and pressing the button, the value does not change. I attempted to console log it using JavaScript but encountered an error in the Chrome inspector: index.js:26 Uncaught TypeError: Cannot read prop ...

Learn how to apply formatting to all textboxes on an ASP.NET website using CSS without having to set the CSS class property for each individual textbox

I am currently developing a website using Asp.Net. I am looking for a way to customize the font, color, and background color of all the textboxes on my site using CSS. However, I would like to avoid having to assign a "cssclass" property to each individua ...

Ways to determine the numerical value of a specific word chosen by a user

I'm struggling to retrieve the position number of a word selected by a user. For instance If I have a simple paragraph with two words: Hi and Bob <p>Hi Bob</p> Therefore, logically Hi=1 and Bob=2. Now, if the user selects Bob, I want t ...

The initial element within the div style is malfunctioning

Could someone assist me in understanding why the first-of-type CSS is not working correctly? .item:first-of-type .delete{ display: none ; } .delete { text-decoration: none; color: red; padding-top: 40px;} .add_form_field { white-space: nowrap; } < ...

Converting CSS colors from RGBA to hexadecimal format: A step-by-step guide

Within my selenium code, I am faced with the challenge of verifying that the background color code is #192856. However, when obtaining the CSS property of the element, it returns the color in rgba format. How can I retrieve the values in hex format? quick ...

Is there a way to customize the visual appearance of radio buttons by using unique color schemes

Incorporating bootstrap 4, I have implemented a basic set of custom yes/no radio buttons. <div class="custom-control custom-radio custom-control-inline"> <input type="radio" id="rd_1" name="rd" class="custom-control-input green" value="Yes"&g ...

One program with dual web views but shared localStorage

I've developed an application that includes two webviews, named webview_One and webview_Two. webview_One is set to access "http://localhost:12345:/a/index.html" webview_Two is set to access "http://localhost:12345:/b/index.html" Within a/index.html ...

Differences Between require: ngModel and scope: { ngModel: '=' } in AngularJS DirectivesWhen it comes to AngularJS directives, there are

Hi there, I'm trying to figure out which option is better. Can you please explain the differences and list out the pros and cons of each? Below is a comparison between the two options: scope: { ngModel:'=' } <!DOCTYPE html> <html ...

Why won't hover over function properly in separate divs for two items?

My goal is to show text when hovering over a logo, but the text and logo are in different divs. Despite trying various solutions like using display: none and display: block, I still can't get it to work. import styles from '../styles/Float.module ...

Using a jQuery function within an Angular application requires a careful integration process to ensure

I developed a small program in Angular that includes a method for searching a string in an array of strings like this: function searchStringInArray(str, strArray) { for (var j = 0; j < strArray.length; j++) { if (strArray[j].match(str)) return j; ...

Which video format is best to enable transparent video on web applications for both desktop and mobile, ensuring compatibility with all browsers on these platforms?

My NextJS web application utilizes a transparent video, currently available in MOV and WEBP formats. Interestingly, WEBP works perfectly on Chrome (Desktop) while MOV is optimized for Safari (Desktop). However, I encounter difficulties when viewing the v ...

What are some effective ways to modify many-to-many field information?

I need assistance with editing manytomanyfield data. Below is the model structure: class Permission(models.Model): shop = models.ForeignKey(Shop, on_delete=models.SET_NULL, null=True) permission_title = models.CharField(max_length=255) class M ...

Learn the method of including attributes with values in the td tag using jQuery

I am working with a table that has 2 rows and 3 columns. I am trying to add 2 attributes to each td element to identify its position in the table (these attributes are col & row which indicate the column and row for each td). This is my current code: ...

Creating multiple "read more" and "read less" buttons on a single webpage

Can you figure out what's going wrong here? I assigned each one a separate ID, but it's still not functioning properly in my code. I need to get this fixed for an assignment that's due in a few days, so any help is appreciated! function r ...

Deciphering image coordinates provided for incorporating an animated image component into a form

Is there a way to store the X:Y coordinates of a user click on an animated image by examining the php $_POST array? My issue is that I am able to retrieve input submission data from static images, but not from the animated one. Below is the full HTML page ...

Ways to adjust CSS styles according to the height of various device screens

Is there a way to adjust CSS/SCSS rules in applications like Angular based on the viewport height of various devices? When dealing with viewport width, we can use @media (max-width: ...) {}, but how can we address the height aspect? For instance, how wou ...

Discover the secret to toggling Bootstrap 4 cards visibility when hovering over the navigation menu using CSS

Hey, I'm currently working on a project where I want to show and hide specific sections of cards by just hovering over the menu list. While I can successfully hide the cards using CSS, I am facing issues with displaying them using the display:block pr ...

Tips for enhancing the user experience with a speed-optimized framework interface

The perception of a web page's loading time by users may not always align with the actual loading time. Here are various scenarios that can affect how users experience page loading: Users waiting for a blank page to load all at once. Some parts of t ...