Using the hide() and show() functions may result in sporadic page jumps

My table is filled with information about job listings that a user is interested in. Here's how the structure looks:

<div class="container">
    <table class="table-responsive table-hover table-bordered col-sm-12 col-md-6 col-md-offset-1 col-lg-6 col-lg-offset-1" id="resultsTable" border="0" cellspacing="0" cellpadding="0"><tbody>
        <tr class="selectable selected">
            <td class="summary">A Summary</td>
        </tr>
        <tr>
            <td class="detail" id="32113607" style="display: table-cell;">This is some detail</td>
        </tr>
<!-- More rows here............... -->
    </tbody></table>
</div>

I've also implemented a function that manages expanding and collapsing the table rows.

$(function () {

    //Clicking on a row in the table will hide the other rows...
    $("#resultsTable").on('click', '.selectable', function (e) {
        $(this).addClass('selected').siblings().removeClass('selected');
        $(this).siblings().children("td.detail").slideUp();

        $(this).next("tr").children("td.detail").slideDown();
    });
});

In addition to this JavaScript functionality, there are some basic CSS styles applied. You can view all of this in action through this fiddle link: http://jsfiddle.net/v9ec3/1176/

An interesting issue arises when navigating through the records: starting from the bottom up works fine, but going from top to bottom causes the display to jump midway during scrolling.

Upon inspecting the code using developer tools, it seems like jQuery is attempting to adjust the scroll offset during animation transitions.

Various methods have been tested to address this problem:

  • e.preventDefault()
  • e.stopPropogation()
  • adjusting width/height properties
  • modifying overflow-y attribute
  • implementing animations (such as scrolling to specific ID)
  • applying CSS transitions

Answer №1

Big thanks to Kevin B for sharing a helpful link that I had not come across before. It enabled me to figure out how to maintain the positioning of elements:

$("#resultsTable").on('click', '.selectable', function (e) {

        var prevSelectedHeight = $(".selectable.selected ").next("tr").height();
        var currentScrollTop = $(window).scrollTop();

        $(this).addClass('selected').siblings().removeClass('selected');
        $(this).siblings().children("td.detail").hide();

        if (currentScrollTop > prevSelectedHeight) {
            $(window).scrollTop(currentScrollTop - prevSelectedHeight)
        }
        $(this).next("tr").children("td.detail").slideDown();
});

This solution now works in most scenarios. There are some rare cases where the page is scrolled to the bottom, causing the elements to shift upwards slightly. However, it functions well for the majority of items on the list.

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 could be the reason why both the add and remove functions are unable to work simultaneously within a JavaScript function?

Hi there! I recently started diving into JavaScript and encountered a little hiccup. I've been working on a dice game where images change randomly whenever a button is clicked. The images transition from one to another, but I wanted to add a rolling ...

Is it possible to create a development build using Npm with React and Typescript?

I have successfully set up a TypeScript React app by using the command below: npx create-react-app my-app --template typescript However, running "npm start" generates development javascript files and launches a development server which is not id ...

I am facing an issue with the asynchronous function as it is displaying an error message

**I am facing an issue with displaying categories. I have attempted to do this using async function, however the data is not showing up** <div class="form-group"> <label for="category">Category</label> <select id="categor ...

What is the best way to extract key/value pairs from an object in javascript that have a specific suffix?

I'm dealing with an object: { a_suff:"Something", b_suff: "Value", c: "Value", d: "Value, e_suff: "Value" } I'm looking for a way to extract all the keys that end in "_suff". What would be the most efficient solution? Currently, I have this im ...

Displaying spinner until the entire section has finished loading

In order to display a loading spinner until all images in a specific section of the page have fully loaded, I am utilizing Django, jQuery, and Ajax technologies. The HTML structure consists of two main divs: On the left side, there is a table, while on t ...

Passing parameters from a div to a single page component in Vue.js: A complete guide

There is code that appears on multiple pages: <div id="contact-us" class="section md-padding bg-grey"> <div id="contact"></div> <script src="/dist/build.js"></script> </div> Included in main.js is: im ...

Delay Export of React Component Until After Request in Shopify App Development

Being a newbie in Shopify App Development, React, and Next.js, I may have a silly question. Currently, I am making a request to a website and using the response in the React component that I want to export/render. To avoid it being undefined, I need to wai ...

What is the best way to retrieve the current date and time in PHP?

How do I get the date and time similar to 05-17-05 at 02:33:15 Can you please assist me in writing this in php? Can I use at with the date function in php? ...

I am looking to implement the ajax data variable for use in a submit button. How should

I'm facing an issue where, upon clicking the submit button, I am unable to receive the value yearend. Instead, I am getting an undefined value. How can I ensure that I receive the correct yearend value when I click the submit button? Here is my code: ...

Searching for a JavaScript tool that offers syntax highlighting capabilities

I have come across some excellent RTE HTML text editors such as jsredaktor, TinyMCE, and FCK Editor, but I am in search of an HTML/JavaScript component that functions similarly to a TEXTAREA with built-in code syntax highlighting. ...

Avoid adding any empty entries to the list using jQuery

I have implemented a code to prevent any blank entries from being added to my list, and it seems to be working fine. However, I can't shake the feeling that there might be a better way to achieve this. Even though it functions correctly, it doesn&apos ...

Customizing an external module class using CSS module pattern in React

Is there a way to locally override an external module's CSS class for a specific component? Here is my current code: import `style` from './style.module.css' // local CSS module import `ExternalComponent` from 'ExternalComponent&apos ...

Creating a classification for a higher order function

In the code snippet below, I have a controller that acts as a higher order method: const CourseController = { createCourse: ({ CourseService }) => async (httpRequest) => { const course = await CourseService.doCreateCourse(httpRequest. ...

Troubleshooting CSS issues in HTML pages

I created an HTML file but the CSS properties are not displaying correctly in the browser. I'm not sure where I made a mistake. Instead of using a separate CSS file, I have included it directly in the HTML file. <!DOCTYPE html> <html> &l ...

How does the 'order' stack up against the 'products' array? Display the names of the products that are included in the order

I have an array of items for sale: const products = { { id: product1, name: 'Product A', price: 100 }, { id: product2, name: 'Product B', price: 200 }, { id: ...

When using the TinyMCE editor in a modal, the submenus appear at the top of the screen

I am currently using TinyMCE version 5.0.2, which is self-hosted, and integrating the editor through the official TinyMCE React Component. I have placed the editor inside a modal that was created using React Modal. The issue I am facing is that all of the ...

The words overlaid on the image spill out beyond its edges

I want to create a design where the text flows outside of the image and continues into another container. After some trial and error, I managed to achieve a layout where the text seamlessly extends beyond the image and into a separate section. Ideally, the ...

What is the best way to continuously loop a CSS animation that has multiple keyframe definitions?

Exploring the Challenge In my coding journey, I encountered an interesting dilemma involving two css keyframe animations operating on a single element: .fade-bg { animation-name: fade-bg-1, fade-bg-2; animation-delay: 0, 6s; animation-iteration-cou ...

Trouble Deciphering JSON Array Using Eval Function

I'm facing an issue with two files in my project - one is a PHP file containing an array that is echoed using json_encode, and the other is a JavaScript file containing various functions for a webpage. One particular function in the JavaScript file is ...

Reached 10 iterations in the Digest, stopping due to a scope function problem

When processing data, I encounter a unique challenge where the information is returned in an unusual format (a single string representing all the options/labels of a radio button group). For example: "yes|Yes no|No" To address this issue, I have develop ...