user24312
user24312

Reputation: 73

Automatically scrolling

I am trying to create the typing effect in javascript produced by typed.js library. http://www.mattboldt.com/demos/typed-js/

I'm using a div as a container. The problem is that when the text reaches the height of the div the scrolls appear (I set overflow to auto) but I want it to scroll automatically so that I can see the text that is being typed. Do you have any idea of how I can accomplish that? I'm coding in my tablet.

Upvotes: 0

Views: 86

Answers (1)

Joseph Marikle
Joseph Marikle

Reputation: 78520

You can tie into the built-in callbacks to set an interval that will scroll the container to the bottom until the string is fully typed.

var wrapper = document.querySelector('.wrapper')
Typed.new('.element', {
  strings: ["Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."],
  typeSpeed: -2000,
  preStringTyped: function() {
    this.t = setInterval(function() {
      wrapper.scrollTop = wrapper.scrollHeight;
    }, 20);
  },
  onStringTyped: function() {
    clearInterval(this.t);
  }
});
.wrapper {
  width: 500px;
  height: 100px;
  background: #eee;
  overflow: hidden;
  overflow-y: scroll;
}

.typed-cursor{
    opacity: 1;
    -webkit-animation: blink 0.7s infinite;
    -moz-animation: blink 0.7s infinite;
    animation: blink 0.7s infinite;
}
@keyframes blink{
    0% { opacity:1; }
    50% { opacity:0; }
    100% { opacity:1; }
}
@-webkit-keyframes blink{
    0% { opacity:1; }
    50% { opacity:0; }
    100% { opacity:1; }
}
@-moz-keyframes blink{
    0% { opacity:1; }
    50% { opacity:0; }
    100% { opacity:1; }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/typed.js/1.1.7/typed.min.js"></script>
<div class="wrapper">
  <span class="element"></span>
</div>

If you want to get really fancy, you can even disable the interval if the user manually scrolls the container:

var wrapper = document.querySelector('.wrapper')
Typed.new('.element', {
  strings: ["Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."],
  typeSpeed: -2000,
  preStringTyped: function() {
    this.t = setInterval(() => {
      if(wrapper.scrollTop === 0 || wrapper.scrollTop === wrapper.scrollHeight) {
        wrapper.scrollTop = wrapper.scrollHeight;
      } else {
        clearInterval(this.t);
      }
    }, 20);
  },
  onStringTyped: function() {
    clearInterval(this.t);
  }
});
.wrapper {
  width: 500px;
  height: 100px;
  background: #eee;
  overflow: hidden;
  overflow-y: scroll;
}

.typed-cursor{
    opacity: 1;
    -webkit-animation: blink 0.7s infinite;
    -moz-animation: blink 0.7s infinite;
    animation: blink 0.7s infinite;
}
@keyframes blink{
    0% { opacity:1; }
    50% { opacity:0; }
    100% { opacity:1; }
}
@-webkit-keyframes blink{
    0% { opacity:1; }
    50% { opacity:0; }
    100% { opacity:1; }
}
@-moz-keyframes blink{
    0% { opacity:1; }
    50% { opacity:0; }
    100% { opacity:1; }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/typed.js/1.1.7/typed.min.js"></script>
<div class="wrapper">
  <span class="element"></span>
</div>

Upvotes: 1

Related Questions