0.1.5 • Published 2 years ago

js-increments v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

jsIncrements

A small library with the logic needed to increment counters and progress bars

Size Language Last commit

See a Demo

Installation

Browser

<!-- self hosted -->
<script src="<PATH>/dist/index.min.js"></script>

<!-- CDN -->
<script src="https://cdn.jsdelivr.net/npm/js-increments@<VERSION>/dist/index.min.js"></script>

Node.js

npm i js-increments
import jsIncrements from 'js-increments'

How to use

const counter = new jsIncrements({
  from: 0, 
  to: 100,
  max: 100,
  step: 1,
  duration: 5000, 
  wait: 0,
  target: {
    selector: '#progressText',
    type: 'text',
  },
});

counter.start();

// or new jsIncrements(options).start()

Counter Options

OptionTypeDefaultDescription
fromNumber0The number that the counter should start
toNumber100The number that the counter must stop
durationNumber50Time duration of the animation (ms)
waitNumber0Time to wait before starting (ms)
maxNumber100Reference to the maximum number of the counter. Useful when working with percentages
stepNumber1How many numbers to advance with each increment
targetBoolean or ObjectfalseSee bellow
targetsArray[]Same as target. But an array
onStartnull or functionnullFunction when run on startup
onUpdatenull or functionnullFunction when increment
onFinishnull or functionnullFunction when finished

The onStart, onUpdate and onFinish callbacks receives the counter instance as a parameter

onUpdate: (data) => {
  console.log('updated', data.counter)
},

Target Options

OptionTypeDefaultDescription
selectorStringnullDOM element selector that should be manipulated. Something like #progress or .counter
typeString'text'Kind of manipulation. style or text
propertyString'width'CSS property that must be changed (will be manipulated with setProperty)
unitString'%'CSS property unit
percentageBooleantrueWhether you want to display the number in percentage or the real number. For example: a progress bar containing the steps of a wizard that goes from 0 to 5. When you get halfway through the progress bar, do you want to display 3 or 50%?
modeString'precision'If set to precision, each increment will generate a DOM update. This mode requires more processing. If set to performance, only an initial update is performed and the effect happens through CSS (transition-property and transition-duration).

Example for manipulating a progress bar

const target = {
  selector: '#progressBar #bar',
  property: 'width',
  type: 'style',
  unit: '%'
},

Example for manipulating a text

const target = {
  selector: '#progressCircle text',
  type: 'text',
  percentage: true
},

Developer commands

Run tests

  • npm run test

Watch project

  • npm run watch

Build

  • npm run build
0.1.4

2 years ago

0.1.3

2 years ago

0.1.5

2 years ago

0.1.2

2 years ago

0.1.1

3 years ago

0.1.0

3 years ago