Follow the collapsible bottom window/screen

I'm attempting to make my screen automatically scroll to show the content under a collapsible when it is opened. I've tried using window.scrollTo() but it only scrolls to the current location of the bottom footer when the collapsible is opened.

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


Edit:
Here is my code snippet: https://jsfiddle.net/jz961rL0/2/

Answer №1

I successfully implemented this solution using a ResizeObserver. By assigning an id to the collapsible container for easier querying, I then attached an observer to it. Whenever there is a dimension change, the container will automatically be scrolled into view:

// https://www.w3schools.com/howto/howto_js_collapsible.asp
var coll = document.getElementsByClassName("collapsible");
var i;

let collapsibleContainer = document.getElementById("container");

let resizeObserver = new ResizeObserver(entries => {
  // Ensure that entries only contain one element.
  // Scrolling to multiple elements simultaneously may lead to unexpected behavior.
  for(let entry of entries) {
    collapsibleContainer.scrollIntoView()
  }
});

resizeObserver.observe(collapsibleContainer);

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";
    } 
  });
}

HTML:

<button class="collapsible">      
  <div class="wrapper">
    <h1 class="headline">collapsible</h1>
  </div>
</button>   
<div class="content-collapse" id="container">
    <!-- Remaining content -->
</div>

Unfortunately, ResizeObserver is not compatible with Internet Explorer. However, hopefully you do not need to support that platform.

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

What type of programming language is utilized in creating this online interactive calculator?

I have a strong interest in creating an interactive calculator like the one found on keto-calculator, but I am unsure of where to begin my research on how to develop it. Can you provide insights on the coding language typically used for such calcula ...

Troubleshooting Cross-Origin Resource Sharing problem with Stripe API integration in Angular

I am diving into the world of Stripe API and CORS for the first time. I've set up a POST request from Angular to my Node.js server. Since the client origin differs from the server destination, I have implemented CORS on the server side. When inspectin ...

Having difficulty with checkboxes in Vue.js: selecting one checkbox selects all, and struggling to pass dynamic data as the value

Currently, I am facing a complex issue related to dynamic checkboxes in Vue. This is the current state of my code: <table class="table table-striped"> <thead> <tr> <th>Student</th> & ...

Vue Single Page Application - invoking methods across all components

I am currently developing a notification feature that can be triggered from any component. It utilizes a straightforward Vuetify v-snackbar. Within App.vue <router-view :key="$route.fullPath"></router-view> <v-snackbar :valu ...

Removing/modifying selected choices in an element

I have implemented a material ui select element with the ability to make multiple selections using checkboxes. My query is, can I incorporate the functionality to delete or update names directly from the select element itself? For instance, by clicking on ...

IN to CM Conversion Size Chart

I'm just starting out and I want to make a size chart table that can convert between metric units. For example, I have table data with values in "inches" and I want to be able to switch it to "centimeters" with the click of a button. Some of the data ...

React Native - Issue with Chart not updating correctly when new data is added

I'm struggling to create a dynamic chart using the Victory-Native library (click here for documentation). The goal is to modify the chart after pressing the "Get Data" button, but I've encountered a problem that has me stumped after hours of att ...

What is the best way to send data to an API controller using AJAX in an MVC framework?

I am facing an issue with POSTing a string data to the api controller in mvc using ajax. Despite my efforts, the data does not seem to reach the api controller. Here is what I have attempted: This is the JavaScript code I have used: ...

Restricting the embedding of my website to a select few domains

Looking to embed my web app on various websites, but I want to restrict access based on domain. For example, allowing and to embed without issues, while blocking . Is there a way to achieve this? Using iFrames for the embedding process. ...

What is the best way to ensure that bootstrap columns collapse in equal multiples?

I need to have 4 equal columns in each bootstrap row with only specific configurations allowed: All 4 next to each other Two above and two below All in a single column The issue is that when I adjust the screen size, sometimes I get 3 columns at the top ...

An issue arises when trying to showcase necessary data within vue.js templates

I'm having an issue with displaying a specific value based on an onclick event. The value is taken from an object in the bookData array: this.bookData.push( { avalable: true, titles: [title, id], author ...

What is the best way to change the order of rows and columns in

Is there a way to rotate a table using jQuery? Maybe with a function or some other method? For instance, if I have a table structured like this: <table> <tr> <th></th> <th></th> <th>&l ...

Pictures are not aligned at the center of the bigger card container

I am facing an issue with centering images on my cards. The containers look fine on PC (centered) when smaller, but on responsive mode they revert back to the larger size and are left aligned. I want the images in the card containers to be centered regardl ...

Received an Error: (middle value)(...) does not function as intended [Yes, Formik]

const {values, handleChange, handleSubmit, errors} = useFormik({ initialValues: { name: '', address: '', latitude: '', longitude: '', nit: '', category: '', ...

Tips for accessing parameters from a URL in a React JS application

Being a beginner in React, I am struggling to access the parameter from the URL. Despite trying various solutions, I keep encountering the error "Uncaught TypeError: Cannot read property 'params' of undefined". How can I successfully retrieve the ...

AngularJS offers a variety of 'select all' checkboxes tailored for specific lists of checkboxes

Within my webpage, I have three different sets of checkboxes. Each set includes a "select all" checkbox. Instead of repeating code lines, I am implementing a single function with a parameter to select specific checkboxes within each set. $scope.sele ...

My Node/Express service seems to be generating a span in the logs, but I am unable to locate my service in the Ja

Currently, I am in the process of setting up Jaeger tracing for my Node.js microservice using Express.js. After adding a basic GET request handler in my Express app and hitting the endpoint via curl, I notice that a span is created in the logs. However, w ...

When utilizing PassportJS, SequelizeJS, and JWT tokens, req.user may be found to be undefined

Despite searching various answers on Stackoverflow and consulting the documentation, I am still unable to identify what might be causing the issue. Within my application, I am utilizing SequelizeJS to interact with my MySQL database and now attempting to s ...

What is causing the action button in my MUI snackbar to occupy an additional line?

I'm currently learning how to use MUI and I'm struggling with a specific issue. The close button in my code is appearing on a separate line, but I want it to be on the same line as the word "again". I've tried experimenting with absolute pos ...

Box Filter Plugin

Can anyone please help me identify the name of the plug-in used for the perfect filter box on this website I found? Website address: ...