@oipage/core.js 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mailmap +1 -0
- package/AUTHORS.txt +6 -0
- package/CHANGELOG +6 -0
- package/LICENSE +21 -0
- package/README.md +77 -0
- package/dist/corejs.js +172 -0
- package/dist/corejs.min.js +5 -0
- package/package.json +33 -0
- package/src/animation/index.d.ts +10 -0
- package/src/animation/index.js +102 -0
- package/src/index.js +7 -0
- package/src/throttle/index.d.ts +29 -0
- package/src/throttle/index.js +50 -0
- package/types/index.d.ts +10 -0
package/.mailmap
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
zxl20070701 <1904314465@qq.com>
|
package/AUTHORS.txt
ADDED
package/CHANGELOG
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) zxl20070701 走一步,再走一步
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# [core.js](https://github.com/oi-contrib/core.js)
|
|
2
|
+
前端开发常用基础&核心方法
|
|
3
|
+
|
|
4
|
+
<p>
|
|
5
|
+
<a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@oipage/core.js&interval=7">
|
|
6
|
+
<img src="https://img.shields.io/npm/dm/@oipage/core.js.svg" alt="downloads">
|
|
7
|
+
</a>
|
|
8
|
+
<a href="https://www.npmjs.com/package/@oipage/core.js">
|
|
9
|
+
<img src="https://img.shields.io/npm/v/@oipage/core.js.svg" alt="Version">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/oi-contrib/core.js" target='_blank'>
|
|
12
|
+
<img alt="GitHub repo stars" src="https://img.shields.io/github/stars/oi-contrib/core.js?style=social">
|
|
13
|
+
</a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<img src="https://nodei.co/npm/@oipage/core.js.png?downloads=true&downloadRank=true&stars=true" alt="NPM">
|
|
17
|
+
|
|
18
|
+
## 如何使用?
|
|
19
|
+
|
|
20
|
+
首先,我们需要使用npm进行安装:
|
|
21
|
+
|
|
22
|
+
```shell
|
|
23
|
+
npm install --save @oipage/core.js
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
安装完成以后,就可以在项目中引入了,下面就不同的环境,使用`aniamtion`举例子。
|
|
27
|
+
|
|
28
|
+
### 浏览器环境下
|
|
29
|
+
|
|
30
|
+
可以直接使用import语法:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
import { animation } from "@oipage/core.js"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
当然,也可以单独引入:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import animation from "@oipage/core.js/src/animation/index"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
> 支持Typescript环境。
|
|
43
|
+
|
|
44
|
+
### Nodejs环境下
|
|
45
|
+
|
|
46
|
+
直接使用require语法即可:
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const { animation } = require("@oipage/core.js");
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 使用CDN方式
|
|
53
|
+
|
|
54
|
+
此外,在浏览器端,如果你不想使用npm管理,或者是老项目,也支持CDN的方式:
|
|
55
|
+
|
|
56
|
+
```html
|
|
57
|
+
<script src="https://cdn.jsdelivr.net/npm/@oipage/core.js"></script>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
然后直接:
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
Corejs.animation(function(){
|
|
64
|
+
// todo
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
具体的API细节(使用import语法说明,其余方式类似),如下:
|
|
69
|
+
|
|
70
|
+
- [animation 动画](./docs/animation.md)
|
|
71
|
+
- [throttle 节流函数](./docs/throttle.md)
|
|
72
|
+
|
|
73
|
+
## 版权
|
|
74
|
+
|
|
75
|
+
MIT License
|
|
76
|
+
|
|
77
|
+
Copyright (c) [zxl20070701](https://zxl20070701.github.io/notebook/home.html) 走一步,再走一步
|
package/dist/corejs.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @oipage/core.js v0.1.0-alpha.0
|
|
3
|
+
* git+https://github.com/oi-contrib/core.js.git
|
|
4
|
+
*/
|
|
5
|
+
(function (global, factory) {
|
|
6
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
7
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
8
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Corejs = factory());
|
|
9
|
+
})(this, (function () { 'use strict';
|
|
10
|
+
|
|
11
|
+
//当前正在运动的动画的tick函数堆栈
|
|
12
|
+
var $timers = [];
|
|
13
|
+
//唯一定时器的定时间隔
|
|
14
|
+
var $interval = 13;
|
|
15
|
+
//定时器ID
|
|
16
|
+
var $timerId;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 动画轮播
|
|
20
|
+
* @param {function} doback 轮询函数,有一个形参deep,0-1,表示执行进度
|
|
21
|
+
* @param {number} duration 动画时长,可选
|
|
22
|
+
* @param {function} callback 动画结束回调,可选,有一个形参deep,0-1,表示执行进度
|
|
23
|
+
*
|
|
24
|
+
* @returns {function} 返回一个函数,调用该函数,可以提前结束动画
|
|
25
|
+
*/
|
|
26
|
+
function animation (doback, duration, callback) {
|
|
27
|
+
if (arguments.length < 2) duration = 400;
|
|
28
|
+
if (arguments.length < 3) callback = function () { };
|
|
29
|
+
|
|
30
|
+
var clock = {
|
|
31
|
+
//把tick函数推入堆栈
|
|
32
|
+
"timer": function (tick, duration, callback) {
|
|
33
|
+
if (!tick) {
|
|
34
|
+
throw new Error('Tick is required!');
|
|
35
|
+
}
|
|
36
|
+
var id = new Date().valueOf() + "_" + (Math.random() * 1000).toFixed(0);
|
|
37
|
+
$timers.push({
|
|
38
|
+
"id": id,
|
|
39
|
+
"createTime": new Date(),
|
|
40
|
+
"tick": tick,
|
|
41
|
+
"duration": duration,
|
|
42
|
+
"callback": callback
|
|
43
|
+
});
|
|
44
|
+
clock.start();
|
|
45
|
+
return id;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
//开启唯一的定时器timerId
|
|
49
|
+
"start": function () {
|
|
50
|
+
if (!$timerId) {
|
|
51
|
+
$timerId = setInterval(clock.tick, $interval);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
//被定时器调用,遍历timers堆栈
|
|
56
|
+
"tick": function () {
|
|
57
|
+
var createTime, flag, tick, callback, timer, duration, passTime, timers = $timers;
|
|
58
|
+
|
|
59
|
+
$timers = [];
|
|
60
|
+
$timers.length = 0;
|
|
61
|
+
|
|
62
|
+
for (flag = 0; flag < timers.length; flag++) {
|
|
63
|
+
//初始化数据
|
|
64
|
+
timer = timers[flag];
|
|
65
|
+
createTime = timer.createTime;
|
|
66
|
+
tick = timer.tick;
|
|
67
|
+
duration = timer.duration;
|
|
68
|
+
callback = timer.callback;
|
|
69
|
+
|
|
70
|
+
//执行
|
|
71
|
+
passTime = (+new Date().valueOf() - createTime.valueOf()) / duration;
|
|
72
|
+
passTime = passTime > 1 ? 1 : passTime;
|
|
73
|
+
tick(passTime);
|
|
74
|
+
if (passTime < 1 && timer.id) {
|
|
75
|
+
//动画没有结束再添加
|
|
76
|
+
$timers.push(timer);
|
|
77
|
+
} else {
|
|
78
|
+
callback(passTime);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if ($timers.length <= 0) {
|
|
82
|
+
clock.stop();
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
//停止定时器,重置timerId=null
|
|
87
|
+
"stop": function () {
|
|
88
|
+
if ($timerId) {
|
|
89
|
+
clearInterval($timerId);
|
|
90
|
+
$timerId = null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
var id = clock.timer(function (deep) {
|
|
96
|
+
//其中deep为0-1,表示改变的程度
|
|
97
|
+
doback(deep);
|
|
98
|
+
}, duration, callback);
|
|
99
|
+
|
|
100
|
+
// 返回一个函数
|
|
101
|
+
// 用于在动画结束前结束动画
|
|
102
|
+
return function () {
|
|
103
|
+
var i;
|
|
104
|
+
for (i in $timers) {
|
|
105
|
+
if ($timers[i].id == id) {
|
|
106
|
+
$timers[i].id = void 0;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function throttle (callback, _option) {
|
|
115
|
+
|
|
116
|
+
// 缺省值
|
|
117
|
+
var option = {
|
|
118
|
+
time: 200,
|
|
119
|
+
keep: false,
|
|
120
|
+
opportunity: "end"
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// 校对
|
|
124
|
+
if (_option) {
|
|
125
|
+
for (var key in _option) {
|
|
126
|
+
option[key] = _option[key];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
var hadInterval = false, hadClick = false, oneClick = false, arg;
|
|
131
|
+
return function () {
|
|
132
|
+
const _this = this;
|
|
133
|
+
arg = arguments;
|
|
134
|
+
|
|
135
|
+
// 如果前置任务都完成了
|
|
136
|
+
if (!hadInterval) {
|
|
137
|
+
if (option.opportunity != 'end') {
|
|
138
|
+
callback.apply(_this, arg);
|
|
139
|
+
}
|
|
140
|
+
hadInterval = true;
|
|
141
|
+
|
|
142
|
+
var interval = setInterval(() => {
|
|
143
|
+
if (hadClick) {
|
|
144
|
+
if (!option.keep) {
|
|
145
|
+
callback.apply(_this, arg);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
if (option.opportunity != 'begin') {
|
|
149
|
+
if (oneClick || option.opportunity == 'end') callback.apply(_this, arg);
|
|
150
|
+
}
|
|
151
|
+
hadInterval = false;
|
|
152
|
+
oneClick = false;
|
|
153
|
+
clearInterval(interval);
|
|
154
|
+
}
|
|
155
|
+
hadClick = false;
|
|
156
|
+
}, option.time);
|
|
157
|
+
} else {
|
|
158
|
+
hadClick = true;
|
|
159
|
+
oneClick = true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
var index = {
|
|
166
|
+
animation: animation,
|
|
167
|
+
throttle: throttle
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
return index;
|
|
171
|
+
|
|
172
|
+
}));
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @oipage/core.js v0.1.0-alpha.0
|
|
3
|
+
* git+https://github.com/oi-contrib/core.js.git
|
|
4
|
+
*/
|
|
5
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Corejs=e()}(this,(function(){"use strict";var t,e=[];var n={animation:function(n,i,r){arguments.length<2&&(i=400),arguments.length<3&&(r=function(){});var o={timer:function(t,n,i){if(!t)throw new Error("Tick is required!");var r=(new Date).valueOf()+"_"+(1e3*Math.random()).toFixed(0);return e.push({id:r,createTime:new Date,tick:t,duration:n,callback:i}),o.start(),r},start:function(){t||(t=setInterval(o.tick,13))},tick:function(){var t,n,i,r,a,u,f,l=e;for((e=[]).length=0,n=0;n<l.length;n++)t=(a=l[n]).createTime,i=a.tick,u=a.duration,r=a.callback,i(f=(f=(+(new Date).valueOf()-t.valueOf())/u)>1?1:f),f<1&&a.id?e.push(a):r(f);e.length<=0&&o.stop()},stop:function(){t&&(clearInterval(t),t=null)}},a=o.timer((function(t){n(t)}),i,r);return function(){var t;for(t in e)if(e[t].id==a)return void(e[t].id=void 0)}},throttle:function(t,e){var n={time:200,keep:!1,opportunity:"end"};if(e)for(var i in e)n[i]=e[i];var r,o=!1,a=!1,u=!1;return function(){const e=this;if(r=arguments,o)a=!0,u=!0;else{"end"!=n.opportunity&&t.apply(e,r),o=!0;var i=setInterval((()=>{a?n.keep||t.apply(e,r):("begin"!=n.opportunity&&(u||"end"==n.opportunity)&&t.apply(e,r),o=!1,u=!1,clearInterval(i)),a=!1}),n.time)}}}};return n}));
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oipage/core.js",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "前端开发常用基础&核心方法",
|
|
5
|
+
"main": "dist/corejs.min.js",
|
|
6
|
+
"typings": "./types/index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"scripts": {
|
|
9
|
+
"start": "npm install && npm run dev",
|
|
10
|
+
"dev": "devby --run \"rollup --config ./rollup.config.js -w\" \"devby -s 20000\"",
|
|
11
|
+
"build": "rollup --config ./rollup.config.js && terser ./dist/corejs.js --compress --mangle --output ./dist/corejs.min.js"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/oi-contrib/core.js.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"h5",
|
|
19
|
+
"core.js",
|
|
20
|
+
"oipage"
|
|
21
|
+
],
|
|
22
|
+
"author": "zxl20070701",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/oi-contrib/core.js/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://oi-contrib.github.io/core.js",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"devby": "^0.7.0",
|
|
30
|
+
"rollup": "^4.6.1",
|
|
31
|
+
"terser": "^5.17.1"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
//当前正在运动的动画的tick函数堆栈
|
|
2
|
+
var $timers = [];
|
|
3
|
+
//唯一定时器的定时间隔
|
|
4
|
+
var $interval = 13;
|
|
5
|
+
//定时器ID
|
|
6
|
+
var $timerId;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 动画轮播
|
|
10
|
+
* @param {function} doback 轮询函数,有一个形参deep,0-1,表示执行进度
|
|
11
|
+
* @param {number} duration 动画时长,可选
|
|
12
|
+
* @param {function} callback 动画结束回调,可选,有一个形参deep,0-1,表示执行进度
|
|
13
|
+
*
|
|
14
|
+
* @returns {function} 返回一个函数,调用该函数,可以提前结束动画
|
|
15
|
+
*/
|
|
16
|
+
export default function (doback, duration, callback) {
|
|
17
|
+
if (arguments.length < 2) duration = 400;
|
|
18
|
+
if (arguments.length < 3) callback = function () { };
|
|
19
|
+
|
|
20
|
+
var clock = {
|
|
21
|
+
//把tick函数推入堆栈
|
|
22
|
+
"timer": function (tick, duration, callback) {
|
|
23
|
+
if (!tick) {
|
|
24
|
+
throw new Error('Tick is required!');
|
|
25
|
+
}
|
|
26
|
+
var id = new Date().valueOf() + "_" + (Math.random() * 1000).toFixed(0);
|
|
27
|
+
$timers.push({
|
|
28
|
+
"id": id,
|
|
29
|
+
"createTime": new Date(),
|
|
30
|
+
"tick": tick,
|
|
31
|
+
"duration": duration,
|
|
32
|
+
"callback": callback
|
|
33
|
+
});
|
|
34
|
+
clock.start();
|
|
35
|
+
return id;
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
//开启唯一的定时器timerId
|
|
39
|
+
"start": function () {
|
|
40
|
+
if (!$timerId) {
|
|
41
|
+
$timerId = setInterval(clock.tick, $interval);
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
//被定时器调用,遍历timers堆栈
|
|
46
|
+
"tick": function () {
|
|
47
|
+
var createTime, flag, tick, callback, timer, duration, passTime, timers = $timers;
|
|
48
|
+
|
|
49
|
+
$timers = [];
|
|
50
|
+
$timers.length = 0;
|
|
51
|
+
|
|
52
|
+
for (flag = 0; flag < timers.length; flag++) {
|
|
53
|
+
//初始化数据
|
|
54
|
+
timer = timers[flag];
|
|
55
|
+
createTime = timer.createTime;
|
|
56
|
+
tick = timer.tick;
|
|
57
|
+
duration = timer.duration;
|
|
58
|
+
callback = timer.callback;
|
|
59
|
+
|
|
60
|
+
//执行
|
|
61
|
+
passTime = (+new Date().valueOf() - createTime.valueOf()) / duration;
|
|
62
|
+
passTime = passTime > 1 ? 1 : passTime;
|
|
63
|
+
tick(passTime);
|
|
64
|
+
if (passTime < 1 && timer.id) {
|
|
65
|
+
//动画没有结束再添加
|
|
66
|
+
$timers.push(timer);
|
|
67
|
+
} else {
|
|
68
|
+
callback(passTime);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if ($timers.length <= 0) {
|
|
72
|
+
clock.stop();
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
//停止定时器,重置timerId=null
|
|
77
|
+
"stop": function () {
|
|
78
|
+
if ($timerId) {
|
|
79
|
+
clearInterval($timerId);
|
|
80
|
+
$timerId = null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
var id = clock.timer(function (deep) {
|
|
86
|
+
//其中deep为0-1,表示改变的程度
|
|
87
|
+
doback(deep);
|
|
88
|
+
}, duration, callback);
|
|
89
|
+
|
|
90
|
+
// 返回一个函数
|
|
91
|
+
// 用于在动画结束前结束动画
|
|
92
|
+
return function () {
|
|
93
|
+
var i;
|
|
94
|
+
for (i in $timers) {
|
|
95
|
+
if ($timers[i].id == id) {
|
|
96
|
+
$timers[i].id = void 0;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type throttleOpportunityType = "begin" | "end" | "wide"
|
|
2
|
+
|
|
3
|
+
export interface throttleOptionType {
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 节流时长
|
|
7
|
+
*/
|
|
8
|
+
time?: number
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 是否持续节流
|
|
12
|
+
*/
|
|
13
|
+
keep?: boolean
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 执行时机
|
|
17
|
+
*
|
|
18
|
+
* begin(开始触发)、end(结束触发)、wide(第一次开始触发,其余结束触发)
|
|
19
|
+
*/
|
|
20
|
+
opportunity?: throttleOpportunityType
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 节流函数
|
|
26
|
+
*/
|
|
27
|
+
export default interface throttle {
|
|
28
|
+
(callback: Function, option?: throttleOptionType): Function
|
|
29
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export default function (callback, _option) {
|
|
2
|
+
|
|
3
|
+
// 缺省值
|
|
4
|
+
var option = {
|
|
5
|
+
time: 200,
|
|
6
|
+
keep: false,
|
|
7
|
+
opportunity: "end"
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// 校对
|
|
11
|
+
if (_option) {
|
|
12
|
+
for (var key in _option) {
|
|
13
|
+
option[key] = _option[key];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var hadInterval = false, hadClick = false, oneClick = false, arg;
|
|
18
|
+
return function () {
|
|
19
|
+
const _this = this;
|
|
20
|
+
arg = arguments;
|
|
21
|
+
|
|
22
|
+
// 如果前置任务都完成了
|
|
23
|
+
if (!hadInterval) {
|
|
24
|
+
if (option.opportunity != 'end') {
|
|
25
|
+
callback.apply(_this, arg);
|
|
26
|
+
}
|
|
27
|
+
hadInterval = true;
|
|
28
|
+
|
|
29
|
+
var interval = setInterval(() => {
|
|
30
|
+
if (hadClick) {
|
|
31
|
+
if (!option.keep) {
|
|
32
|
+
callback.apply(_this, arg);
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
if (option.opportunity != 'begin') {
|
|
36
|
+
if (oneClick || option.opportunity == 'end') callback.apply(_this, arg);
|
|
37
|
+
}
|
|
38
|
+
hadInterval = false;
|
|
39
|
+
oneClick = false;
|
|
40
|
+
clearInterval(interval);
|
|
41
|
+
}
|
|
42
|
+
hadClick = false;
|
|
43
|
+
}, option.time);
|
|
44
|
+
} else {
|
|
45
|
+
hadClick = true;
|
|
46
|
+
oneClick = true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
};
|
|
50
|
+
};
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import animationType from "../src/animation"
|
|
2
|
+
import throttleType from "../src/throttle"
|
|
3
|
+
|
|
4
|
+
export default class Corejs {
|
|
5
|
+
static animation: animationType
|
|
6
|
+
static throttle: throttleType
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export let animation: animationType
|
|
10
|
+
export let throttle: throttleType
|