Giter Club home page Giter Club logo

number-flip's Introduction

number-flip

NPM

Change number with flipping animation

demo on codesandbox

install

$ npm install --save number-flip

usage

import number-flip

import { Flip } from 'number-flip'

use it!

create one and make it flip immediately:

new Flip({
  node: $('.flip'),
  from: 9527,
  to: 42
})

flip one with delay:

new Flip({
  node: $('.flip'),
  from: 9527,
  to: 42,
  delay: 1 // second
})

create one and flip it later:

const el = new Flip({
  node: $('.flip'),
  from: 9527
})

el.flipTo({to: 42})

customize animation duration:

new Flip({
  node: document.querySelector('.flip'),
  from: 9527,
  to: 42,
  duration: 2 // second
})

more complex usage

new Flip({
  node: document.querySelector('.flip'),
  from: 73,
  to: 25,
  duration: 2,
  delay: 1,
  easeFn: function(pos) {
    if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,3);
    return 0.5 * (Math.pow((pos-2),3) + 2);
  },
  // for more easing function, see https://github.com/danro/easing-js/blob/master/easing.js
  systemArr: ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
})

CSS customizable:

HTML structure of a 3-digits flip would be like:

.number-flip
    .ctnr.ctnr0
        .digit*10
    .ctnr.ctnr1
        .digit*10
    .ctnr.ctnr2
        .digit*10

The height / width of .number-flip is based on the height / width of .digit, you can customize the size by changing the css of .digit:

  .number-flip { ... }
  .ctnr { ... }
  .digit { ... }

or you can rename it in config:

new Flip({
  containerClassName: 'c',
  digitClassName: 'd',
  separatorClassName: 's'
})

separator:

Spearator allowed

new Flip({
  node: $('.flip'),
  from: 95279527,
  separator: ','
})

even more

new Flip({
  node: $('.flip'),
  from: 95279527,
  separator: ['万', '亿', '兆'],
  separateEvery: 4
})

syntax

var flipInstance = new Flip(options)
flipInstance.flipTo(instanceOptions)

return value

The returned Flip instance has a function called flipTo.
flipTo takes one instanceOptions, so you can start the flip animation whenever you want.

parameter

options

  • node: An Element object representing the animation container. Make sure this element is already existed in the DOM when you new the instance.
  • from: The number that animation starts from. Optional if you want to flip with 0. Expected a positive integer.
  • to: The number that animation rolls to. Optional if you want to start manually. Expected a positive integer.
  • duration optional: The animation duration in seconds. If not specified, duration defaults to 0.5 second.
  • delay optional: The delay of animation in seconds. If not specified, there's no delay.
  • easeFn optional: A easing function to be executed. If not specified, easeFn defaults easeInOutCubic.
  • systemArr optional: An array ten-lengthed, representing the content of each decimal rolling system. If not specified, systemArr defaults to [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ].
  • direct optional: A boolean representing if the number would rolling directly or one by one. For example, from 0 to 99, the ones place would pass 9 digits if is direct. Or if is not directly, would pass 99 digits, 9 rounds for each of the tens place. If not specified, direct defaults true.
  • separator: A string / array representing the separator, defaults off. Could set to a string or an array-of-string.
  • seperateOnly: A number representing the only separator, defaults 0.
  • separateEvery: The number per digit separator would add to, defaults 3, won't work if seperateOnly has been set.

instanceOptions

  • to: Same as options.to.
  • duration optional: Same as options.duration.
  • easeFn optional: Same as options.easeFn.
  • direct optional: Same as options.direct.

TODO

  • flip with FLIP
  • syntax
  • browser compatibility list

license

MIT

credit

Special thanks to Browserstack providing cross-browser testing.

dev and build

yarn dev
yarn build

contributing

  1. fork this repo
  2. git checkout -b NEW-FEATURE
  3. git commit -am 'ADD SOME FEATURE'
  4. git push origin NEW-FEATURE

number-flip's People

Contributors

artu-ole avatar gaoryrt avatar kyle-martin1 avatar lyric-zemin avatar oodavy41 avatar whu12yz avatar zmarouf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

number-flip's Issues

UMD

It would be nice you could use the library from CDN like https://unpkg.com/[email protected]/flip.js it notain require from node module and exports.

It would be nice if you don't need to use bundler yourself to just test your library, it will not work in codepen.

Numbers cannot grow from low to high

hi friend,
The problem is that the number can only be changed from high level such as 1000 to low level such as 100, but it can't change from 100 to 1000.

code show as below:

你好朋友,
发现个问题,就是数字只能从高位比如1000变到低位比如100,但是无法从100变到1000哦。

代码如下:

this.counter.HFilp = new Flip({ node: this.$refs.H, from: 19, duration: 0.5 })
this.counter.HFilp.flipTo({ to: 100 })
// 这样只会显示后两位 即 00
this.counter.HFilp = new Flip({ node: this.$refs.H, from: 100, duration: 0.5 })
this.counter.HFilp.flipTo({ to: 60 })
// 这样会自动补0 即060

Everything else is very good, thank you~

Looked at the other issue should be the same meaning as the #9 friend

其他的都很好用,谢谢您~

看了下其他issue 应该和 #9 的朋友表达的是同一个意思

NPM package import error.

Hi , I tried to import the package import { Flip } from "number-flip" to my framer project and this happens :

Screenshot 2023-11-11 at 21 50 40

my current hunch is that maybe the package is not ESM compatible.
so, is the package ESM compatible?

TypeError: Cannot read property 'classList' of undefined

It seems something wrong when I put it the following code in a setInterval function.
Code like following:

      this.counter.timer && clearInterval(this.counter.timer)
      this.counter.timer = setInterval(() => {
        let newTime = new Date();
        this.counter.timeLeft = Math.floor((new Date(this.counter.startTime).getTime() - newTime.getTime()) / 1000);
        if (this.counter.timeLeft >= 0) {
          let h = Math.floor(this.counter.timeLeft / 60 / 60);
          this.counter.H = h < 10 ? "0" + h : h;

          let m = Math.floor((this.counter.timeLeft / 60) % 60);
          this.counter.M = m < 10 ? "0" + m : m;

          let s = this.counter.timeLeft % 60;
          this.counter.S = s < 10 ? "0" + s : s;
        } else {
          this.counter.timer && clearInterval(this.counter.timer)
          // 切换倒计时
          this.counter.GFlip = new Flip({ node: this.$refs.gifts,  from: 132486, duration: 2 })
          this.counter.GFlip.flipTo({ to: 131909 })
        }
      }, 1000);

I got an error when I use `flipTo` function in a 'react' project

my react component code:

const FlipNumber = ({ number: realNumber, className }) => {
  const ref = useRef();
  const flipRef = useRef(null);
  useEffect(() => {
    if (!flipRef.current) {
      flipRef.current = new Flip({
       node: ref.current,
        from: 0,
        to: realNumber,
        duration: 1.5,
        separator: ',',
      });
    } else {
      // error when flipTo runs 
      flipRef.current.flipTo({ to: realNumber });
    }
  }, [realNumber]);
  return (
   <div
      ref={ref}
    />
  );
};

image

image

uglifyJs can not minify the bundle when using number-flip

when I build my project, it shows:
Failed to minify the bundle. Error: 0.afde46cb.async.js from UglifyJs.
Then I went to check the 0.afde46cb.async.js and found that there is a 'const' in code. Unfortunately the it is from number-flip. Since UglifyJs is not supported es6, hope it may draw your attention. Thx.

Display error after ‘resize’ event.

  • in English, so that might help more people
  • make sure it's not another duplicated issue

1. steps to reproduce or any minimal reproducible example
If there's a 'resize' after flip, it will display error. Like that:
image
And how to reset my flip?
2. what is expected and what is actually happening

Could you please publish your new update 'remove log' to npmjs.com?

  • in English, so that might help more people
  • make sure it's not another duplicated issue
    It's a fantastic plugin, Thank you for your share!
    1. Is your feature request related to a problem? Please describe.
    what the problem is. Ex. I'm always frustrated when [...]

2. Describe the solution you'd like
what you want to happen.

3. Describe alternatives you've considered
any alternative solutions or features you've considered.

support float

2. Describe the solution you'd like
from float to float ignore decimal point, put it where it is .

I can select invisible numbers!

1. steps to reproduce or any minimal reproducible example
cmd + a to select all, invisible numbers will be selected.
2. what is expected and what is actually happening
only visible ones should be selected.

hide the leading zeros when decreasing

1. Is your feature request related to a problem? Please describe.
There will be some unnecessary zeros when decreasing the number, like, from 88888 to 555, it stops at '00555' with two leading zeros.

2. Describe the solution you'd like
Hide the zeros with blank rows.

3. Describe alternatives you've considered
opacity: 0 with css selector.

flipping from 9 to 10 throws

After fiddling around way too long to import this into my solidjs app I notice that this throws when incrementing to a new digit (eg. 9->10, 99->100 etc.):

Uncaught TypeError: e3.ctnrArr[r4] is undefined
    value index.js:1
    value index.js:1
    value index.js:1
    AnimatedProgressBar AnimatedProgressBar.tsx:71
    runComputation dev.js:781
    updateComputation dev.js:759
    runTop dev.js:876
    runUserEffects dev.js:989
    completeUpdates dev.js:945
    runUpdates dev.js:893
    completeUpdates dev.js:945
    runUpdates dev.js:894
    writeSignal dev.js:732
    setter dev.js:217
    triggerAnimation RightHud.tsx:13
    eventHandler dev.js:859
    delegateEvents dev.js:591
    <anonymous> CharacterCreation.tsx:64
2 index.js:1:7707
    value index.js:1
    forEach self-hosted:203
    value index.js:1
    value index.js:1
    AnimatedProgressBar AnimatedProgressBar.tsx:71
    runComputation dev.js:781
    updateComputation dev.js:759
    runTop dev.js:876
    runUserEffects dev.js:989
    completeUpdates dev.js:945
    runUpdates dev.js:893
    completeUpdates dev.js:945
    runUpdates dev.js:894
    writeSignal dev.js:732
    setter dev.js:217
    triggerAnimation RightHud.tsx:13
    eventHandler dev.js:859
    (Async: EventListener.handleEvent)
    delegateEvents dev.js:591
    <anonymous> CharacterCreation.tsx:64

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.