Varied animation response for the secondary background image

Attempting to design a sidebar menu with CSS animations and striving to achieve the maximum without relying on JavaScript or JQuery. However, if necessary, using JS / JQuery is acceptable.

The challenge at hand involves an <span></span> element with two background images that require different animation behaviors. The first image should rotate and increase in size based on the span's dimensions, while the second image should remain static in size but only appear after the animation finishes.

Is it feasible to have completely distinct animations for each background image within one element? If so, how can this be accomplished?

Below is the current code structure:

The list utilizes an unordered list format where each <li></li> represents a menu item

<ul>
    <li>
        <a class="menu-item-link" href="#"><span class="menu-item">1</span>Active or hovered Menu</a>
    </li>
    <li><li>
</ul>

CSS styling with animation keyframes included:

.menu-item-wrapper {
  width: 500px;
  clear: both;
}

.menu-item-link {
  margin-left: 5px;
  color: white;
  text-decoration: none;
}

.menu-item {
  float: left;
  color: white;
  text-align: center;
  padding-top: 5px;
  display: block;
  width: 30px;
  height: 30px;
  background-image: url("hexagon.svg"), url("mars.png");
  background-size: contain;
  background-repeat: no-repeat;
}

.menu-item:hover {
  animation-name: menuAnimation;
    animation-duration: 0.5s;
    animation-fill-mode: forwards;
    color: transparent;
}

@keyframes menuAnimation {
    from {
        width: 30px;
        height: 30px;
        transform: rotate(0deg);
    }

    to {
        width: 50px;
        height: 50px;
        transform: rotate(210deg);
    }
}

Here is a link to view the layout along with the code on a Fiddle: Fiddle Link

Lastly, here is an illustration of how the result should look after the animation completes (with the planet image and linked text visible):

https://i.sstatic.net/uvpsY.png

Answer №1

Could this be what you're looking for?

.menu-item-wrapper {
  width: 500px;
  clear: both;
}

.menu-item-link {
  margin-left: 5px;
  color: white;
  text-decoration: none;
}

.menu-item {
  float: left;
  color: white;
  text-align: center;
  padding-top: 5px;
  display: block;
  width: 30px;
  height: 30px;
  position: relative;
}

.menu-item::before {
  content: "";
  background-color: green;
  background-image: url("hexagon.svg"); /*added bg color because of the missing image*/
  background-size: contain;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  left:0;
  width: 30px;
  height: 30px;
  transition: all 0.5s ease;
}
.menu-item::after {
  content: "";
  background-color: red;
  background-image: url("mars.png");
  background-size: contain;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  left:0;
  bottom: 0;
  right: 0;
  opacity: 0;
  transition: opacity 0s 0.5s ease;
}

.menu-item:hover::before {
  animation-name: menuAnimation;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  width: 50px;
  height: 50px;
  transform: rotate(210deg);
}

.menu-item:hover::after {
  opacity: 1;
}

https://codepen.io/zothynine/pen/GQQdLp

Answer №2

You can enhance the appearance of an element by splitting it into two parts and animating them separately with a pseudo element. Here's how you can achieve this:

body {
  background: black;
}

.menu-item-link {
  margin-left: 5px;
  color: white;
  text-decoration: none;
}

.menu-item {
  color: white;
  text-align: center;
  padding-top: 5px;
  display: block;
  width: 30px;
  height: 30px;
  background-image: url("https://openclipart.org/image/2400px/svg_to_png/247374/Mars.png");
  background-size: contain;
  background-repeat: no-repeat;
  position: relative;
}

.menu-item:before {
  content:"";
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index:1;
  background-image: url(data:image/svg+xml;utf8;base64,PD94bW.../*Base64 data*/);
  background-size: contain;
  background-repeat: no-repeat;
}

.menu-item:hover {
  animation-name: anime1;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  color: transparent;
}
.menu-item:hover::before {
  animation-name: anime2;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
}

@keyframes anime1 {
  from {
    width: 30px;
    height: 30px;
    transform: rotate(0deg);
  }
  to {
    width: 50px;
    height: 50px;
    transform: rotate(210deg);
  }
}
@keyframes anime2 {
  from {
    transform: rotate(0deg);
    opacity:0;
  }
  to {
    transform: rotate(-210deg);
    opacity:1;
  }
}
<div class="menu-item-wrapper">
  <a class="menu-item-link" href="#"><span class="menu-item">1</span>Active or hovered Menu</a>
</div>

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

Integrating secondary functionality into fullPage and Vue.js

I am encountering an error in VueJS that says app.js:11754Uncaught TypeError: (intermediate value)(intermediate value).bind is not a function. I want to trigger the isLoaded function inside fullPage. I have tried using 'this' binding but it' ...

I require assistance in adjusting my text to properly wrap around the div image

Struggling to make my text fit around the div on this web page. <div id="othermain"> Are you getting ready for your big day? Do you need alterations or remodeling done to your Wedding Dress, Bridesmaid Dress, or any other Bridal Wear? You're in ...

Encountering an issue following the update from Angular 8 to 12 - receiving the error message: "The 'controls' property is not present in the 'AbstractControl' type."

Previously, I had a fully operational code in Angular 8. Recently, I made the decision to upgrade from version 8 to Angular 12. The main feature of my project is a dynamic reactive form structured in a question-answer format. This form adapts based on the ...

Guide on utilizing the RapidAPI with Excel VBA

I am a newcomer to using RapidAPI. My goal is to extract real-time Cricket Scores from RapidAPI using Excel VBA, however, the programming language is not supported on the platform. I am wondering if there is a way I can directly view the json results thro ...

Interested in transferring an additional column value to the $scope.seriesSelected variable?

I've been delving into Timeline charts using the angularjs google chart API and have come across an interesting limitation. It seems that only 4 columns are allowed, with the extra column designated for tooltips. However, I have a specific requirement ...

Switching iFrame based on dropdown selection

Hello, I'm a beginner in the world of programming and I'm currently experimenting with creating a webpage. However, I've encountered a roadblock and could use some help. My current goal is to change the source of an iFrame to a specific link ...

Update the value of a Vue tree select component using JavaScript

I'm working on a school project using plain JavaScript and needed a tree view select with multiple layers. After extensive searching, I stumbled upon this tool. It's working smoothly, but the one thing that has me stumped is how to change its va ...

Tips for activating multiple CSS animations when scrolling

I am currently working on a project that involves multiple CSS animations. However, I am facing an issue where these animations only occur once when the page initially loads. I would like them to trigger every time the user scrolls past them, regardless of ...

Is It Possible to Extract a Hidden Document Value from a Web Browser Using IWebBrowser2 in LabVIEW?

After hours of combing through the internet, I've come up empty-handed in my quest to find information related to my current project. I've created an HTML document that gathers user data and stores it in a JavaScript array. This array is then com ...

Is the promises functionality respected by the Nextjs API?

Greetings, I hope all is well with you. I am currently learning NEXTJS and working with its API, but I have encountered a problem. When I click too quickly, the promises seem to get stuck or encounter issues. You can see the tests in action in this brief 3 ...

Triggering OnClick() more than once may result in the function failing to execute as intended

My dilemma involves a table of cells where clicking a cell triggers an event. I need to dynamically add cells, so I plan to call OnClick again on all rows. But when I do so for the second time, cells that have already had OnClick called twice stop firing a ...

Creating a horizontal scrolling section in your CSS and HTML to avoid line breaks

I have noticed that this question, or similar ones, has been asked multiple times on various platforms. My query is regarding preventing line breaks in floated items within a scrolling section. Specifically, I have a div where I desire horizontal scrolling ...

What is the best way to duplicate a text using a button in ReactJS?

Hey there, I'm working with an array and trying to figure out how to copy the text in a p element ({item.adress} => this is part of an array item) when a button is clicked. Could you lend me a hand? import classes from './CryptoBox.module.css& ...

Understanding the Document.ready function?

Recently, I came across some websites that follow this specific pattern: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(function (){...do some stuff with p ...

Assign specific CSS classes to elements based on the v-model value in Vue.js

Looking to create a dynamic table using Vue and trying to add a class to a row when a checkbox is checked. Each checkbox is associated with a specific value in an object of objects. View the code on Codepen. <tbody> <tr v-for="row in filter ...

What are the distinctions in how jQuery behaves when an element is assigned to a variable versus in Javascript?

I've noticed that when I assign a jQuery element to a variable, it doesn't match the element in a comparison. However, if I assign a JavaScript element, it does... test1 = $(".the_div"); console.log(test1 == $(".the_div")); // This logs false ...

producing base64 encoding that results in a blank image

I have some code that is supposed to get an image from a video using canvas. However, when I save this base64 code into an image, I end up with a black image. What could be causing this issue? Here is my JavaScript code: var input = document.getElementBy ...

Encountering an "undefined is not a function" error across all libraries

While working on ASP.Net MVC4, I have encountered an issue where I consistently receive the error message "undefined is not a function" when using jQuery functions with different libraries. Despite ensuring that every ID is correct and everything has bee ...

Overflow is causing interference with the scrollY value

I've been attempting to retrieve the scrollY value in my React app, but it seems to be affected by overflow-related issues. Below is the code snippet I used to access the scrollY value: import React from "react"; import { useEffect, use ...

Toggle between two different global SCSS styles using a selector

I am in the process of upgrading my company's AngularJS project to Angular 8 using ngUpgrade. This has resulted in a hybrid application with components from both AngularJS and Angular. Additionally, I am utilizing an angular material template that inc ...