Expandable window feature triggering panel

I've been working on implementing a responsive collapsible panel for my website, and I came across an example on W3Schools. However, the example had a flaw - resizing the window after expanding or collapsing the panel caused it to not scale properly.

EDIT: Check out these screenshots:

https://i.sstatic.net/Qc3Du.png

https://i.sstatic.net/1Cpw1.png

Here is their example:

Code for collapsible section

// Button Collapsible
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function () {
        this.classList.toggle("active");
        var content = this.nextElementSibling;
        if (content.style.maxHeight) { content.style.maxHeight = null; }
        else {
            content.style.maxHeight = content.scrollHeight + "px";
        }
    });
}

CSS Code for affected panel

.aboutCollapsible {
    font: 16px/24px "Roboto";
    background: #202020;
    text-align: justify;
    margin-bottom: 20px;
    width: 580px;
    max-width: 100%;
    padding: 1px 20px 5px 20px;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.5s ease-out;
}

My query: How can I maintain the smooth scrolling animation of the panel while ensuring it responds correctly to window scaling? I prefer sticking to just HTML, CSS, and JavaScript without using Bootstrap or other frameworks

What I have attempted: I experimented with changing the measurements from pixels to percentages, but that didn't solve the issue with the animation.

Answer №1

UPDATE: While this solution is not without flaws as it may cause the collapsing animation to appear too sluggish (giving the impression of a delayed animation), it can still be effective:

Modify the code within the else statement to read

content.style.maxHeight = "1000px";
. Choose a height that you believe your content will never reach.

Previous Solution:

To overcome this issue in your code, simply make the following adjustment:

else {
    content.style.maxHeight = content.scrollHeight + "px";
}

Change it to:

else {
    content.style.maxHeight = "initial";
}

Give it a try and see if it resolves the problem.

Answer №2

Enhance with a touch of style

.highlighted+.text {
    max-height: 200px;
    overflow-y: auto;
}

Adjust the max-height to suit your needs.

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

Create a unique CSS class that combines custom styling with the pre-defined classes provided by

Hello everyone, I'm curious if there is a way to create a custom CSS class that incorporates existing Bootstrap classes? For instance, take a look at the following snippet of HTML: <div class="text-primary center-block">Here is some text</ ...

What is the best way to condense a touchmove event listener in JavaScript?

Trickshot #29 demonstrates how touch events can be defined in jQuery. I have reinterpreted it with my own unique style of coding in this fiddle. The author sets up a touchmove listener every time a touchstart event is triggered. request.dom.ball.on(&apos ...

Error: Your Discord bot is unable to send a message as it is empty. Please check Discord

I have been developing a Discord Bot to verify Roblox accounts. Although my script is prepared and the command "-verify" can be executed, an error arises: (node:22872) DeprecationWarning: The message event is deprecated. Use messageCreate instead (Use `n ...

"Implementing a method to update an object by its Id within a nested array that includes children, and then managing the state in

How can I dynamically toggle the isExpanded property on click based on an element's ID in a React project? Below is the JSON data structure I am working with const handleExpandOutlineItem = (id: string) => {} This is the structure of my JSON data: ...

Strategies to verify if two objects of the same class possess the same attribute

I have multiple elements with the same class and unique attribute values, such as: <div class="myclass" data-attr="1"></div> <div class="myclass" data-attr="2"></div> <div class="myclass" data-attr="3"></div> <div cl ...

Display a notification for recurring users with the exception of first-time visitors

We have been working on updates for an older website and want to notify returning or repeat visitors of the changes. After making updates, some browsers continue to show a cached version of the site to returning visitors, causing them to see the site as b ...

Calculate the total of an array with the help of the angular forEach function

function dialogController(generate, $scope) { $scope.profiles = generate.get_keys('::role'); $scope.content = {}; $scope.options = []; $scope.servers = {}; $scope.subs = {}; $scope.discountList = {}; $sco ...

The function `open` is not a valid prop for the `Dialog` component

Upon clicking an icon, a dialog box containing a form should appear for either adding a tab or deleting a specific tab. I have utilized reactjs, redux, and material-ui for the components. While I am able to display the dialog box when the icon is clicked, ...

Utilizing JSP to trigger a servlet upon loading without the use of script

Is there a way to automatically call a servlet when my JSP page is loaded? Currently, I have a link like this: <a href="ContentServlet?action=userContents">Homepage</a> However, I would like the servlet to be called without having to click th ...

Load information from a JavaScript object when the user clicks dynamically

My challenge involves utilizing a JavaScript object that contains video information such as title and source URL. Within my HTML, I have image placeholders and the goal is to trigger a modal pop-up (using Twitter Bootstrap modal) of the specific video when ...

Creating a navigation bar with an active tab feature using Nuxt.js and Vue

Currently, I am working on a side navbar and attempting to activate it based on the page click. The sidenav.nav is a component that I am using for this purpose. <template> <aside class="col-md-3"> <nav class="list-group ...

The function val() will not extract the present input value

Can't seem to retrieve the updated value of an input field in my ajax log-in form. Here's the code snippet: <form method="post" id="contactform"> <label for="name">Username</label> <input type="text" id="un" placeho ...

What is the best way to validate a user's input using regular expressions?

My form contains various controls and I opted not to use the validations provided in the Visual Studio toolbox. Instead, I have implemented validations for these controls using JavaScript. One of the validations that I have set up is a Regular Expression. ...

Express JS sub child routes are experiencing issues with static files not functioning properly

I used express-generator to create my project structure. In app.js, the code is as follows: var createError = require('http-errors'); var express = require('express'); var path = require('path'); var cookieParser = require(&a ...

Adjusting the z-index of the tinyMCE toolbar

I have been utilizing the tinyMCE editor plugin, which transforms textareas into iframes and displays a toolbar at the top of the content. It has been functioning flawlessly. However, there are times when there are videos placed above the content. In such ...

NodeJS: Warning of unhandled promise rejection when attempting to change password

During my work on implementing password changing functionality in NodeJS, I encountered the following error while processing a request: (node:16220) UnhandledPromiseRejectionWarning: Error: Can't set headers after they are sent. at validateHeader ...

using an object's key as a variable in JavaScript

Is it possible to reference an object's key as a variable and then modify its value? I am experimenting with the following: const obj = { x: { y: { z: 'test' } } } const func = () => { let root = obj['x'] ...

Tips for troubleshooting syntax errors in JavaScript

Are there any online tools similar to jsfiddle where I can input my JavaScript code and receive feedback on syntax errors, misplaced commas, or semicolons? Here is the example code I am looking to validate: $("#customer").autocomplete({ ...

Tips for adjusting the font size of a Chip using Material-UI

I am using a widget called Chip const styles = { root:{ }, chip:{ margin: "2px", padding: "2px" } } const SmartTagChip = (props) =>{ const classes = useStyles(); return( <Chip style={{color:"white&q ...

The data in my JSON string is not fully received through the HTTP GET request

Whenever I send my array to the PHP file, it receives incomplete data. The content of my 'arr' variable is as follows: [["╪▒┘à┘╛┘╪د","67126881188552864","Empty,Empty,Empty,Empty,8644,-360,-4,8691,-3.48,-313,1015,4.334 M,1392/12/2 ...