前端常用方法集合
- 前端常用方法收集集合,方便平时开发(部分方法来自网上)
- 模块化开发,方便引用
- 支持
tree shaking
,以便减少打包体积
Date
:关于日期的一些方法封装Dom
:Dom
操作一些方法封装Device
:关于设备的一些方法封装File
:关于File
的一些方法封装Format
: 关于格式化的一些方法封装Object
: 关于Object
的一些方法封装Array
: 关于array
的一些方法封装Storage
: 关于Storage
的一些方法封装Url
: 关于Url
的一些方法封装Validate
: 关于校验的一些方法封装Animation
: 关于css
,vue
可使用的动画
- 使用
npm
方式
npm i fecmjs
// 1. 使用所有模块
import * as Fecmjs from 'fecmjs';
const flag = Fecmjs.isEmail('[email protected]');
console.log(flag); // true
// 2. 当你只需要用部分功能的时候,可以引入部分模块(推荐)
import {isEmail} from 'fecmjs';
const flag = isEmail('[email protected]');
console.log(flag); // true
- 使用
<script></script>
方式引入
<script src="https://cdn.xxxx/fecmjs.min.js"></script>
<script>
var flag = fecmjs.checkEmail('[email protected]');
console.log(flag); // true
</script>
-
示例:
import {getCurrentTimestamp} from 'femcjs'; console.log(getCurrentTimestamp()) // 1730129184412
-
参数名 说明 默认值 fewdays
要获取的距离目标日子的多少天 0
time
目标时间 new Date().getTime()
- 当
fewdays
为正整数,则获取目标日期的后几天 - 当
fewdays
为负整数,则获取目标日期的前几天
示例:
// 获取2024-10-28日的前30天 import {getCurrentTimestamp} from 'femcjs'; console.log(getCurrentTimestamp(-30, '2024-10-28')); // 2024-09-28 // 获取2024-10-28日的后30天 console.log(getCurrentTimestamp(30, '2024-10-28')); // 2024-11-27
- 当
-
参数名 说明 默认值 timestamp
时间戳 needHMS
是否需要返回时分秒 false
示例:
import {getDateByTimestamp} from 'femcjs'; console.log(getDateByTimestamp('1730129184412')); // 2024-10-28 console.log(getDateByTimestamp('1730129184412', true)); // 2024-10-28 23:26:24
-
参数名 说明 默认值 time
具体日期。格式与 new Date()
传入的格式一样new Date().getTime()
- 默认返回当前时间的时间戳
- 入参形式与
new Date()
方法相同
示例:
import {getTimestampByDate} from 'femcjs'; console.log(getTimestampByDate()); // 1730131646512 console.log(getTimestampByDate('2024-10-29')); // 1730160000000 console.log(getTimestampByDate('2024-10-29 01:30')); // 1730136600000
-
参数名 说明 默认值 selectName
css
选择器- 如果有多个
Dom
,则返回数组 - 如果只有一个
Dom
,则返回单个Dom
- 如果有多个
-
参数名 说明 默认值 selectName
css
选择器或者是dom
对象nameList
要添加的类名 - 想添加多个类名时,
nameList
可传入数组 - 想添加单个类名时,
nameList
可传入字符串
示例:
import {addClass} from 'femcjs'; addClass('#dom', ['name1', 'name2', 'nam3']);
- 想添加多个类名时,
-
参数名 说明 默认值 selectName
css
选择器或者是dom
对象返回该对象类名组成的数组
-
参数名 说明 默认值 selectName
css
选择器或者是dom
对象nameList
要删除的类名 - 想删除多个类名时,
nameList
可传入数组 - 想删除单个类名时,
nameList
可传入字符串
- 想删除多个类名时,
-
参数名 说明 默认值 selectName
css
选择器或者是dom
对象style
要设置的样式 - 想设置多个样式时,
style
可传入对象 - 想设置单个样式时,
style
可传入字符串
示例:
import {setStyle} from 'femcjs'; setStyle('#dom', {color: 'red', backgroundColor: '#000'}); // 设置多个样式 setStyle('#dom', 'color: red'); // 设置单个样式
- 想设置多个样式时,
-
参数名 说明 默认值 selectName
css
选择器或者是dom
对象,或者是一组NodeList
- 如果
selectName
是一组NodeList
,则返回这个NodeList
组成的信息数组
示例:
import {setStyle} from 'femcjs'; console.log(getDomInfo($('li'))); // [DOMRect, DOMRect]
- 如果
-
参数名 说明 默认值 parentSelectName
父元素类名选择器或者是 dom
对象childSelectName
子元素类名选择器或者是 dom
对象父元素得设置高度
-
参数名 说明 默认值 selectName
元素类名选择器或者是 dom
对象 -
-
-
参数名 说明 默认值 file
file
对象 -
参数名 说明 默认值 file
file
对象 -
参数名 说明 默认值 file
file
对象- 主要用于预览上传的图片
- 注意使用
revokeObjectURL
方法回收内存
示例:
import {getFilePreviewSrc, $} from 'femcjs'; const previewSrc = getFilePreviewSrc(file); $('#preview_img').src = previewSrc;
-
参数名 说明 默认值 file
file
对象 -
参数名 说明 默认值 file
file
对象 -
参数名 说明 默认值 file
file
对象 -
参数名 说明 默认值 textStr
内容文本
-
参数名 说明 默认值 array
被操作的数组 size
要指定的大小 示例:
import {chunkArrBySize} from 'femcjs'; const arr = [1, 2, 3, 4, 5, 6, 7, 8]; const SIZE = 3; console.log(chunkArrBySize(arr, SIZE)); // [[1,2,3], [4,5,6], [7,8]]
-
参数名 说明 默认值 array
被操作的数组 item
要删除的项 newone
是否返回一个新的数组,不改变原数组大小 false
item
只能是字符串或者数值类型- 如果
newone = false
;会改变原始数组大小 - 如果
newone = true
;不会改变原始数组大小,返回一个新的目标数组
示例:
import {removeArrayItem} from 'femcjs'; const arr = [1, 2, 3, 4, 'hello', 'lee', 7, 8]; console.log(removeArrayItem(arr, 'hello', true)); // [1, 2, 3, 4, 'lee', 7, 8]; console.log(arr); // [1, 2, 3, 4, 'hello', 'lee', 7, 8];
-
参数名 说明 默认值 key
要获取的key值 - 如果获取的
key
值是对象,则直接返回key
值对应的对象 - 否则返回
key
值对应的字符串
示例:
// 如果localStorage存储了info: {"name":"lee","age":29}, name: test import {getLocalStorage} from 'femcjs'; console.log(getLocalStorage(info)); // {name: lee, age} 对象 console.log(getLocalStorage(name)); // test 字符串
- 如果获取的
-
参数名 说明 默认值 key
要设置的 key
值val
要设置的 key
对应的值val
可以为对象,也可以为字符串示例:
import {setLocalStorage} from 'femcjs'; setLocalStorage('info', {name: 'lee', age: 29}); // info -> {"name":"lee","age":29}
-
参数名 说明 默认值 key
要删除的 key
值- 如果
key
为数组,则可以删除多个对应的key
值 - 如果
key
为字符串,则删除单个值
示例:
import {removeLocalStorage} from 'femcjs'; console.log(removeLocalStorage(['info', 'test'])); // 将删除info,test对应的值
- 如果
-
示例:
import {clearLocalStorage} from 'femcjs'; console.log(clearLocalStorage());
-
参数名 说明 默认值 key
要获取的值的 key
''
url
要获取的 url
链接window.location.href
- 当
key
为空,会返回url
所有的参数对应值组成的对象 - 当
key
为某个值,则返回url
这个key
的值
示例:
import {getUrlValue} from 'femcjs'; const url = 'http://localhost:5501/test/?name=lee&age=29' console.log(getUrlValue('', url)); // {name: 'lmc', age: 29} console.log(getUrlValue('name', url)); // 'lmc'
- 当
-
参数名 说明 默认值 value
要设置的值 {}
示例:
import {setUrlWithNorefresh} from 'femcjs'; console.log(setUrlWithNorefresh({name: 'lmc', age: 29}); // http://localhost:5501/test/?name=lmc&age=29
-
参数名 说明 默认值 textStr
内容文本 只要有中文,就返回
true
-
参数名 说明 默认值 textStr
内容文本 只要有英文,就返回
true
-
参数名 说明 默认值 textStr
内容文本 只要有中文特殊字符,就返回
true
-
参数名 说明 默认值 textStr
内容文本 只要有英文特殊字符,就返回
true
-
参数名 说明 默认值 textStr
内容文本 是否为纯中文(含中文特殊字符和数字;不含英文和特殊英文字符)
-
参数名 说明 默认值 textStr
内容文本 是否纯英文 (含英文特殊字符和数字;不含英文和特殊英文字符)
-
参数名 说明 默认值 email
邮件 -
参数名 说明 默认值 phoneNumber
手机号码 验证中国大陆手机号
-
参数名 说明 默认值 phoneNumber
国际手机号码
-
参数名 说明 默认值 file
文件对象 {}
示例:
import {formatFileSize} from 'femcjs'; console.log(formatFileSize(file)); // 1.37KB
-
参数名 说明 默认值 totalSeconds
视频时长 0
示例:
import {formatVideoDuration} from 'femcjs'; console.log(formatVideoDuration(3600)); // 01:00:00
-
fade-in
:淡入fade-right-to-left
:从右往左淡入fade-top-to-bottom
:从上往下淡入fade-zoom-in
:放大淡入
示例:
<Transition name="fade-right-to-left"> <components v-if="show" /> </Transition>
-
具体可查看
dist/styles/css-ani.css
示例:
<!-- 该元素将会延迟500ms,以750ms,进行循环闪烁动画 --> <div class="shine time750 delay500"></div>