Having trouble with animate() and fadeIn() functions not behaving as expected

I am facing an issue where I want the third1 div to smoothly move inside the third div as it fades in. Unfortunately, due to its absolute position, it is not being positioned correctly. I am also trying to achieve a simultaneous fadeIn and animate effect, but the current code is not yielding the desired result. Any suggestions on how to solve this problem would be greatly appreciated. Please help!

<html>
 <head>
    <title>Test</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript" charset="UTF-8"></script>
<script src="animation.js"></script>
<link rel="stylesheet" type="text/css" href="req.css">
 </head>
 <body>
 <div class="one"><button class="b">Click Me!</button></div>
 <div class="two"></div>
 <div class="three">
    <div class="three1 anim" style="position:absolute;"></div>
        <div class="three2 anim"></div>
        <div class="three3 anim"></div>
 </div>
 </body>
 </html>

The jquery and css codes are as follows:


.one{
    height: 100%;
    width: 400px;
    background: rgb(241, 196, 15);
    float:left;
}
.two{
    height: 100%;
    width: 400px;
    background: rgb(230, 126, 34);
    float:left;
}
.three{
    height: 100%;
    width: 400px;
    background: rgb(231, 76, 60);
    float:left;
}
.three1{
    height: 100px;
    width: 350px;
    margin-left: 25px;
    margin-right: 25px;
    margin-top:15px;
    background: rgb(26, 188, 156);
    float:left;
    position:absolute;
    display:none;
}
.three2{
    height: 100px;
    width: 350px;
    margin-left: 25px;
    margin-right: 25px;
    margin-top:15px;
    background: rgb(46, 204, 113);
    float:left;
    position:absolute;
    display:none;
}
.three3{
    height: 100px;
    width: 350px;
    margin-left: 25px;
    margin-right: 25px;
    margin-top:15px;
    background: rgb(52, 152, 219);
    float:left;
    position:absolute;
    display:none;
}

JQuery:

$(document).ready(function(){
    $('.b').click(function(){
        $('.anim').fadeIn(2000);
        $('.anim').animate({left:'100px'});
    });
});

Answer №1

One way to accomplish this is by:

$('.anim').animate({'opacity':1,'left':'100px'},2000);

When performing animations on the same element, the second one will be queued, preventing both from running simultaneously.

Answer №2

When using fadeIn and animate together, they do not run simultaneously but instead queue up. If I were starting from scratch, I would approach this differently. To adjust your code easily, follow these steps:

  • Remove the display:none from the CSS
  • Replace it with opacity:0
  • Update your jQuery code as shown below

jQuery:

$('.anim').animate({left:'100px', opacity: '1'},2000);

Check out the amended version of your code on this jsFiddle:

http://jsfiddle.net/Uj5rp/

One more thing to note, there is a typo in the position: absolute CSS style. It's also advisable to minimize the use of both external stylesheets and inline CSS for better style management.

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

The synchronization issue between ng-class and animation

I'm experiencing a strange issue with ng-class and suspect that it may be related to a race condition. Here is the example on Plunker: example Below is the relevant JavaScript code: self.slideLeft = function() { if (self.end_index < se ...

Tips for retrieving a single value from AJAX response data

I'm facing an issue where I am unable to retrieve a specific value from the data returned by an ajax call in order to make a decision. Below is my jQuery code that calls a PHP file. var displayMsg = $.ajax({ url:"displayMessage.php" }); displ ...

Bootstrap dropdown feature automatically rearranges all items in my navigation bar

I recently set up a navbar for my website, but I'm struggling to grasp the concept of positioning items with Bootstrap. One issue I encountered is that when I click on the dropdown button (profile image), it causes all the items in my navbar to shift, ...

What setting should I adjust in order to modify the color in question?

Looking to Customize Radar Chart Highlighted Line Colors https://i.sstatic.net/PqWc4.png I am currently working on a Radar Chart and I am trying to figure out which property needs to be edited in order to change the color of the highlighted lines. I have ...

Adjusting the size of the text input without changing the padding

How can I increase the font size to 1rem without changing the height of an input text field that has a padding of 1rem? The goal is to maintain the same height for the input field. Any ideas on how to achieve this? This is my current HTML structure: < ...

Tips for creating a mirrored iframe without the need to load the URL twice: CSS does not trigger the desired effect

I have a unique requirement for two iframes - I want only the first iframe to load a specific URL. Once the first iframe finishes loading, I need the second iframe to display the same content without actually loading the URL again. Here is the approach I h ...

AJAX function failing to trigger for the second time

I'm currently facing an issue with my JQuery AJAX function. It's supposed to populate a dropdown in a partial view based on the user's selection from another dropdown menu. The first part of the function is working as expected but the secon ...

Utilizing Codeigniter Ajax to return multiple rows in an ajax call, implementing two conditions (AND) in the SELECT query within the model file

When I click on the 'invoice number' in my 'purchase list', I want to open a collapsible table that will display the list of products matching the invoice number. To achieve this, I am utilizing jQuery to create the collapsible table an ...

Utilize jQuery selectors to target elements on a web page that has been stored in a variable

Currently, I am in the process of developing a Greasemonkey script that involves using JQuery. As part of this project, I am loading multiple pages into variables and successfully retrieving them using $.get. One challenge I have encountered is figuring o ...

Ways to avoid extra function loads in JavaScript

I'm currently working on an instant search feature and have some code implemented: $(function() { var timer; $("#searchterm").on('keypress',function() { timer && clearTimeout(timer); timer = setTimeout(doStuff, 800); tim ...

Safari experiencing a problem with the CSS mix-blend-mode: difference

I have been experimenting with a mouseover effect using mix-blend-mode. The effect is functioning as desired in Chrome and Firefox. .btn-anim { position: relative; display: inline-block; overflow: hidden; padding: 1.25rem; color: #fff; --x: 6 ...

Is there a way to verify the presence of an image using the alt attribute?

If I wanted to add another image to this list, how can I check if the alt text already exists in the list using jQuery? <img src="1.jpg" alt="apple"> If the alt text "apple" is already in this list, then I don't want to do anything. Is there a ...

Concealing specific HTML elements with ng-view in AngularJS

I recently started a project in AngularJS and I'm utilizing ng-view with $routeProvider for templating. However, I've encountered a minor issue where I don't want the navbar to display on specific pages like the landing, login, and registrat ...

Spin and flip user-submitted images with the power of HTML5 canvas!

I am currently working on a profile upload system where we are implementing image rotation using the HTML5 canvas object with JavaScript. Although the uploaded image is rotating, we are facing issues where parts of the image are being cut off randomly. So ...

Utilizing the power of Datatables through AJAX calls with HTML Forms

Hello, I am currently working on incorporating djangorestframework-datatables with the datatables' JQuery plugin. My task involves loading a large table (consisting of approximately 15000 entries, paginated) with the serverSide option enabled. Enabli ...

Chrome is experiencing a delay in closing the Select Option dropdown menu

On my webpage, I have two select option buttons placed on different tabs. Whenever one button is changed, I want to update the value of the other button and assign it to a specific element like a span in the example code provided. While everything works sm ...

Solutions for concealing the current div when clicking on a new div that reveals a fresh one

Is there a way to hide the current div once you click on a new div that reveals another one? The code below toggles the display of the div, but what I am attempting to achieve is that when you click on a new item after clicking on the first one, the first ...

What is the proper way to include enctype in my jQuery post request?

I've attempted to include it in my code, but it's not functioning properly. Even though all my form values are serialized and submitted through ajax, I can't seem to post my file name because it needs the enclosure type to be included. subm ...

Issue with Custom Dropdown input not triggering blur event

I've been working on a custom dropup component and have encountered some strange issues with it. The main problem is the blur event not firing when clicking outside of the input field. My goal is to hide the options elements on blur, reset the compon ...

How can we restart jquery if both functions fail to execute within 5 seconds?

I'm in the process of incorporating a timeout or reset into this section of jQuery code. <script type="text/javascript> var $msgNav = $('.nav4'); $msgNav.on('click', handler); function handler() { $('.drop_down_ ...