What is the method for advancing the cursor to the next line in this animation?

I've created an animation that types out:

"[Your Name] blah blah"

with a typing effect. Now, I want the cursor to move to the next line and type out:

"[Your Father Name] blah blah"

I attempted to add <br>, but it types out both lines simultaneously instead of one after the other.

.typewriter h1 {
  color: black;
  font-family: monospace;
  overflow: hidden; /* Ensures content isn't revealed until animation */
  border-right: .15em solid orange; /* Typewriter cursor */
  white-space: nowrap; /* Keeps content on single line */
  margin: 0 auto; /* Provides scrolling effect as typing occurs */
  letter-spacing: .15em; /* Adjust as needed */
  animation: 
  typing 3.5s steps(40, end),
  blink-caret .75s step-end infinite;
}

/* Typing effect */
@keyframes typing {
  from { width: 0 }
  to { width: 100% }
}

/* Typewriter cursor effect */
@keyframes blink-caret {
  from, to { border-color: transparent }
  50% { border-color: orange; }
}
<div class="typewriter" class="username">
    <h1>
     [Your Name] blah blah
    </h1>
</div>

Answer №1

Here is a creative solution for achieving this effect:

To create the desired animation effect, you can toggle a class on your <div>.

  • The initial typewritten line is visible and has the class first typing

  • The second typewriter line is initially hidden with just the class second

A JavaScript file is used to check every 500ms if the first line's typing is complete by comparing widths:

if ($('.first').parent().width() - $('.first').innerWidth() <= 10)

This condition indicates that the child element's width is close to its parent's width.

Once the first line finishes typing (when it reaches the parent div's width), the class typing is added to trigger the animation for the second line. The class typing can be removed from the first line while ensuring the styling remains consistent.

var checking = 1;

function checkForChanges()
{
console.log($('.first').parent().width() - $('.first').innerWidth());
    if ($('.first').parent().width() - $('.first').innerWidth() <= 10) {
      //div is as big as parent
        $(".second").css("visibility", "visible");
        $(".second").addClass("typing");
        //$(".first").removeClass("typing");
    } else {
      // div is smaller/bigger than parent
    }
    
    setTimeout(checkForChanges, 500);
}

checkForChanges(checking);
.typewriter{
  width: 100%;
}

.typing {
  color: black;
  font-family: monospace;
  overflow: hidden; /* Ensures content is revealed only during animation */
  border-right: .15em solid orange; /* Typewriter cursor */
  white-space: nowrap; /* Keeps content on one line */
  margin: 0 auto; /* Provides scrolling effect */
  letter-spacing: .15em; /* Adjust spacing as needed */
  animation: 
  typing 3.5s steps(40, end),
  blink-caret .75s step-end infinite;
}

/* Typing animation */
@keyframes typing {
  from { width: 0 }
  to { width: 100% }
}

/* Typewriter cursor animation */
@keyframes blink-caret {
  from, to { border-color: transparent }
  50% { border-color: orange; }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

<div class="typewriter" class="username">
    <h1 class="first typing">
     [Your Name] blah blah
    </h1>
    <h1 class="second" style="visibility: hidden">
    [My Name] blah blah
    </h1>
    
</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

Is there a way to change the URL in the address bar without refreshing the page?

Is it possible to update the URL in the address bar without reloading the page? I came across 2 potential solutions: Option 1: Check out this link It involves using window.history.replaceState. But when I tried implementing it in my angularjs projec ...

Tips for adding content to a textarea with JavaScript without disrupting the editing history

I have a specific requirement where I want the user to be able to highlight text in a textarea, then press ctrl + b to automatically surround that selected text with stars. Here is what I envision happening: 1) The initial content of the textarea is: "he ...

Dimensions of images on Read the Docs (small image widths)

Using MkDocs along with the Read the Docs theme, I am facing an issue with small images not scaling properly on mobile devices. It seems like this problem is related to the CSS of Read the Docs theme, but I'm unsure how to override the default behavi ...

remain on the multi drop down page for a specified duration

I have created a dropdown menu with a second level drop-down page that is a form. Now, I want the second level drop-down page to stay open longer, so I have used the following JavaScript code: <script> var timer; $(".parent").on("mouseover", functio ...

CSS directives are not compatible with Internet Explorer 8

I implemented a registration form with CSS rules that highlight error fields with a red border when users submit incorrect or empty data. However, this functionality is not working in Internet Explorer. The red border appears correctly in Firefox and Safar ...

What could be the reason my HTML5 application is not working offline on my android device?

Hello, I am encountering a problem and I could really use your assistance in figuring out what I am missing. There is a webpage that showcases numerous HTML5 mobile/web applications which can be found at For instance, let's look at this app where yo ...

Getting the value of a JavaScript variable and storing it in a Python variable within a Python-CGI script

Is there a way to capture the value of a JavaScript variable and store it in a Python variable? I have a Python-CGI script that generates a selection box where the user can choose an option from a list. I want to then take this selected value and save it ...

Update: "Mui V5 - Eliminate collapse/expand icons in TreeView and reduce TreeItem indentation"

My current project involves Mui V5 and I am looking to customize the TreeView component. Specifically, I need to remove the collapse/expand icons as I want them to be integrated into the TreeItem label component on the left side instead of the right. Add ...

To make a JavaFX label stand out, simply prepend a vibrant red asterisk at the start of

I'm facing a challenge with my SceneBuilder project. I have developed a scene that consists of several labels, and I want to highlight some of them by adding a red asterisk at the beginning of their text. Unfortunately, I haven't been able to fin ...

Submitting information from a lone row within a table that is contained within a single form

Within my form, there is a table containing records generated by a for loop iterating through items in a list. Each record has a submit button that triggers an AJAX call to serialize and POST the form data to the controller. The goal is to only POST data f ...

Joomla CSS styling malfunctioning

I've been exploring the creation of an in line menu using Joomla 1.6 and CSS. The issue arises when Joomla removes <span class="dmenu"> from the document before saving, despite having all cleanup options turned off. This led me to try a couple ...

Angular - ui-router states are not being detected

I'm currently working on a Spring and Angular JS web application project. The structure of the project is as follows: app.state.js (function() { 'use strict'; angular .module('ftnApp') .config(stateConfig); stateConfig. ...

Revamping the Bootstrap admin template

Is there a way to customize this Bootstrap 3 admin template? https://getbootstrap.com/docs/3.3/examples/dashboard/ I want to make some changes like removing the top fixed navbar and shifting everything up by 50px (as defined in dashboard.css). However, I ...

Tips on avoiding the accumulation of event handlers when dealing with click events triggered by the document selector in jQuery

I am currently working on a project that involves using AJAX to load various pieces of HTML code. This is done in order to properly position dynamic buttons within each portion of the HTML content. In my case, I need to trigger click events on the document ...

Conditional rendering is effective for displaying a form item based on certain conditions, but it may not be as effective for

I want a textarea element to appear or disappear based on the selected state of the radio buttons before it. If "no" is chosen, the textarea will be hidden, and if "yes" is chosen, the textarea will become visible. <fieldset class="input-group form-che ...

The browser is only showing particular changes made to the CSS file

I'm currently working on a web project and have the following CSS and HTML code: style.css: body { background-color: red; font-size: 14px; font-family: Roboto,arial,sans-serif color: gray; } img { height: 92px; width: 272px; ...

When the mouse is clicked, the character fails to reach the intended destination or moves in the wrong direction on the HTML canvas

UPDATE: RESOLVED I am currently working on a game where the character moves by right-clicking. The character is meant to walk slowly, not teleport, towards the destination set by right-clicking on the canvas. However, I have encountered an issue where the ...

Is window.getComputedStyle not functioning as anticipated?

Here is a function I created to determine the width and height of a text: function size_calculation(word,fontSize) { const div = document.body.appendChild(document.createElement('div')); div.textContent = word; div.style.cssText = ` fo ...

The HTML navbar seems to have a mind of its own, disappearing and shifting around unpredictably as I zoom in or resize

WHEN DISPLAYING THIS CODE, IT IS ADVISED TO VIEW IN FULL PAGE MODE This code includes navigation, header, and styling elements. However, when the browser is zoomed in or out, the navbar tends to move around and eventually disappears off the screen if wind ...

Set the height of a div based on the height of another div

My challenge involves a textarea that dynamically expands as content is added to it. The structure of the textarea within a div element is illustrated below: <div id='sendMes' class='container-fluid'> <form action='#&apos ...