说明:选项卡定时切换,事件监听鼠标移动事件,移动后重新计时。
数据:
data(){
x: "", // 坐标
y: "",
count: 0, // 控制时长
switchTime: 0.1,
},
生命周期:
mounted() {
this.switch();
window.addEventListener("mousemove", this.mouse);
},
方法:
methods: {
switch() {
var card = this;
this.count++;
this.timer = setTimeout(function () {
if (card.count == card.switchTime * 100) {
card.isshowstep = true;
if (card.isactive < card.finacialAna_tabs.length - 1) {
card.isactive++;
card.fina_tabs_click(card.isactive); //调用切换方法
} else {
card.isactive = 0;
card.fina_tabs_click(card.isactive);
}
card.count = 0;
}
clearTimeout(card.timer);
return card.switch();
}, 1000);
// 节流调用
mouse: nowThrottle(function (e) {
var x1 = e.clientX;
var y1 = e.clientY;
if (this.x != x1 || this.y != y1) {
this.count = 0;
}
// console.log("鼠标滑动后", this.count);
this.x = x1;
this.y = y1;
}, 500),
}
节流文件
export const nowThrottle = (func, wait) => {
console.log("进入节流")
let timeout;
return function () {
let context = this;
let args = arguments;
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}