Guide to repairing a CSS ball animation

I am attempting to create a simulation where a ball moves in the following pattern: bottom right -> top center -> bottom (/) left. However, my animation seems to be moving from top right -> bottom center -> top left (/).

#stage {
 height:300px;
 border:1px solid #000;
}
#ball {
  position:relative;
  width:50px;
  height:50px;
  border-radius:50%;
  background-color:red;
  animation:ball-move 2s infinite reverse linear 2s,
  color-change 2s infinite alternate 2s;
}
@keyframes ball-move {
  /* your keyframe styles go here */
  0% {
    top:50px;   
    left:50px;
  }
  50% {
    top:calc(100% - 50px); 
    left:calc(50% - 25px); 
  } 
  100% {
    top:0;
    left:calc(100% - 50px);
  }
}
<div id="stage">
  <div id="ball"></div>
</div>

Answer №1

Make adjustments to the keyframes. Only the "top" ones need modification.

#stage {
  height: 300px;
  border: 1px solid #000;
}
#ball {
  position: relative;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: red;
  animation: ball-move 2s infinite reverse linear 2s,
    color-change 2s infinite alternate 2s;
}
@keyframes ball-move {
  /* your keyframe styles go here */
  0% {
    top: calc(100% - 50px);   
    left: 50px;
  }
  50% {
    top: 0px; 
    left: calc(50% - 25px); 
  } 
  100% {
    top: calc(100% - 50px);
    left: calc(100% - 50px);
  }
}
<div id="stage">
  <div id="ball"></div>
</div>

Answer №2

#container{
            box-sizing: border-box;
            width:  400px;
            height: 190px;
            margin:  0 auto;
            border: 1px solid gray;
            background: #fefefe;
        }

        .ball{
            width: 50px;
            height: 50px;
            background: tomato;
            border:  1px solid crimson;
            border-radius: 50%;
            position: relative;
            animation:  bounce 2s infinite cubic-bezier(.5,.5,.5,.5) alternate;
        }

        @keyframes bounce{
        0%{
            top: calc(100% - 50px);
            left: calc(100% - 50px);
        }

        50%{
            left:  calc(50% - 50px);
            top: 0;
        }

        100%{
            top:  calc(100% - 50px);
            left: 0;
        }
    }
<body>
    <div id="container">
        <div class="ball"></div>
    </div>
</body>

I believe this code snippet can assist you in resolving your problem.

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

Enhancing HTML Documents with the Header Element

When HTML 5 was implemented, it brought along new semantic tags such as header and footer. I find myself puzzled on whether I should use the header tag directly or assign a class of "header". Which option is superior and why? ...

Utilize flexbox to create a list that is displayed in a column-reverse layout

I am facing a challenge in displaying the latest chat person in the 1st position (active) using Firebase. Unfortunately, Firebase does not have a date field which makes it difficult to achieve this. I have attempted converting the date into milliseconds an ...

What is the best way to set up a server-sent-events broadcast feature in totaljs?

Let's imagine this situation: Client1 and Client2 are currently in session1 Meanwhile, Client3 and Client4 are part of session2 My aim now is to send event "a" to all clients in session1 exclusively. I came across this example: https://github ...

Is it possible to place a div element from an aspx page into the div of an html page?

Currently, I am faced with the challenge of loading a specific DIV from an ASPX page into an HTML page. The ASPX page tends to load slowly, while the HTML page loads much faster. Therefore, I am attempting to display the DIV that takes time to load from t ...

Use jQuery to parse the JSON below and then showcase the information in an HTML table

Can anyone assist me with parsing the following JSON using jQuery and presenting it in an HTML table? I want to showcase the values of "key" and "doc_count" in an HTML table. Your help would be greatly appreciated. Please find the JSON data below: { ...

Ways to dynamically incorporate media queries into an HTML element

Looking to make some elements on a website more flexible with media rules. I want a toolbar to open when clicking an element, allowing me to change CSS styles for specific media rules. Initially thought about using inline style, but it turns out media rul ...

How much does it typically cost to implement external libraries into a project?

Here are some of the most commonly used front-end web development libraries: jquery-min.js (95.9 kB) angular.min.js (108.0 kB) bootstrap.min.css (113.5 kB) bootstrap-theme.min.css (19.8 kB) bootstrap-fonts (185.7 kB) bootstrap.min.js (35.6 kB) All in al ...

How to properly display an Angular Template expression in an Angular HTML Component Template without any issues?

When writing documentation within an Angular App, is there a way to prevent code from executing and instead display it as regular text? {{ date | date :'short'}} Many sources suggest using a span element to achieve this: <span class="pun"&g ...

Transforming a Bootstrap 4 HTML project into Angular 9

After stumbling upon a beautiful HTML template that caught my eye, I realized it was built using Bootstrap. Luckily, I came across tutorials on how to convert such templates into Angular 6 projects, making the process seem quite straightforward (like this ...

Animating elements within Firefox can sometimes cause adjacent elements to shift positions

Currently, I am working on a single-page website that utilizes keyboard-controlled scrolling. To achieve the desired scroll effect, I have developed a JavaScript function that animates divs. Here is the JavaScript code: var current_page = "#first"; func ...

Is there a way to make the fixed table header scroll along with the table body data?

I am facing an issue with my table where I have multiple columns. I have managed to fix the table header successfully, however, when I scroll horizontally through the table body columns, the header remains fixed and does not move accordingly. How can I res ...

Section content neatly framed by a stationary menu overlay

I created a menu structured like this: <div id="menu"> <a href="#p1">Paragraph 1</a> <a href="#p2">Paragraph 2</a> ... </div> and set it to have a fixed position: #menu { position: ...

Extracting the input data from a JSON source

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Testing AJAX Requests ...

Tips for assigning a standard or custom value using JavaScript

What is the best way to automatically assign a value of 0 to my input field when the user leaves it blank? Should I use an if statement { } else { } ...

Issue with CakePdf unable to properly render CSS styling

I am having issues with the CakePdf Plugin (CakePdf.DomPdf) as it is not recognizing my stylesheet. Despite copying my /View/Layouts/default.ctp to /View/Layouts/pdf/default.ctp, the pdf file generated does not reflect the styling. Could this be due to t ...

Welcome to the interactive homepage featuring multitouch authentication

I am looking to design a homepage that is interactive based on touch input. For instance, if the user uses 5 fingers to touch the screen, similar to the green circles in the image below, they will be redirected to a specific homepage. If they touch in a di ...

Incorporate zoom feature into the jQuery polaroid gallery

Currently, I am utilizing a jQuery and CSS3 photo gallery found on this website. My goal is to allow the images to enlarge when clicked, however, the method provided by the author isn't very clear to me, as I'm not an expert in jQuery. I attempt ...

Implementing a Class Addition Functionality Upon Button Click in AngularJS

I have a form that initially has disabled fields. Users can only view the information unless they click the edit button, which will enable the fields with a new style (such as a green border). I want to add a class to these fields that I can customize in m ...

My Testimonial Slider seems stuck in its current position and won't budge to the left

As an aspiring web designer, I took a template from CodePen and crafted a testimonial slider for my website. To seamlessly merge it with the background, I've been trying to shift the entire testimonial to the left. Despite attempting the float-left p ...

Reduce the size of a webpage by 10%

Have you ever noticed that when you press Ctrl+- on any browser, the page scales down to 90% of its original size? It actually looks nicer at 90%, so I want my webpage to default to opening at 90% instead of 100%. I've tried following common approach ...