Using if-else statements in jQuery when animating a webpage

While jQuery is running the .animate function, is it possible to interact with it? I have an animation that moves an image from bottom to top, but I need it to scale down when it reaches more than 70% of its distance traveled.

Imagine a bottleneck effect where the image needs to scale down as it gets closer to the top. But I'm not sure how to achieve this effect while the animation is running.

For example:

Action 1: Move 50% -- no problem

Action 2: Move 90% -- currently in progress -- but at 70%, scale the image down to 50%

EDIT

Thank you all for your suggestions, but unfortunately, I am still experiencing an offset issue between the circle and the water in my animation:

Take a look on CodePen.io

Answer №1

Since no one else seems to be providing a response, I'll give it a shot.

You have the option of using either the step method or $.Animation. Both methods are quite similar in function. Below is an example using the latter approach. For examples on how to utilize the step method, you can easily find them here on SO.

var elem  = $('#some_element'),
    width = elem.width();

$.Animation( {left: elem.position().left}, 
    {
        left: 500
    }, {
        duration: 2000
    }
).progress(function (e) {
    var start  = e.tweens[0].start, // starting position
        end    = e.tweens[0].end,   // ending position
        now    = e.tweens[0].now,   // current position
        lapsed = Math.ceil(now / end * 100); // calculate percentage

    elem.css('left', now);

    if (lapsed > 70) { // check if 70% progress has been achieved
        elem.width(width + ((lapsed-70)*2));  // animate the width as well
    }
});

FIDDLE

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

Dynamic setting of field values in Django based on the values of other fields

My goal is to automatically set a default value for a field in Django based on the selection of another field. In this case, the field that needs the default value is linked to a foreign key. class Product(models.Model): description = models.CharF ...

How to delete the last item of an array in AngularJS using scope

In my Angular controller, I have an array and a method for deleting objects. function($scope, $http){ $scope.arrayOfObjects = []; $scope.remove = function(obj){ var i = $scope.arrayOfObjects.indexOf(obj); if( i > -1 ){ ...

Tips for displaying specific HTML elements in AngularJS using ng-repeat and ng-if

I am working with some bootstrap HTML code that includes an ng-repeat function. <div class="row"> <div class="col-lg-4" ng-repeat="p in persons"> <img class="img-circle" src="images/{{ p.image }}" width="140" height="140"> < ...

How can I center my dynamic CSS3 menu with varying widths?

I am facing an issue with my css menu where the width is set to 400 for non-Admin users and 500 for Admins. The problem arises when I center the menu for Admins by setting the UL width to 500, as it appears off-kilter for non-admin users whose menu is only ...

Error: Unsupported Media Type when attempting to send JSON data from an AngularJS frontend to a Spring controller

Below is the controller function code snippet @RequestMapping(value = "/logInChecker", method = RequestMethod.POST, consumes = {"application/json"}) public @ResponseBody String logInCheckerFn(@RequestBody UserLogData userLogData){ Integer user ...

The color scheme detection feature for matching media is malfunctioning on Safari

As I strive to incorporate a Dark Mode feature based on the user's system preferences, I utilize the @media query prefers-color-scheme: dark. While this approach is effective, I also find it necessary to conduct additional checks using JavaScript. de ...

Error: The Gravatar service is unable to retrieve the property 'get' from an undefined source

I have a basic app with just one controller. I'm trying to debug the output of the Gravatar.get() function in the console. However, I encounter an error saying: "TypeError: Cannot read property 'get' of undefined". I'm confused because ...

Encountering an issue while running a query in Golang using the MySQL driver

I have a function that runs queries on a MySQL database. I am using the package "github.com/go-sql-driver/mysql" However, when I execute the following code: ... err := database.ExecuteSql("INSERT INTO xxx(field1, field2, field3) VALUES(?, ?, ?)", "field1" ...

The issue with MaterialUI Select's set value is that it consistently falls outside the expected

I'm currently working on a MaterialUI Select component where I am dynamically handling the value parameter. However, I'm facing an issue where even though I set a valid value from the available options, it always shows as out of range. SelectInp ...

Show a variety of logos on the website based on the current date

I've been experimenting with displaying a unique logo on my website based on the current date (for example, showing a Christmas-themed logo during December). Here's what I have so far: <img class="logo" id="global_logo" sty ...

Is it possible for someone to assist me in making my map stay in a fixed position?

How can I make sure that my map stays in a fixed position on the screen even when the user scrolls? Here is the code snippet I am using: #map { float: right; height: 545px; width: 40%; margin-top: 12px; margin-right: 30px; posit ...

"Enhancing User Experience: Dynamically switch between tab content in Angular UI Tab

In my angular app, I am using the ui-tab feature. There are 2 tabs (tab1 and tab2) and I need to programatically set tab1 as active when a button in tab2 is clicked. Here is the HTML code snippet: <uib-tabset active="active" justified="true"> ...

Techniques for adding values to arrays in JavaScript

My Anticipated Result let Album = { album_desc: AlbumTitle, id: 1, album_title: 'ss', AlbumDescription: 'sdsd', ImageName: 'image.jpg', album_pics: below array }; I am looking to dynamically p ...

Storing a collection of strings in a mongoose schema: A step-by-step guide

I am working with a user schema that looks like this: const UserSchema = mongoose.Schema({ username: { type: String, required: false }, social: [{ facebook: { type: String, required: false ...

Oops! There was a validation error as the duration was not formatted as a number. Please make sure to provide a valid numerical value for the duration field

When attempting to update data from a MongoDB database and checking the results on Postman, an error is encountered. The error reads as follows: "Error: ValidationError: duration: Cast to Number failed for value \"NaN\" at path \"duration&bs ...

Creating an identifier for the jQuery slideToggle function involves assigning a unique class or ID to

I am currently working on creating an identifier for the jQuery slide.Toggle function. In my PHP code, I have a loop that prints values, each with a button and a div that should be able to slide toggle. So far in PHP, here is what I have attempted, withou ...

What is the method for inserting line breaks in Django at particular characters?

How can I add line breaks after specific characters in my text? Check out this image: https://i.sstatic.net/bz1h1.png I need to insert line breaks after every 50 characters in the text shown in the image. Take a look at my HTML FILE: {% extends ' ...

What is the purpose of using *//<![CDATA[* and *//]]>* in a jQuery code snippet?

Check out this JavaScript code snippet that I have: <script type="text/javascript> //<![CDATA[ jQuery(document).ready(function() { jQuery("#page_template option[value='sidebar-page.php']").remove(); }); ...

Tips for altering the checked status of a radio input in a React component

I'm working with input type radio buttons const AnswerOptions = (props) => ( <> <input type="radio" id="something" ...

Clear out any unnecessary white space beneath the content on your webpage

I am relatively new to Web Development and currently following Colt Steele's udemy course. I have been working on a landing page, but as I try to set the height of HTML to 100%, it leaves whitespace at the bottom. After some research, I suspect that t ...