Giter VIP home page Giter VIP logo

malarkey's Introduction

Malarkey.js npm Version Build Status Coverage Status

Simulate a typewriter/ticker effect on a DOM element.

Features

For an in-production demo, see the npm front page.

Usage

Editable demo

<body>
  <div class="malarkey"></div>
  <script src="../dist/malarkey.min.js"></script>
  <script>
    var elem = document.querySelector('.malarkey');
    var opts = {
      typeSpeed: 50,
      deleteSpeed: 50,
      pauseDelay: 2000,
      loop: true,
      postfix: ''
    };
    malarkey(elem, opts).type('Say hello')   .pause().delete()
                        .type('Wave goodbye').pause().delete();
  </script>
</body>

Custom element property

By default, the effect is applied on elem.innerHTML. To apply the effect on a different element property (such as the placeholder attribute of an input element), pass in opts.getter and opts.setter, like so:

Editable demo

<body>
  <input type="text" class="malarkey">
  <script src="../dist/malarkey.min.js"></script>
  <script>
    var elem = document.querySelector('.malarkey');
    var attr = 'placeholder';
    var opts = {
      loop: true,
      getter: function(elem) {
        return elem.getAttribute(attr) || '';
      },
      setter: function(elem, val) {
        elem.setAttribute(attr, val);
      }
    };
    malarkey(elem, opts).type('Say hello')   .pause().delete()
                        .type('Wave goodbye').pause().delete();
  </script>
</body>

Pausing and resuming

To pause and resume the sequence on-the-fly, use the triggerPause and triggerResume methods:

Editable demo

<body>
  <div class="malarkey"></div>
  <script src="../dist/malarkey.min.js"></script>
  <script>
    var elem = document.querySelector('.malarkey');
    var opts = {
      typeSpeed: 50,
      deleteSpeed: 50,
      pauseDelay: 2000,
      loop: true,
      postfix: ''
    };
    var m = malarkey(elem, opts).type('Say hello')   .pause().delete()
                                .type('Wave goodbye').pause().delete();
    document.addEventListener('click', function() {
      if (m.isRunning()) {
        m.triggerPause();
        elem.style.color = 'red';
      } else {
        m.triggerResume();
        elem.style.color = 'black';
      }
    });
  </script>
</body>

Note that here, we toggle between calling triggerPause and triggerResume on every click event.

API

In the browser, malarkey is a global variable. In Node, do:

var malarkey = require('malarkey');

var m = malarkey(elem [, opts])

Initialises the typewriter/ticker effect on elem with the given opts settings.

  • elem — A DOM element.

  • opts — An object literal:

    Key Description Default
    loop Whether to repeat the entire sequence false
    typeSpeed Time in milliseconds to type a single character 50
    deleteSpeed Time in milliseconds to delete a single character 50
    pauseDelay Delay in milliseconds to pause 2000
    postfix A string appended to any str passed to type and delete ''
    getter A function to get the current value of elem Returns elem.innerHTML
    setter A function to assign a new value to elem Assigns to elem.innerHTML

m.type(str [, speed])

Type the str at the given speed.

  • speed — Defaults to opts.typeSpeed.

m.delete()

Delete the contents of elem, one character at a time, at the default speed.

m.delete(str [, speed])

Delete the given str, one character at a time, at the given speed.

  • strnull, or a string. If null, deletes every character in elem. Else deletes str from elem if and only if elem ends with str.
  • speed — Defaults to opts.deleteSpeed.

m.delete(n [, speed])

Delete the last n characters of elem, one character at a time, at the given speed.

  • n — An integer. If -1, deletes every character in elem. Else deletes the last n characters of elem.
  • speed — Defaults to opts.deleteSpeed.

m.clear()

Clear the contents of elem.

m.pause([delay])

Do nothing for delay.

  • delay — Defaults to opts.pauseDelay.

m.call(fn)

Call the given fn, passing it elem as the first argument.

  • fn — Can be asynchronous. We must invoke this within this function to signal that it has finished execution.

m.triggerPause([fn])

Pauses the sequence. Calls the given fn when the sequence has been paused, passing it elem as the first argument.

m.triggerResume()

Resumes the sequence immediately. Has no effect if the sequence is already running.

m.isRunning()

Returns true if the sequence is currently running. Else returns false.

Installation

Install via npm:

$ npm i --save malarkey

Install via bower:

$ bower i --save yuanqing/malarkey

License

MIT

malarkey's People

Contributors

yuanqing avatar stefanoverna avatar

Watchers

James Cloos avatar inmyfree avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.