Jump to content

Typewriter Effect - One Loop

Recommended Posts

Site URL: https://seadragon-apple-c697.squarespace.com/

Hello!

I've been wanting to add in a typewriter effect to a Squarespace 7.1 website that I'm working on.

I found a great guide by @tuanphan that I followed and I'm really happy with the resulting effect at the top of the homepage. Code below as a reference to the original solution.

It would be great to implement this code in other areas of the website; for example - section/product page sub-headers. The only issue is that instead of having the typewriter effect cycle through a series of words or sentences, am I able to have it do the typewriter effect on one sentence and instead of deleting and redoing it on a loop - just hold on the word with the blinking cursor once it has typed out? 

Additionally, is it possible to just have a linking cursor at the end of a word or sentence, without the typing effect itself?

I appreciate that this is a bit of a double ended one, but any help on this would be amazing.

Thanks!

 

Quote

<div class="container">
  <h1>
    typewriter01 |
    <!-- The words that we want to have typerwriter effect -->
    <span class="txt-type" data-wait="3000" data-words='["consultancy", "food", "creative tech", "ranj"]'></span>
  </h1>
</div>
<style>
/* CSS RESET */

/* ALIGN CONTENT */
.container {
  display: flex;
  /* Remove horizontal 'justify-content' center if you want the base text not to move */
  justify-content: center;
  align-items: center;
}

/* ADD CURSOR */
.txt-type > .txt {
  border-right: 0.08rem solid #fff;
  padding-right: 2px;
  /* Animating the cursor */
  animation: blink 0.6s infinite;
}

/* ANIMATION */
@keyframes blink {
  0% {
    border-right: 0.08rem solid rgba(255, 255, 255, 1);
  }
  100% {
    border-right: 0.08rem solid rgba(255, 255, 255, 0.2);
  }
}
  #page .section-background {background: white;}
  #page section * {color: black !important;}
  #page .content {
    width: 100%;
}
</style>
<script>
class TypeWriter {
  constructor(txtElement, words, wait = 3000) {
    this.txtElement = txtElement;
    this.words = words;
    this.txt = "";
    this.wordIndex = 0;
    this.wait = parseInt(wait, 10);
    this.type();
    this.isDeleting = false;
  }

  type() {
    // Current index of word
    const current = this.wordIndex % this.words.length;
    // Get full text of current word
    const fullTxt = this.words[current];

    // Check if deleting
    if (this.isDeleting) {
      // Remove characters
      this.txt = fullTxt.substring(0, this.txt.length - 1);
    } else {
      // Add charaters
      this.txt = fullTxt.substring(0, this.txt.length + 1);
    }

    // Insert txt into element
    this.txtElement.innerHTML = `<span class="txt">${this.txt}</span>`;

    // Initial Type Speed
    let typeSpeed = 50;

    if (this.isDeleting) {
      // Increase speed by half when deleting
      typeSpeed /= 2;
    }

    // If word is complete
    if (!this.isDeleting && this.txt === fullTxt) {
      // Make pause at end
      typeSpeed = this.wait;
      // Set delete to true
      this.isDeleting = true;
    } else if (this.isDeleting && this.txt === "") {
      this.isDeleting = false;
      // Move to next word
      this.wordIndex++;
      // Pause before start typing
      typeSpeed = 500;
    }

    setTimeout(() => this.type(), typeSpeed);
  }
}

// Init On DOM Load
document.addEventListener("DOMContentLoaded", init);

// Init App
function init() {
  const txtElement = document.querySelector(".txt-type");
  const words = JSON.parse(txtElement.getAttribute("data-words"));
  const wait = txtElement.getAttribute("data-wait");
  // Init TypeWriter
  new TypeWriter(txtElement, words, wait);
}

</script>

 

Link to comment
  • 2 months later...
  • Replies 1
  • Views 1.9k
  • Created
  • Last Reply
On 10/28/2020 at 10:25 AM, capturecollect said:

Site URL: https://seadragon-apple-c697.squarespace.com/

Hello!

I've been wanting to add in a typewriter effect to a Squarespace 7.1 website that I'm working on.

I found a great guide by @tuanphan that I followed and I'm really happy with the resulting effect at the top of the homepage. Code below as a reference to the original solution.

It would be great to implement this code in other areas of the website; for example - section/product page sub-headers. The only issue is that instead of having the typewriter effect cycle through a series of words or sentences, am I able to have it do the typewriter effect on one sentence and instead of deleting and redoing it on a loop - just hold on the word with the blinking cursor once it has typed out? 

Additionally, is it possible to just have a linking cursor at the end of a word or sentence, without the typing effect itself?

I appreciate that this is a bit of a double ended one, but any help on this would be amazing.

Thanks!

 

 

I am looking for the same exact thing. Any luck finding a solution? 

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.