JS工具库

lishihuan大约 6 分钟

JS工具库

Day.js

一个极简的处理时间和日期的 JavaScript 库,和 Moment.js 的 API 设计保持一样, 但体积仅有2KB。

npm install dayjs

基本用法

import dayjs from 'dayjs'

dayjs().format('YYYY-MM-DD HH:mm') // => 2022-01-03 15:06
dayjs('2022-1-3 15:06').toDate() // => Mon Jan 03 2022 15:06:00 GMT+0800 (中国标准时间)

qs

一个轻量的 url 参数转换的 JavaScript 库

npm install qs

基本用法

import qs from 'qs'

qs.parse('user=tom&age=22') // => { user: "tom", age: "22" }
qs.stringify({ user: "tom", age: "22" }) // => user=tom&age=22

一个简单的、轻量的处理 cookies 的 js API

npm install js-cookie

基本用法

import Cookies from 'js-cookie'

Cookies.set('name', 'value', { expires: 7 }) // 有效期7天
Cookies.get('name') // => 'value'

flv.js

bilibili 开源的 html5 flash 视频播放器,使浏览器在不借助 flash 插件的情况下可以播放 flv,目前主流的直播、点播解决方案。

npm install flv.js

基本用法

<video autoplay controls width="100%" height="500" id="myVideo"></video>

<script>
import flvjs from 'flv.js'

// 页面渲染完成后执行
if (flvjs.isSupported()) {
  var myVideo = document.getElementById('myVideo')
  var flvPlayer = flvjs.createPlayer({
    type: 'flv',
    url: 'http://localhost:8080/test.flv' // 视频 url 地址
  })
  flvPlayer.attachMediaElement(myVideo)
  flvPlayer.load()
  flvPlayer.play()
}
</script>

vConsole

一个轻量、可拓展、针对手机网页的前端开发者调试面板。如果你还苦于在手机上如何调试代码,用它就对了。

npm install vconsole

基本用法

import VConsole from 'vconsole'

const vConsole = new VConsole()
console.log('Hello world')

Animate.css

一个跨浏览器的 css3 动画库,内置了很多典型的 css3 动画,兼容性好,使用方便。

npm install animate.css

基本用法

<h1 class="animate__animated animate__bounce">An animated element</h1>

import 'animate.css'

animejs

一款功能强大的 JavaScript 动画库。可以与CSS3属性、SVG、DOM元素、JS对象一起工作,制作出各种高性能、平滑过渡的动画效果。

npm install animejs

基本用法

<div class="ball" style="width: 50px; height: 50px; background: blue"></div>

<script>
import anime from 'animejs/lib/anime.es.js'

// 页面渲染完成之后执行
anime({
  targets: '.ball',
  translateX: 250,
  rotate: '1turn',
  backgroundColor: '#F00',
  duration: 800
})
</script>

lodash.js

一个一致性、模块化、高性能的 JavaScript 实用工具库

npm install lodash

基本用法

import _ from 'lodash'

_.max([4, 2, 8, 6]) // 返回数组中的最大值 => 8
_.intersection([1, 2, 3], [2, 3, 4]) // 返回多个数组的交集 => [2, 3]

mescroll.js

一款精致的、在H5端运行的下拉刷新和上拉加载插件,主要用于列表分页、刷新等场景。

npm install mescroll.js

基本用法(vue组件)

<template>
  <div>
    <mescroll-vue
      ref="mescroll"
      :down="mescrollDown"
      :up="mescrollUp"
      @init="mescrollInit"
    >
      <!--内容...-->
    </mescroll-vue>
  </div>
</template>

<script>
import MescrollVue from 'mescroll.js/mescroll.vue'

export default {
  components: {
    MescrollVue
  },
  data() {
    return {
      mescroll: null, // mescroll实例对象
      mescrollDown: {}, //下拉刷新的配置
      mescrollUp: {
        // 上拉加载的配置
        callback: this.upCallback
      },
      dataList: [] // 列表数据
    }
  },
  methods: {
    // 初始化的回调,可获取到mescroll对象
    mescrollInit(mescroll) {
      this.mescroll = mescroll
    },
    // 上拉回调 page = {num:1, size:10}; num:当前页 ,默认从1开始; size:每页数据条数,默认10
    upCallback(page, mescroll) {
      // 发送请求
      axios
        .get('xxxxxx', {
          params: {
            num: page.num, // 当前页码
            size: page.size // 每页长度
          }
        })
        .then(response => {
          // 请求的列表数据
          let arr = response.data
          // 如果是第一页需手动置空列表
          if (page.num === 1) this.dataList = []
          // 把请求到的数据添加到列表
          this.dataList = this.dataList.concat(arr)
          // 数据渲染成功后,隐藏下拉刷新的状态
          this.$nextTick(() => {
            mescroll.endSuccess(arr.length)
          })
        })
        .catch(e => {
          // 请求失败的回调,隐藏下拉刷新和上拉加载的状态;
          mescroll.endErr()
        })
    }
  }
}
</script>

<style scoped>
.mescroll {
  position: fixed;
  top: 44px;
  bottom: 0;
  height: auto;
}
</style>

Chart.js

一套基于 HTML5 的简单、干净并且有吸引力的 JavaScript 图表库

npm install chart.js

基本用法

<canvas id="myChart" width="400" height="400"></canvas>

<script>
import Chart from 'chart.js/auto'

// 页面渲染完成后执行
const ctx = document.getElementById('myChart')
const myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [
      {
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: [
          'rgba(255, 99, 132, 0.2)',
          'rgba(54, 162, 235, 0.2)',
          'rgba(255, 206, 86, 0.2)',
          'rgba(75, 192, 192, 0.2)',
          'rgba(153, 102, 255, 0.2)',
          'rgba(255, 159, 64, 0.2)'
        ],
        borderColor: [
          'rgba(255, 99, 132, 1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      y: {
        beginAtZero: true
      }
    }
  }
})
</script>

signature签名

vue 签名

https://l2j2c3.gitee.io/smooth-signature/demo/index.htmlopen in new window

npm install smooth-signature
# 或
yarn add smooth-signature

randomColor 颜色随机生成脚本

npm install randomcolor
// 导入 randomColor 库
import randomColor from 'randomcolor';

// 生成20个随机颜色
var customColors = randomColor({
  count: 20,
  luminosity: 'bright',
  format: 'hex'
});
// 或者
var customColors = Array.from({ length: 20 }, () => randomColor())
console.log(customColors);

 method3 () {
     return "#"+(function(color){
     return new Array(7-color.length).join("0")+color;
     })((Math.random() * 0x1000000 | 0).toString(16));
 },

下拉框插件

1.select http://select2.github.io/open in new window

2.双 select http://loudev.comopen in new window

3.selectbox http://aui.github.io/popupjs/doc/selectbox.htmlopen in new window

文字工具插件

1.简繁体转换 https://github.com/BYVoid/OpenCCopen in new window

2.拼音 https://github.com/hotoo/pinyinopen in new window

图表插件

1.Highcharts https://www.hcharts.cn/open in new window

2.ECharts http://echarts.baidu.com/open in new window

3.Chart.js http://chartjs.org/open in new window

4.Paperjs http://paperjs.org/open in new window

5.D3.js https://d3js.org/open in new window

地图插件

1.高德地图 http://lbs.amap.com/apiopen in new window

2.jvectormap http://jvectormap.com/open in new window

验证类插件

1.validator https://niceue.com/validator/open in new window

弹出层插件

1.artDialog http://aui.github.io/artDialog/open in new window

2.layer http://layer.layui.com/open in new window

3.响应式用户交互组件库 http://bh-lay.github.io/UI/open in new window

数据处理插件

1.生成随机数据 http://mockjs.com/open in new window

Simplify折线简化库

Simplify.js 是由 Vladimir Agafonkin 编写的高性能 JavaScript 折线简化库,提取自Leafletopen in new window

地址:https://github.com/mourner/simplify-jsopen in new window

焦点图插件

PC端

1.myfocus https://github.com/koen301/myfocusopen in new window

2.SuperSlide | TouchSlide http://www.superslide2.com/open in new window

3.bxslide http://bxslider.com/open in new window

4.niceslider http://ajccom.github.io/niceslider/open in new window

5.Swiper http://www.swiper.com.cn/open in new window

6.Swiper http://idangero.us/swiper/#.WKJsSVN96M8open in new window

7.touchslider http://touchslider.com/open in new window

8.touchslider http://baijs.com/tinycircleslider/open in new window

移动端

1.Parallax.js http://hahnzhu.github.io/parallax.js/open in new window

2.Swiper http://www.swiper.com.cn/open in new window

3.iSlider http://be-fe.github.io/iSlider/open in new window

4.Slip.js http://binnng.github.io/slip.js/open in new window

5.fullpage https://github.com/peunzhang/fullpageopen in new window

时间选择器

1.Date picka http://amsul.ca/pickadate.js/date/open in new window

2.Date Range Picker http://www.daterangepicker.com/open in new window

3.glDatePicker http://glad.github.io/glDatePicker/open in new window

4.Momentjs http://momentjs.com/open in new window

5.Kalendae https://github.com/ChiperSoft/Kalendaeopen in new window

6.Kalendae https://fullcalendar.io/open in new window

7.Mobiscroll https://demo.mobiscroll.comopen in new window

取色插件

1.jquery-color https://github.com/jquery/jquery-coloropen in new window

剪切板插件

1.clipboardjs https://clipboardjs.com/open in new window

表格插件

1.FixedDataTable http://facebook.github.io/fixed-data-table/open in new window

2.handsontable https://handsontable.com/open in new window

3.DataTables https://www.datatables.net/open in new window

烟花效果插件

1.Proton http://a-jie.github.io/Proton/#exampleopen in new window

编辑器插件

1.UEdiet http://ueditor.baidu.com/website/open in new window

天气预报插件(api)

心知天气 http://www.thinkpage.cn/open in new window

新浪天气 http://php.weather.sina.com.cn/xml.php?city=����&password=DJOYnieT8234jlsK&day=0open in new window

国家气象局提供的天气预报接口:

http://www.weather.com.cn/data/sk/101010100.htmlopen in new window

http://www.weather.com.cn/data/cityinfo/101010100.htmlopen in new window

2345天气插件 http://tianqi.2345.com/plugin/open in new window

文件图片上传

1.图片上传预览 http://www.dropzonejs.com/open in new window

2.WebUploader http://fex.baidu.com/webuploader/open in new window

Canvas 实用框架、工具open in new window

canvas 酷炫特效 + 小游戏 https://github.com/poetries/canvasopen in new window

canvas-confetti 酷炫彩色纸屑动画效果 https://www.kirilv.com/canvas-confetti/open in new windowhttps://github.com/catdad/canvas-confettiopen in new window

lucky-canvas 抽奖插件 https://100px.net/open in new windowhttps://github.com/buuing/lucky-canvasopen in new window

Excalidraw 在线白板工具 https://excalidraw.com/open in new windowhttps://github.com/excalidraw/excalidrawopen in new window

fireworks-js 烟花特效 https://fireworks.js.org/open in new windowhttps://github.com/crashmax-dev/fireworks-jsopen in new window

canvas-editor 富文本编辑器 基于 canvas/svg https://hufe.club/canvas-editor/open in new windowhttps://github.com/Hufe921/canvas-editoropen in new window

Luckysheet 类似 excel 在线表格 https://dream-num.github.io/LuckysheetDocs/open in new windowhttps://github.com/dream-num/Luckysheetopen in new window

x-spreadsheet 基于 Web(es6) canvas 构建的轻量级 Excel 开发库 https://myliang.github.io/x-spreadsheet/open in new windowhttps://github.com/myliang/x-spreadsheetopen in new window

QRCanvas 基于 canvas 的 JavaScript 二维码生成工具 https://gera2ld.github.io/qrcanvas/open in new windowhttps://github.com/gera2ld/qrcanvasopen in new window

Signature Pad 基于 Canvas 实现的签名库 http://szimek.github.io/signature_pad/open in new windowhttps://github.com/szimek/open in new window

Rough.js 是一个轻量级的(大约 8k),基于 Canvas 的可以绘制出粗略的手绘风格的图形库。 https://roughjs.com/open in new windowhttps://github.com/rough-stuff/roughopen in new window

Fabric.js 基于 Canvas 的画布(海报) http://fabricjs.com/open in new windowhttps://github.com/fabricjs/fabric.jsopen in new window

uCharts 是一款高性能的前端应用图表库,开发人员编写一套代码,可以在 Web、iOS、Android以及小程序中使用。 https://www.ucharts.cn/v2/#/open in new windowhttps://gitee.com/uCharts/uChartsopen in new window

SpriteJS 是一款由360奇舞团开源的跨终端 canvas 绘图框架,可以基于 canvas 快速绘制结构化 UI、动画和交互效果,并发布到任何拥有canvas环境的平台上(比如浏览器、小程序和node)。 http://spritejs.com/open in new windowhttps://github.com/spritejs/spritejsopen in new window

Paper.js是一款开源的矢量图形脚本框架,基于 HTML5 Canvas 开发,提供清晰的场景图、DOM和大量强大的功能用来创建各种向量图和贝塞尔曲线。 http://paperjs.org/open in new windowhttp://github.com/paperjs/paper.jsopen in new window