Using jQuery to create animations in the ::after pseudo element

My HTML and CSS structure looks like this:

<div class="box">Box Content</div>

The CSS code is as follows:

    <style>
    @-webkit-keyframes widthresize {
        0% {
            width:10px
        }
        50% {
            width:50px
        }
        100% {
            width:100px
        }
    }
@-moz-keyframes widthresize {
        0% {
            width:0%
        }
        50% {
            width:50%
        }
        100% {
            width:100%
        }
}
@-ms-keyframes widthresize {
        0% {
            width:0%
        }
        50% {
            width:50%
        }
        100% {
            width:100%
        }
}
@keyframes widthresize {
        0% {
            width:0%
        }
        50% {
            width:50%
        }
        100% {
            width:100%
        }
    }   
    body {
        background-color: #333;
    }
    .box {
        color: #FFF;
    }
    .box::after {
        background: #FFF;
        content: '';
        position: relative;
        width: 0;
        height: 1px;
        left: 0;
        display: block;
        clear: both;
    }
    .widthanimation::after {
      -webkit-animation-name: widthresize;
      -moz-animation-name: widthresize;
      -o-animation-name: widthresize;
      -ms-animation-name: widthresize;
      animation-name: widthresize;
      animation-timing-function: ease;
      -webkit-animation-timing-function: ease;
      -moz-animation-timing-function: ease;
      -o-animation-timing-function: ease;
    }

    </style>

Finally, the jQuery snippet responsible for adding the 'widthanimation' class to the div:

jQuery(document).ready(function($) {
    $('div.box').addClass('widthanimation');
});

I am facing an issue where the animation specified in the CSS keyframes is not working when the 'widthanimation' class is added via jQuery. I am unable to modify my HTML markup. Any help or suggestions on how to achieve the desired animation with this setup would be highly appreciated. You can view the fiddle link here. Thank you.

Answer №1

The issue may be that the animation duration was not specified. You can define the duration like this:

.widthanimation::after {
    -webkit-animation: widthresize 1s ease;
    -moz-animation: widthresize 1s ease;
    -o-animation: widthresize 1s ease;
    -ms-animation: widthresize 1s ease;
    animation: widthresize 1s ease;
}

Check out the updated demo for reference.

Note that 1s represents 1 second. Feel free to adjust this timing according to your needs.

If you want the animation to end at the 100% keyframe, use animation-fill-mode: forwards:

Here is an example demonstration showcasing this behavior.

Answer №2

Instead of using CSS animation, have you considered simply using a transition effect?

https://jsfiddle.net/abc123def/

.content::before {
    background: #FFF;
    content: '';
    position: absolute;
    width: 0;
    height: 1px;
    left: 0;
    display: block;
    clear: both;
    -webkit-transition: width 1s ease-out; 
    transition: width 1s ease-out; 
}
.content.widthtransition::before {
    width:100%;
}

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

Pass an array from a script file in JavaScript to index.js within the Express framework

I've encountered a challenge in sending an array (or JSON object) from script.js to index.js, my express server file. I've explored various solutions such as passing the variable through an HTML file and then to another JavaScript file, utilizing ...

Tips for customizing the focus border of a TextField input with the Material-UI theme

Struggling to customize the TextField border color and width in my own theme using Material-Ui v.5.4.0. After hours of research, I couldn't find a solution that works. It made me wonder if it's even possible to achieve this customization within t ...

What is the best way to arrange an array in either javascript or typescript based on a specific key, and in the case of identical keys,

Below is an array with objects that need to be sorted: [ { "Books": [], "_id": "5dea9a11a8e1bf301c462ce4", "FileName": "AAAAA", "Order": 99999 }, { "_id": "5dea9864a8e1bf301c462cdb", "Books": [], "FileName": "some1", ...

When it comes to form validations, I encounter an issue. I am able to view errors.minlength in the console, but for some reason, I am unable to access it

I would like the message "name is too short" to be displayed if last_name.errors.minlength evaluates to true. However, I encounter an error: Property 'minlength' comes from an index signature, so it must be accessed with ['minlength']. ...

"Encountering an issue with Asp.net:FullCalendar where events are not

Hey there! I've been trying to set up FullCalendar on my ASP.NET application by following some tutorials, but unfortunately, when I launch my application, the calendar isn't loading the events. Here's a snippet from my index.chtml file: @{ ...

An import error was encountered: The module 'react-router-dom' does not export the component 'Navlink'

import React, { Component } from 'react'; import { Navbar, NavbarBrand, Nav, NavbarToggler, Collapse, NavItem, Jumbotron } from 'reactstrap'; import { NavLink } from 'react-router-dom'; I have added the router using the follo ...

Deactivate Angular Container

Seeking a solution to a common question, I have devised a c# stackpanel component that arranges its contents vertically or horizontally based on user selection. The HTML code for this is as follows: <div #root [fxLayout]="this._orientation&quo ...

Exploring the process of implementing a filter search feature using Vue.js and JavaScript

Is it possible to create a filter search code using just data and method functions? I attempted to do the following: var fil = new Vue ({ el: '#app', data: { search: '', proL: [&a ...

Handle changes to input values on ng-click even if they were made outside of Angular's control

Looking to pass input values on ng-click even when they are changed outside of Angular. Everything works fine when manually typing, but once the inputs have dynamic values, ng-click passes empty form data. Below is the HTML code snippet: <form id="form ...

Linking a Wordpress submenu to a specific section of a page

I am utilizing a Wordpress menu to act as the primary navigation for my website. Each main item in this menu is a Page Type that redirects to other existing pages, with one of them containing submenus made up of custom links. These custom links' URLs ...

Transferring JSON information from JavaScript to PHP

I have searched for various solutions, but have not found one yet. I am stuck with the following piece of code and my suspicion is that the jsondata arriving at the PHP script is empty. Unfortunately, I am clueless on how to debug this issue as the script ...

Generating a JavaScript bundle using the npm TypeScript compiler

Is there a way to compile TypeScript source files into one JavaScript bundle file? We have developed a tool on node.js and are currently using the following TypeScript compilation code: var ts = require('typescript'); ... var program = ts.creat ...

Get immediate results in real-time

On a specific page, I have a form that I want to submit and receive a result upon submission. To display the returned data in a div, I've included this code: <script type="text/javascript"> $(function(){ $('#ff').form ...

Can the color scheme be customized for both light and dark themes in Ant Design version 5?

After creating a hook that successfully toggles between light and dark modes in Chrome's Rendering panel, I noticed that the values in the ConfigProvider remain unchanged when switching themes. Can anyone suggest a workaround to modify the design toke ...

Using an onclick function to increment and decrement values

Can anyone help me figure out how to reverse a function onclick? Here is the function: var theTotal = 0; $('button').click(function(){ theTotal = Number(theTotal) + Number($(this).val()); $('.total').text(theTotal); }); ...

Combining two functions into a single button: image changing

I'm currently facing an issue with assigning two actions to a single button. When I click on the image, it changes as expected. However, the action only triggers when clicking on the image itself and not on the button. Any thoughts on how I can modify ...

Using Jquery to fill a dropdown list with options obtained from a JSON array

After developing a PHP script, I encountered an issue where it returned a JSON array as shown below: var option = '[{"mod":"Width","values":{"field":"A","value":"96}}, {"mod":"Height","values":{"field":"B","value":"33}}]'; Here is a snippet of ...

The jQuery ajax post request to a webmethod in my C# code behind is functioning properly on localhost, but is not working as expected when live

While working on my project, I encountered an issue with a straight forward jQuery ajax post query to a webmethod in my C# code behind. The interesting thing is that it works perfectly on my local machine but fails when moved to the production server. Whe ...

The block seems to vanish partially when the CSS scale is applied

Is there a way to zoom in on a div by clicking on it without parts of the block being hidden after the zoom? Check out the demo here: http://jsbin.com/xumuzine/2/edit I've tried using the CSS property transform-origin: 0 0;, but this won't work ...

Combine the data of two JSON objects in JavaScript

Looking to merge two JSON objects in node.js that have the following structure: { "total_count":100 , "types":20 , { "a" : 5 , "b" : 6 }} {"total_count" : 1 , { "a" : 2 } } The goal is to combine these objects to create the following: { "total_count":1 ...