Issue with scrolling to the bottom of collapsed sections in Bootstrap

I have a bootstrap collapse panel and I've added a toggle link at the bottom to allow users to expand and collapse the content with a click.

The Issue

My problem arises when the menu expands, causing it to scroll all the way to the bottom of the page instead of staying at the top where I want users to be. I've tried setting an offset top but haven't had any success.

Does anyone have any suggestions on how to resolve this issue?

My JQuery Code

$('html,body').animate({
     scrollTop: $(".collapsed").offset().top
 },500);

View My jsfiddle Here

Answer №1

In my understanding, if you want to scroll to the top when the collapse area is hidden, you can try using this method. The collapse event hide.bs.collapse triggers immediately when the hide method is called, with a default interval of 350ms. Since you won't see the end of your site, the scroll interval should be less than the default.

I apologize for any mistakes in my English...

Update

After reviewing all the comments, it seems that the issue lies in trying to scroll to the toggle button instead of the collapse area. When the area is not closed and at the bottom, simply scroll to the panel-collapse....

$(document).ready(function() {
  $('.panel-collapse').on('hide.bs.collapse show.bs.collapse', function(e) {
  $('html, body').animate({
      scrollTop: $(e.target).offset().top - 20
    }, 150);
  });
});

Answer №2

To enable the scrolling feature, you can set up an event listener for the toggle element and then monitor the classes of the collapsible panel.

$('#accordion .panel-heading a').on('click', function (event) {
  let panelCollapse = $(this).closest('.panel').find('.panel-collapse');
  if (!panelCollapse.hasClass('collapse')) {
    $('html, body').animate({
      scrollTop: panelCollapse.offset().top
    }, 500);
  }
});

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

JavaScript code to determine if a Rating is enabled for a SharePoint Online list

Currently, I am working on a JavaScript code that will allow users to provide star ratings for specific articles through a widget. One of the requirements is to first verify if the Rating feature is enabled for a particular SharePoint list. If it is enable ...

Finding the closest button using Selenium in Python

I am attempting to click on the "Delete Comment" button once I have identified a comment that contains the specific hashtag, husky, which is a clickable link. Since there are multiple "Delete Comment" buttons on the page, my approach is to locate the comm ...

Using Laravel to remove data with an incorrect ID

Whenever I try to delete data using the Delete button, it seems like the data being deleted does not correspond to the rows I intended. Instead, the data at the top of the table gets deleted. For example, when I use return $meja, id 1 shows up instead of ...

At what times do screen reader users utilize tabs?

Did you know that screen reader users often rely on specific keyboard shortcuts rather than tabs for navigating web pages? For example, in VoiceOver they may use CTRL + OPTION + Arrow Keys instead. This challenges the idea of optimizing for tabbable naviga ...

Discover properties of a TypeScript class with an existing object

I am currently working on a project where I need to extract all the properties of a class from an object that is created as an instance of this class. My goal is to create a versatile admin page that can be used for any entity that is associated with it. ...

What is the best way to remove the box-model from an element?

Within my VueJS project, I am faced with nested elements that are generated dynamically like this: <container> <outerElement class="outer" v-for="obj in objects"> <innerElement class="inner" v-for="el ...

In a two-column layout, ensure that the text in the left box is aligned to the left and the text in the right box is aligned to the

I'm working on a flexbox layout with two columns, each containing text. The text in the left column is aligned to the left, while the text in the right column is aligned to the right. However, I want the text in the right column to switch to left al ...

Having trouble getting webpack and babel to start using npm

Greetings, wonderful people of the internet! I am a newcomer to the enchanting world of programming and I am facing a perplexing issue. Although Webpack is trying to guide me towards the solution, I seem to be struggling with fixing it on my own. const pa ...

Having trouble transmitting JSON data to an Express server through the browser

I have set up two servers: the first one is for frontend (localhost:7200), and the second one is for backend (localhost:7300). There is a test request made by the frontend to the backend on the route '/test'. The issue arises when I attempt to s ...

Filtering dropdown lists using JQuery

One of my drop down menus labeled ddl1 has the following options: Bus Bike The second drop down menu ddl2 displays: Bus Mercedes Bus Ford Bike Honda I am looking for a script that can refine the options in the second dropdown based on the selection i ...

Is there a way to make the text on my Bootstrap carousel come alive with animation effects?

My website features a Bootstrap Carousel with three elements structured like this: <a><img data-src="img" alt="Third slide" src="img"> </a> <div class="carousel-caption"> <h2> <u ...

Is node.js required for utilizing Angularjs?

Considering incorporating angular.js into my website's Image Editing Tool. Is node.js necessary for this integration? I'm a bit puzzled here. Are there instances where both nodejs and angularjs are used together, or can they operate independentl ...

I keep encountering the error message "$(".dropdown-toggle").dropdown is not a function" while attempting to use Dropdowns with Jade and Bootstrap

I am attempting to implement a Bootstrap dropdown in my Jade layout. However, I keep encountering an error when trying to run $(".dropdown-toggle").dropdown in my Jade file: $ is undefined $(".dropdown-toggle").dropdown is not a function What could be ...

Is it necessary to have col-xs-12 in Bootstrap 3?

In order to achieve the desired display for different screen sizes, we have implemented the following code: <div class="col-md-6">left</div> <div class="col-md-6">right</div> When viewed on smaller screens than md, this code succe ...

Trigger a jQuery event when a different selection is made in the dropdown menu

There are 2 choices available in the dropdown menu. <select id="_fid_140" onchange="chooseRecordPicker()" > <option value="736">9000000146</option> <option value="x3recpcker#">&lt; Browse choices... &gt;</option> Upo ...

Vue.js component still not transitioning out despite using mode="out-in"

As I navigate my way through learning about vue.js, one issue that has been puzzling me is how to make routed pages fade in and out when a user switches to a new page. Despite thinking it would be a simple task with vue, I have been struggling quite a bit ...

When using Next.js revalidate, an error may occur stating: "There are keys that require relocation: revalidate

I have been attempting to utilize the revalidate function in my code by following the example provided by Vercel. However, I keep encountering an error. Here is the snippet of code that I am currently using: export async function getServerSideProps() { c ...

Interactive PNG Sequence Rotation: Explore 360 Degrees

I am facing an issue with my PNG sequence of 360 images, where each image corresponds to a degree of rotation. Currently, I have a React component that updates the rotation based on the mouse position in the window - with x = 0 corresponding to rotation = ...

Using the Match() function with an array of objects

I am working with an object array containing multiple variables. let Novels = []; class Novel { constructor(isbn, title, author, edition, publication, year) { this.isbn = isbn; this.title = title; this.author = author; this.publicat ...

`Unable to process JSON information from Google Places`

I am currently working on retrieving a rating for a specific place from Google using a JSON request by placeID. The fields I am requesting are the name and rating of the place. Upon viewing the JSON URL in my browser, I can see the data displayed as follow ...