The issue of flickering during the combination of slideToggle and animate in jQuery

I want to incorporate a hidden div that, when triggered by a button click, will smoothly reveal itself and "push" away part of another div without changing the overall size of the containing div.

However, I have encountered an issue where the div below the hidden element appears to flicker or bounce slightly. This could be attributed to the animations not synchronizing properly.

Below is the code snippet:

HTML:

<div id="wrapper">
    <div id="button">CLICK</div>
    <div id="content"></div>
    <div id="hidden"></div>
    <div id="bottom"></div>
</div>

CSS:

#wrapper {
    border: 1px solid green;
    width: 300px;
}
#content {
    border: 1px solid red;
    height: 200px;
}
#hidden {
    border: 1px solid blue;
    height: 100px;
}
#bottom {
    height: 20px;
    border: 1px solid purple;
}

JAVASCRIPT

  $("#button").click(function (e) {
      var newHeight = $("#content").height();
      var hiddenHeight = $("#hidden").outerHeight();
      var visible = $("#hidden").is(":visible");

      newHeight += (visible ? hiddenHeight : -hiddenHeight);

      $("#hidden").slideToggle({
          duration: 200,
          queue: false
      });

      $("#content").animate({
          height: newHeight
      }, {
          duration: 200,
          queue: false
      });
  });

fiddle: http://jsfiddle.net/2exxrs1n/

Are there any suggestions on how this issue can be resolved, or perhaps an alternative approach to achieving the desired effect?

Answer №1

Perhaps the reason lies in the lack of perfect synchronization with jquery animations. You might want to explore using CSS3 transitions instead.

transition-duration: 0.5s;

Check out this updated version of your fiddle for an example.

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

iframe failing to load link after initial attempt

As a beginner in using iframes, I have come across an issue that is puzzling me. I have a navigation bar and an iframe on my webpage. When I click on the "Search" link, it loads into the iframe without any problems and I can work within it. However, if I c ...

Add a new row directly beneath the row that has been selected using JQuery

I am currently working on a code snippet where I need to add functionality to delete and add rows for each individual row. Specifically, I want the ability to add a new row directly below the selected row. Does anyone have any insights on how this can be ...

Choose the currently active md-tab within the md-dialog's md-tab-group

I need to create a dynamic md-dialog with an md-tab-group that has two tabs. The md-dialog should open based on the button clicked, displaying the corresponding tab content. The initial template where the md-dialog is triggered: <button md-button class ...

When it comes to choosing between CSS and Angular Animations for supporting animations on both browsers and NativeScript mobile applications, which approach is more effective?

Are angular animations my only option or can I also utilize css animations with native elements on mobile devices? How are css animations from an angular application translated to native mobile, and is this method less effective than angular animations? I ...

AngularJS: Refresh array following the addition of a new item

After saving an object, I am looking to update an array by replacing the conventional push method with a custom function. However, my attempts have not been successful so far. Are there any suggestions on how to rectify this issue? app.controller("produ ...

I find myself having to double-click the Jquery submit button

Can anyone help with an issue I'm having where I need to click a div button twice for the link to work properly? Thank you <div class="toolbar"> <table width="100%"> <tr> <td class="toolbar-button" title="Print Data" id ...

The top value in CSS animation fails to change properly

Can anyone help me figure out why the animation doesn't work when I try to change the vertical position of a form using JQuery? I want the input field to smoothly move to its new position so that users can see it happening. Here is the JsFiddle link: ...

Using AngularJS: Implementing ng-include with a custom function

When using ng-include, I have encountered a peculiar issue. I am attempting to retrieve the template path by invoking a function defined in the controller that returns the path based on the parameter passed. Below is my code snippet: <tr ng-repeat="det ...

Hiding a parent DIV in JS based on specific content: Here's how

I need help figuring out how to hide multiple parent DIVs based on the content of a specific child DIV. Here's an example: <div class="report-per-day"> <div class="report-day">26 May 2022</div> <div class=" ...

Using the flexslider to override CSS styles in an <li> element

I have implemented the flexslider by whoothemes on my responsive website to showcase tiles. However, I am facing an issue with setting a specific width for the tiles on various devices. Upon inspecting with Chrome, I see this code: <li style="width: 21 ...

Loading HTML dynamically

Currently, I am working with node.js, express, and handlebars. When passing JSON data via res.render() to handlebars (hbs), I use {{#each dataJson}} to loop through the data and display it. However, if my JSON file had a large number of entries, loading t ...

Implementing asynchronous code when updating state using React hooks

Here's a scenario I'm dealing with: const [loading, setLoading] = useState(false); ... setLoading(true); doSomething(); // <--- at this point, loading remains false. Since setting state is asynchronous, what would be the best approach to ...

Successfully launched Laravel application, experiencing issues with all pages except for the homepage

I recently completed a website for a client and everything seems to be working fine locally. Unfortunately, when trying to access any links on the site, I am getting a 500 server error. The hosting is with 1and1, and I've heard mixed reviews about it ...

The browser is opting to download the HTML file rather than displaying it

Recently, I set up a server using Node.JS express where I specified the location of an HTML file in the public folder. app.use(express.static(__dirname + '/public')); app.listen(8080); In previous projects, this setup worked seamlessly. However ...

Challenges encountered with the "load" event handler when creating a Firefox Extension

I am currently troubleshooting a user interaction issue with my Firefox extension. The tasks that my extension needs to complete include: Checking certain structures on the currently viewed browser tab Making backend server calls Opening dialogs Redirect ...

Jquery Ajax Request

My goal is to create an Ajax call with the following specifications: $.ajax({ type: "GET", dataType: "json", contentType: "application/json", username: "user123", password: "admin123", url: "http://localhos ...

Having trouble implementing new controllers in AngularJS UI-Router for nested states?

I'm currently facing an issue with nested states in the ui-router. My task involves rendering a page that includes regions, countries, and individuals per country. In the index.html file, there are three regions represented as links: EMEA, APAC, and ...

Why does Res.send return an empty object when console.log indicates it is not empty?

I am currently facing a challenge while using the Google Sheets API with Express, as I have limited experience with JavaScript. My goal is to pass a JSON object from Express to React, but for some reason, when I send the object, it appears empty on the fro ...

css center arrow-aligned underline text

Trying to emphasize my text with an additional arrow centered on the line is proving to be a challenge. When attempting this, the text does not align in the center of the page. .souligne { line-height: 17px; padding-bottom: 15px; text-transform: u ...

Is it possible to transfer data to the next page by clicking on a table row?

I'm facing an issue with a non-clickable table. When a row is clicked, I want to navigate to a detail page from the table while passing the id value in the process. I know that I need to create an href attribute on the table row and somehow transfer ...