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

@ -191,3 +191,21 @@ export function delProp(data, children, props){
return item; 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

@ -2,6 +2,7 @@ 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,12 +19,13 @@ 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') {