Achieve the same CSS animation effect on the ::after pseudo-element using jQuery

After conducting extensive research, I have yet to come across a similar inquiry that provides a solution to my issue. However, I suspect this may be a common problem and could potentially be a duplicate.

I currently have an impressive CSS Underline animation in place when I hover over a piece of text. You can view the example here:

let options = ['Over Here', 'This Way', 'At Me']

$(document).ready(function () {
  let jobInterval = setInterval(() => changeText(), 3000)
})

function changeText () {
  let newText = options[Math.floor(Math.random() * options.length)]
  $('#change').text(newText)
}
#change {
    display: inline-block;
    position: relative;
    padding-bottom: 3px;
}
#change:after {
    content: '';
    display: block;
    margin: auto;
    height: 3px;
    width: 0px;
    background: transparent;
    transition: width 3s ease, background-color 3s ease;
}
#change:hover:after {
    width: 100%;
    background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <h1>
    <span>Look </span>
    <span id="change">Over Here</span>
  </h1>
</div>

https://jsfiddle.net/6b2cqrj7/7/

However, I am looking to achieve this effect without relying on hovering. My goal is to have a piece of text that changes every X seconds, with the underline animating for X seconds before the word switches and the animation restarts. Essentially, creating a timer-like bar.

I have experimented with various approaches, but due to the limitations of manipulating the ::after tag using JS/JQuery, I am struggling to figure out a solution. Since the animation is applied to the ::after tag, simply adding a new class won't suffice as I need to alter the CSS property for the transition to take effect. Or so I believe.

This marks my initial attempt at utilizing CSS animations, leaving me quite puzzled by this challenge.

Answer №1

Utilize CSS3 keyframes for achieving the desired outcome.

let options = ['Here', 'This Direction', 'Towards Me']

$(document).ready(function () {
  let jobInterval = setInterval(() => changeText(), 3000)
})

function changeText () {
  let newText = options[Math.floor(Math.random() * options.length)]
  $('#change').text(newText)
}
#change {
    display: inline-block;
    position: relative;
    padding-bottom: 3px;
}
#change:after {
    content: '';
    display: block;
    margin: auto;
    height: 3px;
    width: 0px;
    background: transparent;
    animation: myLine 3s ease infinite;
    transition: width 3s ease, background-color 3s ease;
}
#change:hover:after {
    width: 100%;
    background: red;
}

@keyframes myLine{
  from {width: 0; background: transparent;}
  to {width: 100%; background: red;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <h1>
    <span>View </span>
    <span id="change">Here</span>
  </h1>
</div>

Trust this will be beneficial

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

When clicking on the Bootstrap navbar after implementing the onclick functionality, the color changes to

I was having no issues with my navbar until I introduced an onclick function to it. Here is the code for my onclick function: $('.nav li a').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/ ...

What is the best way to iterate through an array and dynamically output the values?

I am facing an issue with a function that retrieves values from an array, and I wish to dynamically return these values. const AvailableUserRoles = [ { label: 'Administrator', value: 1 }, { label: 'Count', value: ...

receive the output of the inquiry

Here's the issue I'm facing: file accounts.controlles.ts import { requestT } from "src/service/request.api"; export const getaccounts = async () => { const response = await requestT({ method: "GET", ur ...

I am looking to ensure that the ul li a() elements remain hidden until I hover over the h2 tag, at which point they will be displayed

Insert your code here : <table cellpadding="0" cellspacing="0" align="center" class="TblGreen"> <tr> <td> <h2><a href="#" class="AGreen">Sweater</a></h2> <p>The coding business</p> ...

Check out the CSS media query for screens with a maximum width on larger devices

Is there a way to preview a specific div and see how it appears on mobile devices? The HTML code for the div is obtained from an external source, like this: var external_html='<div>Responsive div</div>' + '<style>@media ...

Guide to utilizing JSDoc within your local project

Objective My goal is to utilize jsdocs with npm in my project. Experience I am new to working with npm and its plugins. Recently, I came across jsdoc and attempted to incorporate it into my project without success. Attempted Solution Initially, I inst ...

Move and place, editing tools

Is it possible to create a drag-and-drop editor similar to the one found in the form editor on wufoo.com? ...

Modifying the color of a GIF animated image using CSS

I'm currently working on an Angular application and I have a transparent GIF Loader image that I downloaded. I've been trying to figure out how to change the color of the GIF image, but so far, I've only been successful in resizing it. I sea ...

Commitments do not come with a collections feature

Struggling to implement the collection method, it seems that I can't directly use db.collection as db is calling connectDB, which returns conn, my mongoURI, as a promise. How can I modify this setup to effectively utilize the collection method and sen ...

Using PHP's for loop to iterate through data and store them into arrays

Attempting to transmit data from JavaScript to a PHP file through an AJAX request, and then store each result in an array using PHP. queryData={"data":{"data_string":{"data":"medicine","default_field":"Content"}}} testArgument=0; $.ajax({ url:"test/q ...

I am a beginner in node.js and currently exploring the concept of asynchronous callbacks and how they function

When running the following code, "one" and "two" are displayed as output but "three" is absent. Can someone provide an explanation as to why "three" is not showing up in the output? Despite trying various methods, I am still unable to pinpoint the cause ...

How can I alter the appearance of the active nav tab in Bootstrap to display an underline?

I am currently utilizing the Bootstrap active tab pane, and it is displayed as shown below: https://i.sstatic.net/FJ4lt.png However, I want to customize it to look like this: https://i.sstatic.net/BUWpK.png Specifically, I aim to have the active tab un ...

Develop a dynamic context menu with jQuery that can be loaded dynamically using Ajax requests

I've integrated a context menu plugin for jQuery 1.8.11.js and am utilizing the "build" callback to dynamically generate the menu upon right-click. My goal is to load menu options dynamically when an item is clicked. The issue I'm facing is tha ...

Showcasing external API JSON data on Google Maps

My goal is to integrate remote API JSON data into Google Maps. Currently, my code successfully works with local JSON data that is within the same script. However, I want to populate the Google Map with remote API JSON data. I am using AngularJS Google Maps ...

What is the best way to conceal an unordered list menu?

Trying to create a menu using ul HTML, but encountering an issue. When the website is opened, the menu shows up by default, whereas I only want it to appear when a mouse hovers over it. Even after attempting to change the CSS class, the desired functionali ...

JSP Airplane Seating Arrangement

I have embarked on a project to create a cutting-edge Java web application dedicated to seat booking through a JSP seating plan. My approach involves utilizing a Boolean array to represent the 24 seats, with seat selection being done by clicking buttons on ...

I'm looking to extract various data from a SQLite table using the URL with ExpressJS. What's the best way to achieve

I need to access data from a SQLite database table that contains information on accounts, movies, and reviews. Specifically, the structure of the reviews-table is as follows: CREATE TABLE IF NOT EXISTS reviews ( id INTEGER PRIMARY KEY, authorId INT ...

Switch Between More and Less Text - Implement Smooth Transition on Shopify Using Javascript

I recently implemented a More/Less toggle button using resources from this website. The functionality is there, but I now want to add a smooth transition effect. When the user clicks on "read more," I would like the hidden content to gradually appear, and ...

Issue with switching iframes using WebdriverJS/ProtractortoggleClass is not functioning

For over a week now, one of my automation test cases has been stuck due to a lazy loaded iframe (the iframe is inserted into the DOM only after clicking a link). Here's an excerpt from my code: driver.get("my url"); driver.findElement(By.linkText(&ap ...

If you try to wrap an object with an anonymous function, you'll receive an error message

Consider the following straightforward example. (function() { var message = { display: function() { alert('hello'); } }; })(); When trying to implement message.display(); An error is triggered stating ReferenceError: message ...