Tips for specifically targeting the accordion panel header when it is clicked

I am currently facing an issue with my accordion that contains lengthy text in the panel body. When I open any accordion, the page scrolls to the top and part of the text gets cut off. Is there a way to fix this problem or do I need to focus on the panel-header when opening the accordion?

Check out this JSFiddle to see how it appears.

https://i.sstatic.net/UTjil.jpg

Looking forward to any helpful suggestions,

Paul

Answer №1

Previously, I provided a solution for a similar question regarding materialize.css.

Feel free to adjust the logic based on your preferences, as my style might have influenced this response. To maintain consistency with my earlier answer, it's important to note that the scrolling effect won't be applied if:

  • It's the initial panel element in the accordion,
  • The height of its body is less than the desired height.

This implementation activates the scrolling effect once the panel body is "shown" post-animation by listening for bootstrap's collapse event, shown.bs.collapse, allowing validation based on whether the height surpasses the specified threshold.

$('.panel-collapse').each(function(){
    $(this).on('shown.bs.collapse', function(){
        var $this = $(this),
            //$header = $this.siblings('.panel-heading'),
            $parent = $this.parent(),
            height = $this.height(),
            maxHeight = 400;

        if ($parent.is(':first-child')) return;

        if (height > maxHeight)
            $('html, body').animate({
                scrollTop: $parent.offset().top
            }, 500);
    });
})

Access updated fiddle here


A feedback mentioned that the previous snippet does not scroll back up to the header if the height doesn't exceed the specified maximum height (adjusted to 400 in the code above).

If you wish to eliminate the validation criteria, simply remove all the if conditions and proceed accordingly.

$('.panel-collapse').each(function(){
    $(this).on('shown.bs.collapse', function(){
        var $this = $(this),
            $parent = $this.parent();

         $('html, body').animate({
            scrollTop: $parent.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

Creating a hierarchical structure in HTML to represent a JSON object as a tree: techniques and best practices

I need help with displaying a nested JSON object that shows a one to many relationship between a parent node and its children in an organizational chart format using HTML. I have looked into utilizing Google Charts for this purpose, but I am unsure if it a ...

Pressing the "Enter" key will trigger a JavaScript function while the textbox is in focus

I am looking to enhance my search functionality on my website. <div class="TextField Large"> <input type="text" name="q" id="SearchBox" /> </div> <div style="height: 40px; text-align: right; position: absolute; top: 0px; right: 0p ...

Python Selenium: Having trouble clicking a button without any element identifiers such as id, value, or name associated with the button tag

I'm experiencing trouble with Selenium as it attempts to interact with the "Become A Member" button found on this link: . Here is the HTML code relating to the button: https://i.sstatic.net/Pjeu3.png Despite my efforts in using various methods such ...

Disabling a button does not automatically shift the focus to the next element in the tab order

Is there a way to limit the number of times a button can be clicked using jQuery? For example, I want a button that can only be clicked three times before disabling itself automatically. test.html <div class="main"> <input class="one" type="t ...

Deciding whether an altered image has been successfully loaded

Currently, I am stuck on a minor point while creating a small gallery using jQuery. The code snippet looks like this: <script type="text/javascript> $(document).ready(function(){ $('#thumb1').click(function(){ $('#fullimage ...

Display the CSS dropdown menu as a block`

I've been attempting to design a dropdown menu. My goal is for the anchor tag to display when hovered over. I understand that using display:block won't work when the 'a' is not part of the block, but I'm unsure how to proceed. Any ...

The function does not provide an output of an Object

I have two JavaScript classes, Controller.js and Events.js. I am calling a XML Parser from Events.js in Controller.js. The Parser is functioning but not returning anything: SceneEvent.prototype.handleKeyDown = function (keyCode) { switch (keyCode) { ...

What's the reason behind this file not opening?

When I try to insert this code into files index.html, style.css, and app.js, the page refuses to open. The browser constantly displays a message saying "The webpage was reloaded because a problem occurred." I am using a MacBook Air with macOS Big Sur and a ...

What is the best way to share the recipient's address with the browser link?

I am currently working on a project using PHP Zend with SQL Server 2008, jQuery, and AJAX. My goal is to send an email containing a webpage link to a specific client and receive feedback from them. I want to ensure that only the intended recipient is abl ...

Concealing a field when the PHP post is devoid of content

On page1.php, there is a form that, upon submission, redirects to page2.php where the selected information is summarized. The code snippet below on page2.php retrieves this information. I am attempting to dynamically hide certain rows if the PHP post is e ...

I am currently working on a project using ar.js

I came across a glitch code that triggers a video when scanning the marker. However, I encountered an issue where it only works on desktop. On Chrome for Android, the video does not appear, only the sound can be heard. I need assistance as my coding knowle ...

Creating a JSON object from form selections in order to construct an SQL query can be achieved by utilizing a jQuery AJAX request to communicate with a PHP script

I need help creating an HTML form with two list-boxes and a check-box that will filter results on a webpage. How can I set it up so that when the Submit button is clicked (.onclick(function(){})), a jQuery AJAX call is triggered to a PHP script on my loca ...

What purpose does the div tagged with the class selection_bubble_root serve in Nextjs react?

Just starting out with Nextjs and setting up a new Next.js React project. I used create-next-app to initialize the code base, but then noticed an odd div tag with the class 'selection_bubble_root' right inside the body. Even though its visibility ...

Adjust the dimensions of the react-dropdown-tree-select element to better fit your needs

Hey there, I'm a beginner web developer currently working on a tree-based dropdown menu. Check out the documentation here To see it live in action, visit the example here I'm trying to adjust the height and width of the "Search/DropDown bar" in ...

Creating a Dual Y-Axis Chart with Two Sets of Data in chart.js

I utilized the chart.js library to write the following code snippet that generated the output shown below. My primary concern is how to effectively manage the labels on the horizontal axis in this scenario. CODE <!DOCTYPE html> <html lang="en"& ...

Concealing dropdown choices in a form with CSS only

My CSS skills are pretty limited, and despite my efforts to find a solution to my problem, I've come up short. I'm trying to tweak a CSS stylesheet to customize an advanced search form tied to a database. Unfortunately, I can't make any chan ...

Tips for disregarding global CSS in Nuxt.js?

I've been utilizing a boilerplate that integrates with the CoreUI template from GitHub, and it's been working well. However, I am now looking to customize my index page with a unique style, and I'm encountering issues where the global CSS fr ...

Displaying a relative div on mouse hover over an HTML tag using React

In the section below, you will find the code snippet... <ul className="no-style board__list"> {Object.keys(today.books).map(function(id) { var refBook = today.books[id][0]; return ( ...

Having trouble deleting multiple rows with ng-repeat in datatables

Having followed the instructions in this post, I quickly integrated it with jquery datatables. However, the functionality is not as expected. When attempting to delete rows, they do not get deleted. Furthermore, if I navigate to the next page and return, ...

Mysterious behavior in JavaScript causes it to skip the second index of an array

Below is a lengthy snippet of html code that I would like to discuss: <!DOCTYPE html> <html> <head> <script type = "text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script> $( ...