Animating the Bookmark Star with CSS: A Step-by-Step Guide

I found this interesting piece of code:

let animation = document.getElementById('fave');
animation.addEventListener('click', function() {
  $(animation).toggleClass('animate');
});
.fave {
  width: 70px;
  height: 50px;
  position: relative;
  overflow: hidden;
}

.fave img {
  position: absolute;
  top: 0;
  left: 0;
  cursor: pointer;
  animation: test_animate_reverse 1s steps(55);
}

.fave .animate {
  animation: test_animate 1s steps(55);
  left: -3519px;
}

@keyframes test_animate {
  from {left: 0;}
  to {left: -3519px;}
}

@keyframes test_animate_reverse {
  from {left: -3519px;}
  to {left: 0;}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="fave"><img src="https://cssanimation.rocks/images/posts/steps/twitter_fave_rectangle.png" id="fave"></section>

The URL for the image in question is: (however, the images are displayed horizontally after modification).

Although the outcome seems well, I have some thoughts on it:

  1. Upon refreshing the browser window, my star always animates from the final frame back to the initial frame. I wish for it to only reverse-animation when toggling between 'active' and 'not active', rather than upon initial refresh.
  2. I believe using two identical @keyframes for reversing an animation is inefficient. Is there a way to achieve the same effect without creating a separate reverse @keyframes?
  3. If a section has no parent element, can I still specify its size without explicitly doing so?
  4. When rapidly clicking the image, I prefer the current animation to finish before starting the next one. Currently, previous animations abruptly end when a new one begins.

EDIT

I attempted to remove the reverse @keyframes by adjusting as follows:

.fave img {
    position: absolute;
    top: 0;
    left: 0;
    cursor: pointer;
    animation: test_animate .7s steps(55);
    animation-direction: reverse;
}

However, the animation disappeared entirely.

Answer №1

Have you considered using the code provided in the tutorial where the image was sourced? The tutorial uses the transition property instead of animation, resulting in a cleaner appearance.

Additionally, the animation will automatically reverse when the transition is applied to the element.

To prevent multiple clicks before the animation completes, you can set a disabled flag and utilize setTimeout().

var click_disabled = false;

$('.fave').click(function() {
  
  if (click_disabled) {
    return; // do nothing
  }

  $(this).toggleClass('faved');
  
  // Set correct aria-label 
  var label = $(this).attr('aria-label') == 'Favourite' ? 'Unfavourite' : 'Favourite';
  
  $(this).attr('aria-label',label);
  
  click_disabled = true;
  
  // Timeout value should match transition length
  setTimeout(function(){
  click_disabled = false;
  }, 1000);
    
});
.fave {
  background: none;
  border: 0;
  padding: 0;
  width: 70px;
  height: 45px;
  background: url(https://res.cloudinary.com/shanomurphy/image/upload/v1547543273/fave_ltre0q.png) no-repeat;
  background-position: 0 0;
  transition: background 1s steps(55);
  outline: 0;
  cursor: pointer;
}

.fave.faved {
  background-position: -3519px 0;
}
<button class="fave" aria-label="Favourite"></button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

What is the best way to extract a message as an object from a JSON data in discord.js?

Currently in the process of developing a discord bot with an election feature for my server's moderator. All the necessary election data is being saved in an external JSON file to ensure that if the bot crashes during an election, it can seamlessly re ...

Steps for generating a div, link, and image that links to another image using Javascript

Hey there, I have a cool picture to share with you! <img src="cards.png" id="img"> <!--CARD PICTURE--> Check out what I want to do with it: <div class="container_img"> <img src="cards.png" id="img"> <!--CARD PICTURE--> ...

Show variously colored information for each selection from the dropdown using Angular Material

I am trying to change the color of displayed content based on user selection in an Angular application. Specifically, when a user chooses an option from a dropdown list, I want the displayed text to reflect their choice by changing colors. For example, i ...

Can someone provide guidance on utilizing parentheses in HTML code?

I am facing an issue in my Angular project while using the WordPress API. The problem lies in the API address structure which includes "" like this: <img src="{{importantvideo.better_featured_image.media_details.sizes.["covernews-med ...

What is the best way to eliminate the gap above a block element using CSS?

Currently in the process of designing a website, I am faced with a challenging issue that seems to have me stumped. The structure of my page consists entirely of HTML DIVs, with certain elements nested within their parent DIVs. My main dilemma lies in tryi ...

Integrating redux-form with the react-widgets DateTimePicker component

I am currently working on a project using ReactJS with Redux and I am trying to incorporate a form similar to the one shown in this example: Most of the components are working well, but I am encountering an issue with the DateTimePicker due to the momentL ...

Submitting information from a single form to two separate URLs

Is it possible to send a post from one form to two different URLs simultaneously? For example, sending POST name data to both process.php and confirm.php at the same time. $(document).ready(function(){ $("#myform").validate({ debug: false, ...

Retrieve the Vue.js JavaScript file from the designated static directory

This is my inaugural attempt at creating a web application, so I am venturing into Vue.js Javascript programming as a newcomer. I have chosen to work with the Beagle Bootstrap template. Within my Static folder, I have a file named app-charts-morris.js whi ...

Finding the nth-level children using JQuery

Is there a way to select nth-level children in CSS/jQuery without using any ID or class references, only tag names? I know how to get first level children with the selector div#block > div. But I am looking for a solution to target deeper levels. & ...

Sending multiple values with the same name via AJAX using PHP

I'm facing an issue with my code: <form method="post" id="result-image" name="result-image" action="" > <?php $i=1; foreach ($data as $key => $datas) { ?> <div class="col-md-2 thumb custom-size col-lg-3 col-xs-4"> ...

Retrieve key-value pairs from a database and store them as variables in PHP before transferring them into an array in JavaScript

My challenge lies in loading Chinese characters as keys and their English translations as values from a database into a PHP array, so that I can use them on the client side in JavaScript. The process involves fetching key:value pairs from PHP into a JavaSc ...

Issue with image preview functionality in dialog modal when loading content via ajax request

<table id="result"> <a href='#' class='pop'><span class='fa fa-eye'><img src='image link here' style='display:none'></a> </table> The hyperlink above is designed to open ...

Utilizing conditional statements in React js to showcase a distinct component

I am looking to create a function or conditional statement that dynamically displays different components based on whether the user has purchased products. If the user has bought products, I want to display the product components; if not, I would like to s ...

Basic jQuery Slideshow Counter

I'm having trouble implementing a text-based horizontal slider on my website that scrolls left and right with mouse control. I want to display the current index of each slide along with the total number of slides (e.g. 1/4) and update the index as use ...

Tips for changing the color of text in the navbar with Bootstrap 5

I am currently learning the basics of Bootstrap for front-end web development. However, I am facing difficulty in changing the color of navbar text items to blue. I have tried several online solutions but none seem to work with the following code: <n ...

How can one make the image pop and stand out from its background?

I am trying to achieve a similar effect as shown in this design where the image stands out from its background. I have managed to do that, but I am struggling with aligning the image to the center and bottom with overflow. Can anyone help me with this? Th ...

Is there a way to modify the color scheme of the hamburger icon

I'm experiencing difficulties changing the color of a hamburger icon. I've searched for the specific code responsible for the color change, but I haven't been able to locate it. Ideally, I want the color to be white, but on small screens, i ...

When you click on the collapsible header background, the content collapses, but the main icon does not update

I am currently using collapsible content from Bootstrap and I am encountering an issue with the icon not changing when I click on the collapsible-header background. Here is a visual representation of the problem: 1st part (not collapsed) 2nd part (After ...

Guide to switching classes with jquery

On my webpage, I have a star rating system that I want to toggle between "fa fa-star" and "fa fa-star checked" classes. I found some information on how to do this here: Javascript/Jquery to change class onclick? However, when I tried to implement it, it di ...

The current Webpack configuration for production fails to account for importing CSS files

I am struggling to figure out how to properly load a static site that is not located in the root folder: let HWPTest = new HtmlWebpackPlugin({ template: __dirname + "/src/artists/test.html", filename: 'artists/test.html', favicon: &apos ...