Spin and Return: CSS Animation with Rotation

I am attempting to create an animation with this arrow that will move up and down, as well as rotate or change its direction at 0% and 100%. However, my current issue is that the arrow rotates for a second at 100% and then reverts back to its previous direction. Essentially, I want the arrow to point up on its way up and point down on its way down. Is it possible to achieve this using only CSS animations?

DEMO

During the way up, the arrow should have this direction https://i.sstatic.net/SUr8J.png, and on the way down, it should look like this https://i.sstatic.net/nLhaA.png. I also intend to animate the rotation at 100% and 0% using rotate if feasible.

Answer №1

Switch up the animation using the code snippet below

@keyframes twistAndFlip {
  0% {
    transform: rotate(0deg); top:-25px;
  }
  20% {
     transform: rotate(45deg); top:-15px;
  }
  40% {
     transform: rotate(90deg); top:-5px;
  }
  60% {
     transform: rotate(135deg); top:5px;
  }
  80% {
     transform: rotate(180deg); top:15px;
  }
  100% {
    transform: rotate(225deg); top:25px;
  }
}

To see it in action, check out this demo HERE is the Demo link

Answer №2

To simplify the animation, consider eliminating the alternating direction setting and adjusting the keyframe settings accordingly. For the first 50% duration, focus on the upward movement and spin, then transition to a downward movement with the arrow pointing downwards for the next 50%. To achieve this without altering the entire animation, double the animation-duration, as the alternate effect is embedded within the keyframes themselves.

However, keep in mind that this adjustment may impact a part of your current animation where the arrow spins both upwards and downwards. By removing the alternate property, you eliminate the reverse rotation at 100%. To address this, add an extra keyframe mirroring the initial position of the element (no rotation).

If you wish to maintain the spinning sequence before the downward movement, include additional keyframes as shown below:

  • The arrow ascends with the head facing up.
  • At the peak, it rotates 180 degrees to point downwards.
  • After completing the rotation, it returns to the original position while still pointing upwards.
  • It then rotates 180 degrees again, pointing downwards as it descends.
  • Finally, the arrow moves down with the head facing downward.
  ---  

.arrow {
  height: 50px;
  position: relative;
  width: 10px;
  background: black;
  margin: 100px;
  display: inline-block;
  animation: bounceAndRotate 2s infinite linear;
}

.arrow:before {
  content: " ";
  width: 10px;
  background: black;
  height: 35px;
  position: absolute;
  top: -10px;
  transform: rotate(50deg);
  left: -10px;
}

.arrow:after {
  content: " ";
  width: 10px;
  background: black;
  height: 35px;
  position: absolute;
  top: -10px;
  transform: rotate(-50deg);
  right: -10px;
}

@keyframes bounceAndRotate {
  0% {
    transform: translateY(0) rotate(0deg);
  }
  12.5% {
    transform: translateY(-10px);
  }
  25% {
    transform: translateY(-20px);
  }
  37.5% {
    transform: translateY(-30px);
  }
  45% {
    transform: translateY(-40px) rotate(180deg);
  }
  55%, 60%{
    transform: translateY(-40px);
  } 
  67.5%{
    transform: translateY(-40px) rotate(180deg);
  }
  90% {
    transform: translateY(0px) rotate(180deg);
  }
  100% {
    transform: translateY(0px);
  }
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div class="arrow"></div>

Answer №3

If you want to create an arrow effect, you can use the following CSS:

.arrow {
  height: 50px;
  position: relative;
  width: 10px;
  background: black;
  margin: 100px;
  display: inline-block;
  animation: bounceAndRotate 2s infinite linear alternate;
}

.arrow:before {
  content: " ";
  width: 10px;
  background: black;
  height: 35px;
  position: absolute;
  top: -10px;
  transform: rotate(50deg);
  left: -10px;
}
.arrow:after {
  content: " ";
  width: 10px;
  background: black;
  height: 35px;
  position: absolute;
  top: -10px;
  transform: rotate(-50deg);
  right: -10px;
}

@keyframes bounceAndRotate {
  0% {
    transform: translateY(10px) rotate(0deg);
  }

  20% {
    transform: translateY(-20px);
  }

  50% {
      transform: translateY(-40px) rotate(180deg);
  }

  60% {
    transform: translateY(-30px) rotate(180deg);
  }

  80% {
     transform: translateY(-20px) rotate(180deg);
  }

  100% {
    transform: translateY(0px) rotate(180deg);
  }
}
<div class="arrow"> </div>

For a complete demonstration, check out this Working Fiddle.

Edit:

To achieve a 360-degree rotation, you can make the following adjustments by removing the 'alternate' property:

.arrow {
  height: 50px;
  position: relative;
  width: 10px;
  background: black;
  margin: 100px;
  display: inline-block;
  animation: bounceAndRotate 2s infinite linear;
}

.arrow:before {
  content: " ";
  width: 10px;
  background: black;
  height: 35px;
  position: absolute;
  top: -10px;
  transform: rotate(50deg);
  left: -10px;
}
.arrow:after {
  content: " ";
  width: 10px;
  background: black;
  height: 35px;
  position: absolute;
  top: -10px;
  transform: rotate(-50deg);
  right: -10px;
}

@keyframes bounceAndRotate {
  0% {
    transform: translateY(10px) rotate(0deg);
  }

  20% {
    transform: translateY(-20px);
  }

  50% {
      transform: translateY(-40px) rotate(180deg);
  }

  60% {
    transform: translateY(-30px) rotate(180deg);
  }

  80% {
     transform: translateY(-20px) rotate(180deg);
  }

  100% {
    transform: translateY(0px) rotate(360deg);
  }
}
<div class="arrow"> </div>

Answer №4

Thank you for all the responses, but here is the solution I ultimately implemented

DEMO

 @keyframes bounceAndRotate {
  0% {
    transform: translateY(0px) rotate(0deg);
  }

  35% {
    transform: translateY(-195px) rotate(0deg);
  }

  50% {
    transform:translateY(-200px) rotate(180deg);
  }

  85% {
    transform: translateY(-5px) rotate(180deg);
  }

   100% {
    transform: translateY(0px)  rotate(0deg);
  }
}

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

Utilizing Material-UI for CSS styling in a live environment

My application has a 'dashboard' feature that utilizes material-ui. Within this dashboard, other apps are called and rendered. While this setup functioned smoothly during development, I encountered issues once switching to a production build. The ...

The Microsoft Booking feature is not being shown in the iframe

https://i.sstatic.net/QYNGI.png Instead of the booking website, this is what is being displayed. Below is the code snippet: index.js import React from 'react' const Appointment = () => { return ( <iframe src='https://outlook ...

Iframe form submissions

I am interested in accomplishing the following task. My objective is to submit a form to my booking engine and show the results on either a new page or within a Div element. Ideally, when the user clicks the submit button, it would display an Iframe or re ...

JavaScript is restricted from being accessed beyond the confines of the ui-view element

After coming across a problem similar to one previously dismissed on stack overflow, I decided to tackle it head-on. You can find the issue and attempted solutions at this link: Previous Problem and Solutions The full project solution is available on Gith ...

Is there an HTML tag that can contain a block of JavaScript code without being processed by the browser?

I am dealing with the code below: <!-- Facebook Pixel Code --> <script> !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; n.push=n;n.loaded= ...

Having trouble with my bootstrap carousel not functioning properly - issue with jQuery line causing the rest of the code to disable

I've been facing some challenges with this Bootstrap slider for quite some time now. Despite my best efforts and thorough research on the forum, I just can't seem to make it function properly. Here's the code snippet in question: <!DOCTY ...

The enigmatic discrepancy in height across various browsers

I have encountered an issue with the layout of my HTML document. Both the BODY and HTML elements are set to a height of 100%. Within these elements, I have two child elements - one with a fixed height of 100px and the other set to height:100%. However, th ...

Implementing ngClass with a dynamic ID in the URL condition

I am attempting to create an ngClass condition that adds an active class to a list element. Specifically, I want it to work for both the URLs '/companies' and '/company/:id'. The issue I am facing is that the current setup does not fun ...

Consistent column height across each row

Currently, I am utilizing bootstrap and jquery to accomplish a specific task. My goal is to select the first child class after all columns on the webpage. var one = $(".col-x .some-class") Afterwards, I aim to obtain the height of all elements with the . ...

Styling with CSS Variables and Transforming Elements

After conducting thorough research both on this platform and via Google, I have yet to find a solution to my query: "how can CSS variables be utilized in a CSS transform?" Within the HTML header, three CSS variables and their fallback values are declared: ...

Leveraging SVG filters for enhancing text with unique borders

I recently discovered a helpful tip on adding a background to text in SVG using a source. However, I am still unsure how to incorporate a border around the text. <svg width="100%" height="100%"> <defs> <filter x="0" y="0" width="1" ...

Firefox's handling of collapsing margins

Summary: Visit this codepen for an example that works smoothly on Chrome, but shows misalignment in Firefox. I have been working on a custom jQuery plugin that enhances a text input by adding a dropdown button on the left side. To ensure proper positionin ...

Attempting to extract only the text content from a specific div using XPath

I am currently facing a challenge in writing a script to extract the title element from a poorly coded webpage. The developer who created this website did not use any classes, only div elements. Below is a snippet of the source code I am trying to scrape: ...

Retain the dashes in their original positions while extracting numbers following each dash from an array using Regex

Someone from a different discussion helped me figure out how to extract the numbers from an array. However, I am now struggling to capture the numbers after the "-" dash. Let me illustrate what I have and put you in the same scenario. Here is the array co ...

Exploring the world of jQuery and Ajax: Experimenting with implementing a POST method through Ajax and retrieving the response in HTML

Hey guys, I'm currently attempting to set up a basic HTML post method using Ajax. Take a look at the code snippet below: <?PHP function fetchInstagramData($url) { $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => ...

Revamp the hues of numerous elements simultaneously using just a single click!

Is there a way to change the colors of various elements (footer, divs, text) background and text color at once using just one button? I attempted to use JavaScript buttons, but I'd prefer not having to name each individual object's button in orde ...

Chic and perfectly aligned stripe button design

I need assistance with centering and styling the Stripe checkout button labeled "update card." Additionally, I want to adjust the appearance of the card number input field to be normal but readonly. How can I achieve this without relying on Bootstrap' ...

Is it possible for the sum to turn into NaN in Bootstrap Table JavaScript?

I used bootstrap to create a table that links with phpmyadmin. I need to show the total current amount in the table, which should update when using the search function. However, I keep getting NaN as my result. Below is the relevant code: // PART OF CODE ...

What are the steps to integrate <br> in a JavaScript code?

I have recently started learning about web development and I'm facing a challenge with this implementation. var obj = [[{ name: "John", age: 30, city: "New York"}, { name: "Ken", age: 35, city: "New Orleans"}]]; ...

Using React.js to dynamically change a CSS class depending on a certain condition

I am trying to assign a CSS class to a div based on a specific condition. The issue I am facing is that it always takes the last condition. Below is the code snippet: When I run the code, I receive logos.length as 3 <div className= "${ (this.state. ...