Tips for shrinking the circumference of a circle

Currently, I have a circular div that has been styled using CSS animations. My goal is to keep the size of the circle consistent when it moves to the bottom, but reduce its size when it bounces back to the top. I am uncertain if this can be achieved solely with CSS or if I will need to involve JS. Can anyone provide a solution or guidance on the best approach?

Link to JsFiddle Demo

Thank you in advance.

HTML:

<div class="hello"></div>

CSS:

.hello{
      width:100px;
      height:100px;
      border:5px solid black;
      border-radius:55px;
 }
 div {
      width: 100px;
      height: 100px;
      background-color: red;
      position: relative;
     -webkit-animation-name: example; 
     -webkit-animation-duration: 4s; 
     animation-name: example;
     animation-duration: 4s;
 }
 @keyframes example {
     0%   {background-color:red; left:0px; top:0px;}
     75%  {background-color:green; left:0px; top:200px;}
     100% {background-color:red; left:0px; top:0px;}
 }

Answer №1

Here are a few key points to keep in mind. Check out the JSFiddle provided below to see these concepts in action:

  1. If you want to shrink the size of a circle in your animation, simply add height: <whatever>px; and width:<whatever>px; to the final stage of the animation.
  2. To ensure that the circle only reduces in size during the last portion of the animation (when it bounces back up), make sure to specify that the height and width values remain the same at the 75% mark.
  3. Keep in mind that changing the size of the circle may cause a noticeable "jump" at the end of the animation. This is because CSS animations do not persist by default, so any alterations revert to the original state once the animation is complete.
  4. If you want the size change to remain in effect even after the animation finishes, use animation-fill-mode: forwards;.

I have made edits to your original JSFiddle incorporating the suggestions mentioned above. You can view the updated version Here.

Answer №3

To achieve the desired effect, adjust the width and height of the div within your keyframes.

@keyframes example {
    0%   {background-color:red; left:0px; top:0px;}
    75%  {background-color:green; left:0px; top:200px;width:100px;height:100px;}
    100% {background-color:red; left:0px; top:0px;width:50px;height:50px}
}  

Link to example

In order to animate the ball to a smaller size as it bounces back from the keyframe 75% to 100%, we decrease the width and height of the circle in the keyframe 100%. Conversely, to maintain the size of the ball until the keyframe 75%, we reapply height:100px and width:100px in that keyframe.

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

Is there a way to selectively transfer attributes and behaviors from an interface to a fresh object in typescript?

Is there a way in javascript to selectively copy properties from one object to another? I am familiar with using Object.assign() for this purpose. Specifically, I am looking to extract only the properties defined within the following interface: export in ...

Utilize CSS to position an image in four various locations on a web page

Expressing this may be a challenge, but I'll give it a shot. Within a div, there is text that can vary in length based on user input. My goal is to have the ability to insert an image in one of several positions, determined by the selection made in a ...

Deciphering JSON data in a textarea following a POST request

I am struggling with decoding a JSON string in PHP and I have identified the problem causing it. However, I am unsure of how to fix it. My approach involves putting an array that I convert to JSON into a hidden textarea, which I then post along with other ...

"Carousel indicator navigation malfunctioning in bootstrap slider with unresponsive click behavior

I'm currently working on modifying the code below. I've managed to separate the indicators from the bootstrap carousel. Everything seems to be functioning well, except for one issue - when I click on the list of indicators, although the image cha ...

Dilemma arises from conflicting javascript codes

Currently, I am developing a web application where the main page features a timeline that needs to update its content automatically. To achieve this, I am utilizing the setTimeOut function of JQuery to refresh the timeline every x seconds. In addition, th ...

Is it possible to implement a hover animation within another hover animation in WordPress?

Trying to incorporate two hover animations for the images in my photo galleries. When hovering over an image, I want a text to appear in a black overlay, and then when hovering over that text, it should become underlined. Looking for something similar to ...

Can scrollHeight ever be less than clientHeight or offsetHeight?

Is it guaranteed that the output of Math.max(el.scrollHeight, el.offsetHeight, el.clientHeight) will always be equal to el.scrollHeight? Typically, clientHeight <= offsetHeight <= scrollHeight. While there are rare instances where clientHeight > ...

The error form is not responding even after attempting to locate the issue

I have created a form and it was working fine, but now when I click on the submit or reset buttons, nothing happens. I've checked my code thoroughly line by line, but can't seem to find the issue. I even tried restoring previous versions, but no ...

Experienced an unexpected setback with the absence of the right-click capability on a Javascript-powered hyperlink, specialized for

I am facing an issue with a hyperlink on my website. This particular hyperlink submits a hidden form using the POST method to redirect users to another site. However, when someone right-clicks on this hyperlink and tries to open it in a new tab, they are o ...

JavaScript | Determining the coordinates of where on the image X and Y were clicked

My goal is to determine the position of an x and y location when a user clicks on an image. This information will be used to add a spot to the image that displays relevant information. The content for the spot will be dynamically loaded from a SQL database ...

The 'log' property cannot be found on the type '{ new (): Console; prototype: Console; }' - error code TS2339

class welcome { constructor(public msg: string){} } var greeting = new welcome('hello Vishal'); Console.log(greeting.msg); I encountered an error at the Console.log statement. The details of the error can be seen in the image attached below. ...

Changing the content of the initial post in a loop on WordPress

<?php $freeadvice=new WP_Query('category_name=freeadvice&showposts=10'); while($freeadvice->have_posts() ): $freeadvice->the_post(); ?> <li class="event"> <input type="radio" name="tl-group" /> ...

Error: Failed to decode audio content due to a DOMException that was caught in the promise

Encountering an issue with the decodeAudioData method while utilizing the Web Audio API for playback in Chrome (works seamlessly in Firefox)- Sending the audio buffer, which has been recorded by the media recorder, back from the server. Server-side wss ...

Implementing a dynamic navigation bar that expands and collapses on mouse hover in a React application

I have a side navigation bar on my webpage that I obtained from a React Bootstrap website, so the class names are already pre-loaded. Instead of using a toggle hamburger icon to expand or collapse the navigation, I want to achieve this functionality based ...

Styling the background of a navigation menu specifically for mobile devices using CSS

Currently, my focus is on developing the website I am using Elementskit nav builder and I aim to change the background color of the navigation menu on mobile and tablet devices to black The existing code snippet is as follows: margin-top: 6px; } @med ...

The indicated processing instruction is incompatible with the provided payment source. PayPal's hosted fields for credit card payments do not support this specific processor

I'm currently working on integrating credit card payments with hosted fields into my checkout process. However, I keep encountering an UNPROCESSABLE_ENTITY error when making the confirm-payment-source request through the PayPal JS SDK. Here is the co ...

Stop the iframe video when the modal is closed

I'm currently developing a website that incorporates the code showcased in this tutorial to launch a modal window featuring an iframe for playing YouTube or Vimeo videos. The issue arises when, as mentioned in the comments on the tutorial page, there ...

How can you efficiently send JSON data from Ajax to a Servlet in a Java environment?

I am encountering a perplexing issue with sending data from my jQuery script to a servlet using AJAX. The strange part is that when I set the contentType to application/json, all values on the server side turn out to be null. However, if I remove it, the s ...

Tips for resolving issues with dynamically displaying state information based on a selected country

I'm currently working on a project that requires me to dynamically fetch the states of a specific country when a user selects the country of birth for their family members. To do this, I am utilizing AJAX. Due to limitations, I can only include detai ...

Can you add descriptive text below a linked image?

I am currently working with a customized CMS created specifically for our company. My goal is to insert text below the image displayed. Ideally, I would like the text to be centered within the border of the image if possible. I have limitations in that I ...