Maintaining the integrity of HTML and CSS while implementing JavaScript can be challenging, especially when trying to achieve a fade-in effect letter by letter

I am attempting to create a fade-in effect on a website letter by letter, but I seem to be running into formatting issues with JavaScript. Can anyone spot the error in this code (see below)? You can view the current version deployed here - - where you can see the problem.

Thanks, Florian

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Results</title>
    <style>
        /* CSS styles here */
    </style>
    <script>
        // JavaScript script here
    </script>
</head>
<body>

<div class="results-container">
    <!-- HTML content here -->
</div>

</body>
</html>

This is what I have attempted - I expected the formatting (h1 tags etc. / list format) to remain intact, but as you can see, it appears to be messed up.

Best Regards

Answer №1

According to @RUKAclMortality, the solution was cloning :-)

        document.addEventListener('DOMContentLoaded', function() {
            const container = document.querySelector('.results-container');
            const clonedContainer = container.cloneNode(true); // Make a copy of the container along with its content
            container.innerHTML = '';  // Empty the container

            function typeNode(node, targetContainer) {
                if (node.nodeType === Node.TEXT_NODE) {
                    // If it's a text node, print each character one by one
                    let i = 0;
                    function typeCharacter() {
                        if (i < node.nodeValue.length) {
                            targetContainer.append(node.nodeValue[i]);
                            i++;
                            setTimeout(typeCharacter, 10);
                        }
                    }
                    typeCharacter();
                } else if (node.nodeType === Node.ELEMENT_NODE) {
                    // If it's an element node, clone it without its children
                    const clonedNode = node.cloneNode(false);
                    targetContainer.appendChild(clonedNode);

                    // Type the children of this node recursively
                    let childIndex = 0;
                    function typeChild() {
                        if (childIndex < node.childNodes.length) {
                            typeNode(node.childNodes[childIndex], clonedNode);
                            childIndex++;
                            setTimeout(typeChild, 10);
                        }
                    }
                    typeChild();
                }
            }

            typeNode(clonedContainer, container);
        });
    </script>

Answer №2

Check out this cool library called typed.min.js for a neat typing effect!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create a Typing Effect on Your Website</title>
    <style>
    /* CSS Styles Go Here */
</style>
</head>
<body>
    <div class="results-container" id="typewriter-content">
        <h1>Experience the Magic of Typing Effect!</h1>

        <h3>Funding Opportunities in Germany</h3>
        <ul>
            <li>
                <span class="highlight">Central Innovation Program for SMEs (ZIM)</span>
                <ul>
                    <li><strong>Objective:</strong> Funding innovation projects</li>
                    <li><strong>Funding Rate:</strong> Up to 45% of eligible costs</li>
                    <li><strong>Key Criteria:</strong> Innovation level, market potential, technological feasibility</li>
                    <li><strong>Website:</strong> <a href="https://www.zim.de/ZIM/Navigation/DE/Home/home.html" target="_blank">ZIM Website</a></li>
                </ul>
            </li>
            <!-- ... Add other funding opportunities in Germany here ... -->

        </ul>

        <h3>EU Funding Programs</h3>
        <ul>
            <li>
                <span class="highlight">Horizon Europe</span>
                <ul>
                    <li><strong>Objective:</strong> Research and innovation</li>
                    <li><strong>Funding Rate:</strong> Typically up to 100% of eligible costs</li>
                    <li><strong>Key Criteria:</strong> Scientific excellence, societal relevance, European collaboration</li>
                    <li><strong>Website:</strong> <a href="https://ec.europa.eu/info/horizon-europe_en" target="_blank">Horizon Europe Website</a></li>
                </ul>
            </li>
            <!-- ... Add other EU funding programs here ... -->

        </ul>

        <p>These programs have different requirements, deadlines, and selection processes. It's advisable to thoroughly research each program and consider seeking professional advice.</p>
    </div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/typed.js/2.0.12/typed.min.js"></script>
    <script>
        // JavaScript code for typing effect
    </script>
</body>
</html>

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

There was an error: TypeError - The function collectionGroup cannot be called on the object _firebase__WEBPACK_IMPORTED_MODULE_10__.usersCollection.doc

I encountered an issue described above. Can someone help me identify what went wrong? My goal is to display all documents in the 'hives' collection. Is the code snippet shown in the second image correct? Or do I need to make some modifications? S ...

How can I set a variable to the input value within the class of a different Component in React?

Currently in the process of developing a React-based damage calculator app. It's still in the early stages, so your patience is appreciated. The following code snippet shows how I am retrieving the input value for 'Atk'. This functionality ...

A guide to crafting a cross design using only CSS

Is it possible to create a cross design in pure CSS that overlaps an image inside a div? Or would I need to create a new image with the cross included? ...

Steps for Adding a class or Id to an Ext.Msg.alert box

Is there a way to customize the style of a specific Ext alert box without affecting all alert boxes? Can someone please explain how to assign a class or ID to an Ext.Msg.alert box? Ext.Msg.alert('Status', 'Changes saved successfully.' ...

Is there a way to arrange an array based on the product or quotient of two values?

I'm working with an array of posts, each containing data on 'views' and 'likes', along with the user IDs associated with those likes. My goal is to sort this array based on the like rate. However, my current approach seems to be i ...

Why isn't the sticky element moving up along with its sibling element when scrolling?

This is an additional query related to my previous question How can I make an element sticky but scrollable to its full (variable) height with a sibling element? The issue at hand is that the right sidebar does scroll to its full available content and th ...

Error: The function setDarkTheme is not defined

Every time I click on the Dark button to apply dark theme on my page, I encounter an error. It seems like the error lies in the onClick function of the button and I can't seem to figure it out. Error message App.js import { useState } from 'rea ...

Submitting a form automatically in React Native using Redux

Is there a way to automatically submit a Redux Form in React Native based on certain conditions being met? I attempted to do so below, but it resulted in a warning. The documentation provides an example for remote submitting, but it uses HTML form's ...

Choose and save data found between line breaks into a list

My current task involves selecting postcodes within line breaks and storing them in a list. However, due to the complexity of the web-page layout, I am encountering significant challenges. While I can successfully gather hospital names, extracting only the ...

Creating an Engaging Data Visualization: Blending Candlestick Insights with Line Graphs

I'm attempting to display a moving average on a candlestick chart, but I'm facing an issue where the path of the line is not fully appearing on the SVG canvas that I've created. I've searched through various posts to understand how to o ...

I am trying to verify my code, but the messages are not displaying under the username. They only appear under the password field. I am not sure where I have made a mistake

Having trouble with my login form validation using jQuery. Can someone help me fix it? $(document).ready(function() { $("#form1").validate({ rules: { username: { required: true, minlength: 6 }, password: { ...

The v-for loop seems to only update the last element instead of all of them, which is incorrect

How can I ensure that all 3 page links in the nav are displayed in 3 languages of user choice? Initially, the language change works fine on page load. However, after clicking on a language change link once, only the last link's language changes instea ...

Should alert boxes and loading windows be called from Controllers or Services?

As I work on developing an AngularJS (Ionic) app, I have come across many resources emphasizing the best practices in AngularJS development. One common guideline mentioned is to avoid using controllers for DOM interactions. I currently have calls to ioni ...

Why does ng-checked fail to function properly in AngularJS?

Why won't the checkbox get checked while in edit mode? Here's the HTML I have: <input type="checkbox" ng-model="overallfeedback" ng-change="collectFeedback(overallfeedbackElg, $index)" ng-checked="{{collectlistbla[$index]}}"> In the ng- ...

Finding the text content of a div element using react testing library

<a className="stats__back" href="./.."> &#129044; </a> <div className="profile-heading">ACCOUNT</div> <div className="profile-header" ...

Struggling to upload a file with multer in a Node.js and Angular.js app

I have been trying to upload a file in my node js + angular js application. I followed the steps provided in this Stack Overflow thread by defining a directive and a service in my controller. However, it seems like it's not working as my server keeps ...

Can I install more than one instance of Framework7 on the same device?

Currently, I am working on a project using cordova 6.2.0 and framework7 1.6.5. However, now I need to initiate a new project that will be based on cordova 7.1.0 and framework7 2.0.7. I am aware that there is version-manager-cordova-software [1] available ...

Implement an onscroll event that dynamically changes the background color to highlight each phase individually

I need help implementing an onscroll event that will change the background color of each phase. Can anyone provide suggestions on how to achieve this? Here is my HTML code: I have experimented with a few listener options, but haven't been successful ...

Unable to update the color of material icon using document.getElementById(item) method

if (document.getElementById(item).style.color == "grey") { document.getElementById(item).style.color = "red"; } <i class="material-icons" [ngStyle]="post.isLiked != null ? {'color': 'red'}: {'color': 'grey'}" id ...

Utilizing the power of AWS Lambda in conjunction with moment JS to generate unique

My current time zone is GMT+8, and the AWS region I am using is Singapore (ap-southeast-1). I am facing an issue where there are discrepancies in date calculations between my local machine and when I deploy my code on AWS Lambda. My goal is to ensure that ...