1.添加节流方法
2.滚动监听添加节流
This commit is contained in:
ruanlinxin 2021-05-31 22:22:34 +08:00
parent 12b7f0ce0e
commit eb4e6fa66e
2 changed files with 64 additions and 44 deletions

View File

@ -163,31 +163,49 @@ export function exchangeOptionsData(arr, { prop }){
} }
} }
return newArr; 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)
}
}

View File

@ -1,8 +1,9 @@
import '@/common/expand' import '@/common/expand'
import '@/style/index.less' import '@/style/index.less'
import '@/style/iconfont.less' import '@/style/iconfont.less'
import { default as xmSelect, datas } from './index.js'; import { default as xmSelect, datas } from './index.js';
import {throttle} from '@/common/util'
const moduleName = 'xmSelect'; 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 => { Object.keys(datas).forEach(key => {
let item = datas[key] let item = datas[key]
item && item.calcPosition && item.calcPosition() item && item.calcPosition && item.calcPosition()
}) })
}) }))
if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') { if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
module.exports = xmSelect; module.exports = xmSelect;
} else if (typeof define === 'function' && define.amd) { } else if (typeof define === 'function' && define.amd) {
define(xmSelect); define(xmSelect);
} else if (window.layui && layui.define) { } else if (window.layui && layui.define) {
layui.define(function(exports) { layui.define(function(exports) {
exports(moduleName, xmSelect); exports(moduleName, xmSelect);
}); });
} }
window[moduleName] = xmSelect; window[moduleName] = xmSelect;