Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaogliu committed Dec 14, 2021
1 parent e877e6a commit fb82c19
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dist/pureFullPage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pure-full-page",
"version": "1.0.9",
"version": "1.0.10",
"description": "Full page scroll developed by pure JavaScript",
"keywords": [
"full page scroll",
Expand Down
11 changes: 4 additions & 7 deletions src/js/pureFullPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class PureFullPage {
this.currentPosition = -(activeNavIndex * this.viewHeight);
this.turnPage(this.currentPosition);
}
handleWindowResize(event) {
// 设置防抖动函数
utils.debounceWithParam(this.getNewPosition, this, event, 500);
}
// 页面跳转
turnPage(height) {
this.container.style.top = `${height}px`;
Expand Down Expand Up @@ -151,9 +147,9 @@ class PureFullPage {
if (this.options.isShowNav) {
this.createNav();
}
const handleMouseWheel = utils.debounce(this.scrollMouse.bind(this), 40, true);

// 鼠标滚轮监听,火狐鼠标滚动事件不同其他
const handleMouseWheel = utils.debounce(this.scrollMouse.bind(this), 40, true);
if (navigator.userAgent.toLowerCase().indexOf('firefox') === -1) {
document.addEventListener('mousewheel', handleMouseWheel);
} else {
Expand All @@ -164,13 +160,14 @@ class PureFullPage {
document.addEventListener('touchstart', event => {
this.startY = event.touches[0].pageY;
});
const handleTouchEnd = utils.throttleWithParam(this.touchEnd, this, 500);
const handleTouchEnd = utils.throttle(this.touchEnd, this, 500);
document.addEventListener('touchend', handleTouchEnd);
document.addEventListener('touchmove', event => {
event.preventDefault();
});

// 窗口尺寸变化时重置位置
window.addEventListener('resize', this.handleWindowResize.bind(this));
const handleNewPosition = utils.debounce(this.getNewPosition.bind(this), 200);
window.addEventListener('resize', handleNewPosition);
}
}
10 changes: 1 addition & 9 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@ const utils = {
this.getWheelDelta = event => -event.detail;
return -event.detail;
},
// 防抖动函数,method 回调函数,context 上下文,event 传入的时间,delay 延迟函数
// 调用的时候直接执行,注意和 throttle 在使用的时候的区别
debounceWithParam(method, context, event, delay) {
clearTimeout(method.tId);
method.tId = setTimeout(() => {
method.call(context, event);
}, delay);
},
// 截流函数,method 回调函数,context 上下文,delay 延迟函数,
// 返回的是一个函数
throttleWithParam(method, context, delay) {
throttle(method, context, delay) {
let wait = false;
return function(...args) {
if (!wait) {
Expand Down

0 comments on commit fb82c19

Please sign in to comment.