Display a CSS animation upon hovering the mouse

I'm looking to create a unique animation that involves drawing angled and straight lines, along with displaying text from left to right upon hovering over a button. I am relatively new to this concept and would appreciate any guidance. Additionally, I'm wondering if there is a way for the text to remain visible after the animation completes.

Below is the code snippet that I have put together, which combines elements of various solutions found on Stack Overflow:

.skew {
  position: relative;
  margin: 100px;
  width: 0;
  height: 2px;
  background: #f00;
  transform-origin: 0 100%;
  transform: rotate(-45deg);
  animation: draw 0.5s linear;
  animation-fill-mode: forwards;
}

.line {
  position: absolute;
  left: 100%;
  top: 0;
  content: '';
  width: 0;
  height: 2px;
  background: #f00;
  transform-origin: 0 100%;
  transform: rotate(45deg);
  animation: drawLine 0.7s linear;
  animation-delay: 0.5s;
  animation-fill-mode: forwards;
}

.showText {
  animation: showText 2s;
  position: relative;
  top: -17px;
  left: 15px;
  opacity: 0;
}

@keyframes showText {
  0% {
    opacity: 0;
    transform: translateX(-20px);
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes draw {
  0% {
    width: 0px;
  }
  100% {
    width: 100px;
  }
}

@keyframes drawLine {
  0% {
    width: 0px;
  }
  100% {
    width: 100px;
  }
}
<div>
  <button class="menubtn">hover over me</button>
</div>
<div class="skew">
  <div class="line">
    <div class="showText">menu item</div>
  </div>
</div>

Answer №1

To make the text visible even after the animation ends, you will need to add or toggle a class on the div.skew element using JavaScript. Define the animation rules on that class or its child elements as shown below:

var button = document.querySelector("button.menubtn");
var skewElement = document.querySelector("div.skew");

button.onmouseover = function(){
  skewElement.classList.toggle("startAnimation");
}
.skew {
  position: relative;
  margin: 100px;
  width: 0;
  height: 2px;
  background: #f00;
  transform-origin: 0 100%;
  transform: rotate(-45deg);
}

.skew.startAnimation {
  animation: draw 0.5s linear;
  animation-fill-mode: forwards;
}

.line {
  position: absolute;
  left: 100%;
  top: 0;
  content: '';
  width: 0;
  height: 2px;
  background: #f00;
  transform-origin: 0 100%;
  transform: rotate(45deg);
}

.startAnimation .line {
  animation: drawLine 0.7s linear;
  animation-delay: 0.5s;
  animation-fill-mode: forwards;
}

.showText {
  opacity: 0;
  position: relative;
  top: -17px;
  left: 15px;
}

.startAnimation .showText {
  animation: showText 2s;
  animation-fill-mode: forwards;
}

@keyframes showText {
  0% {
    opacity: 0;
    transform: translateX(-20px);
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes draw {
  0% {
    width: 0px;
  }
  100% {
    width: 100px;
  }
}

@keyframes drawLine {
  0% {
    width: 0px;
  }
  100% {
    width: 100px;
  }
}
<div>
  <button class="menubtn">hover over me</button>
</div>
<div class="skew">
  <div class="line">
    <div class="showText">menu item</div>
  </div>
</div>

To ensure the text remains visible after the end of the animation, be sure to include animation-fill-mode: forwards on .showText, just like in the code snippet above.

Answer №2

To achieve the animation effect on hover, we need to set up a hovering event for the specific element using JavaScript.

Once the event is triggered, a function is called to display the animations as required.

For simplicity, I've created a parent div to contain all the animation elements, initially hidden from view.

Upon hovering, the CSS display property of the parent element is changed to block, revealing all the animated components.

To ensure that the text remains visible after the animation, the forwards animation property retains the final state of the animation.

var hvrbtn=document.getElementById("hvrbtn");
hvrbtn.onmouseover=()=>{
var anim=document.getElementById("anim");
anim.style.display="block";
};
.animated{
display:none;
}
.skew {
  position: relative;
  margin: 100px;
  width: 0;
  height: 2px;
  background: #f00;
  transform-origin: 0 100%;
  transform: rotate(-45deg);
  animation: draw 0.5s linear;
  animation-fill-mode: forwards;
}

.line {
  position: absolute;
  left: 100%;
  top: 0;
  content: '';
  width: 0;
  height: 2px;
  background: #f00;
  transform-origin: 0 100%;
  transform: rotate(45deg);
  animation: drawLine 0.7s linear;
  animation-delay: 0.5s;
  animation-fill-mode: forwards;
}

.showText {
  animation: showText 2s forwards;
  position: relative;
  top: -17px;
  left: 15px;
  opacity: 0;
}

@keyframes showText {
  0% {
    opacity: 0;
    transform: translateX(-20px);
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes draw {
  0% {
    width: 0px;
  }
  100% {
    width: 100px;
  }
}

@keyframes drawLine {
  0% {
    width: 0px;
  }
  100% {
    width: 100px;
  }
}
<div>
  <button class="menubtn" id="hvrbtn">hover over me</button>
</div>
<div class="animated" id="anim">
<div class="skew">
  <div class="line">
    <div class="showText">menu item</div>
  </div>
</div>
<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

Another component's Angular event emitter is causing confusion with that of a different component

INTRODUCTION In my project, I have created two components: image-input-single and a test container. The image-input-single component is a "dumb" component that simplifies the process of selecting an image, compressing it, and retrieving its URL. The Type ...

How come the overlay only appears the first time the button is clicked in JQUERY? What could be the issue here?

Why is my purple overlay with yellow text showing only once after the button is clicked, even though I have toggled the function? Check out my codepen to test the issue: https://codepen.io/cat999/project/editor/AEeEdg $('.animate-this').cl ...

JavaScript: When setting focus on a DOM element, the document.activeElement is not automatically updated

I am facing an issue where I need to manually set focus on two buttons consecutively. These buttons are jQuery-Objects stored in an array named pMenus. Below is the code snippet: function OpenSubMenus(pMenus) { pMenus[pMenus.length - 1].get(0).focus() ...

Is it possible for a website to instruct a user's browser to save the entire page locally?

Is it possible for a (single-page) website to instruct a user's browser to save the entire page locally? To provide some background: I am running a website on a server that bills based on bandwidth usage. Since the content of the site remains mostly ...

Can applications on Windows 8 operate using JavaScript, HTML5, and CSS3 that adhere to industry standards?

As a .NET developer, I tuned in to the keynote for the Build Event in Anaheim, California and had some questions regarding the new support for creating Windows 8 applications using JavaScript, HTML5, and CSS3. They showcased several examples and mentioned ...

Leverage CSS to manipulate image attributes

Is it possible to use CSS programmatically to set the attributes of my img tag? <span><img class="img-dollar"></img></span> <span><img class="img-royalty"></img></span> I am looking for a way to specify t ...

Integrate a scrollbar seamlessly while maintaining the website's responsiveness

I encountered an issue where I couldn't add a scrollbar while maintaining a responsive page layout. In order to include a scrollbar in my datatables, I found the code snippet: "scrollY": "200px" However, this code sets the table size to 200 pixels r ...

Complete layout photography using bootstrap 4

I'm looking to create a banner that adjusts appropriately for mobile and desktop display. Here's what I've tried: When it is displayed in mobile When it is displayed in desktop In my attempt, the text alignment was off even though I used ...

Use JavaScript or jQuery to implement the position absolute styling

I am currently working on a website where the navigation is aligned to the right side. However, I am facing an issue where the last menu item dropdown extends beyond the page because it is absolutely positioned to the left of its parent element. I am act ...

Pressing a shortcut key will direct the focus to the select tag with the help of Angular

I was trying to set up a key shortcut (shift + c) that would focus on the select tag in my form when pressed, similar to how tab works. Here is the HTML code for my form's select tag: <select id="myOptions"> <option selected> Opti ...

How can I use jQuery to set the color of every other row in a

I'm facing an issue where I want to use jQuery to set the color of alternate rows in an HTML table. However, every time I add a new row, the color of the entire table switches. Below is the JavaScript code snippet that I am utilizing: var alternate = ...

Execute a function only once when using it in conjunction with ng-repeat in AngularJS

I need a way to ensure that a specific function is only called once when used in conjunction with ng-if and ng-repeat. <td ng-if="!vm.monthView && vm.yearView=='contract-year'" nginit="vm.ContractYearBaselines()" class="baseline-data ...

retrieving data in tabular form from a website

Looking to extract a table from this website by clicking on the "National Data" option. While I could easily download it, my interest lies in learning web scraping techniques. Previously, I utilized Python's Selenium package to automate the process of ...

What can I do to stop my list items from dropping below the menu even when there isn't enough space?

I'm facing an issue with my menu and despite several attempts, I can't seem to resolve it. In Firefox, everything looks fine, but in other browsers, the last anchor tag inside my menu keeps collapsing underneath. I've tried various solutions ...

How can I update the background color of the specified element when the text changes without triggering a postback?

Seeking guidance on JavaScript as I am not very adept in it. We have a webpage where users upload .XLS/.CSV files and review the data before submitting it to our database. Users can edit cells in the uploaded document on our "review" screen before final su ...

Transmitting data via HTML anchor tags

Is it possible to send POST data using an HTML tag? I'm aware that scripting is usually required, but I've been attempting to achieve this without success. <a class="test" onClick="assign()"><img src='<?php echo $accounts[$i][&a ...

Shift icon position to the right using CSS

I designed a user interface that includes an icon, user name, and phone number. The CSS properties I applied to the image are as follows: float: left; position: relative; margin-left: 4%; When I increase the margin-left value, both the image and username ...

When using $http get, it returns a "not found" error, but the direct request

While attempting to utilize a webAPI built in ASP.net, I encountered an issue. When I try to retrieve data from the API using angular $http.get in the browser console, I receive an error message saying GET <--URL--> Not found. However, when I make re ...

When you hover over a Wordpress page, the entire text on the page will be underlined

Currently designing my own website using WordPress with the Elementor free plugin and Phlox theme. I've made some progress by completing a few sections which include text, buttons, and images. However, I'm encountering an issue where all the text ...

Create a CSS path that connects two points by drawing a line

My image is currently animating along a path, but I want to make the path itself visible. Here's the CSS code: div { width:10px; height:10px; background:red; position:relative; -webkit-animation-name:Player1; -webkit-animatio ...