<rlx>
1.添加节流方法 2.滚动监听添加节流
This commit is contained in:
parent
12b7f0ce0e
commit
eb4e6fa66e
@ -163,31 +163,49 @@ export function exchangeOptionsData(arr, { prop }){
|
||||
}
|
||||
}
|
||||
return newArr;
|
||||
}
|
||||
|
||||
export function toSimple(data, sels, list, prop){
|
||||
if(!data || !isArray(data)){
|
||||
return;
|
||||
}
|
||||
|
||||
let { children, selected, value } = prop;
|
||||
data.forEach(item => {
|
||||
if(item.__node[selected] || sels.find(i => i[value] === item[value])){
|
||||
list.push(item);
|
||||
}else{
|
||||
toSimple(item[children], sels, list, prop);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function delProp(data, children, props){
|
||||
if(!data || !isArray(data)){
|
||||
return;
|
||||
}
|
||||
return data.map(item => {
|
||||
item = { ...item };
|
||||
props.forEach(prop => delete item[prop]);
|
||||
item[children] = delProp(item[children], children, props);
|
||||
return item;
|
||||
})
|
||||
}
|
||||
|
||||
export function toSimple(data, sels, list, prop){
|
||||
if(!data || !isArray(data)){
|
||||
return;
|
||||
}
|
||||
|
||||
let { children, selected, value } = prop;
|
||||
data.forEach(item => {
|
||||
if(item.__node[selected] || sels.find(i => i[value] === item[value])){
|
||||
list.push(item);
|
||||
}else{
|
||||
toSimple(item[children], sels, list, prop);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function delProp(data, children, props){
|
||||
if(!data || !isArray(data)){
|
||||
return;
|
||||
}
|
||||
return data.map(item => {
|
||||
item = { ...item };
|
||||
props.forEach(prop => delete item[prop]);
|
||||
item[children] = delProp(item[children], children, props);
|
||||
return item;
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export function throttle(cb, time = 100 ,wait = true) {
|
||||
// cb => 回调函数
|
||||
// time => 定时
|
||||
// wait => 是否等待 false 立即生效 true 时间到了再执行
|
||||
const self = this // 保存this指向
|
||||
let disable = false // 节流标志
|
||||
return function (...data) {
|
||||
if(disable)return // 如果是禁止状态那么直接跳出
|
||||
disable = true // 如果不是禁止状态那么立即设置为禁止
|
||||
!wait && cb.call(self,...data) // 如果不等待那么立即执行
|
||||
setTimeout(()=>{
|
||||
wait && cb.call(self,...data) // 如果要等待那么到时间后再执行
|
||||
disable = false // 关闭禁止状态
|
||||
},time)
|
||||
}
|
||||
}
|
36
src/main.js
36
src/main.js
@ -1,8 +1,9 @@
|
||||
import '@/common/expand'
|
||||
import '@/style/index.less'
|
||||
import '@/style/iconfont.less'
|
||||
import { default as xmSelect, datas } from './index.js';
|
||||
|
||||
import '@/style/index.less'
|
||||
import '@/style/iconfont.less'
|
||||
import { default as xmSelect, datas } from './index.js';
|
||||
import {throttle} from '@/common/util'
|
||||
|
||||
const moduleName = 'xmSelect';
|
||||
|
||||
/**
|
||||
@ -18,22 +19,23 @@ window.addEventListener('click', () => {
|
||||
/**
|
||||
* 监听页面滚动事件
|
||||
*/
|
||||
window.addEventListener('scroll', () => {
|
||||
window.addEventListener('scroll', throttle(() => {
|
||||
console.log(1123);
|
||||
Object.keys(datas).forEach(key => {
|
||||
let item = datas[key]
|
||||
item && item.calcPosition && item.calcPosition()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
|
||||
module.exports = xmSelect;
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(xmSelect);
|
||||
} else if (window.layui && layui.define) {
|
||||
layui.define(function(exports) {
|
||||
exports(moduleName, xmSelect);
|
||||
});
|
||||
}))
|
||||
|
||||
|
||||
if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
|
||||
module.exports = xmSelect;
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(xmSelect);
|
||||
} else if (window.layui && layui.define) {
|
||||
layui.define(function(exports) {
|
||||
exports(moduleName, xmSelect);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
window[moduleName] = xmSelect;
|
||||
|
Loading…
Reference in New Issue
Block a user