Skip to content

Commit

Permalink
perf: enhance animation fluency
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhom committed Aug 25, 2022
1 parent 66ab827 commit 488c6dd
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions src/ms-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,20 @@ export default class MobileSelect {
}
break;

case "touchmove":
case "mousemove":
event.preventDefault();
if (event.type === "mousemove" && !this.enableClickStatus) break;
this.moveY = Math.floor(
event instanceof TouchEvent ? event.touches[0].clientY : event.clientY
);
this.offsetY = (this.moveY - this.preMoveY) * this.config.scrollSpeed;
this.updateCurDistance(theSlider, index);
this.curDistance[index] = this.curDistance[index] + this.offsetY;
this.movePosition(theSlider, this.curDistance[index]);
this.preMoveY = this.moveY;
break;

case "touchend":
case "mouseup":
theSlider.style.transition = "transform 0.18s ease-out";
Expand All @@ -720,8 +734,7 @@ export default class MobileSelect {
this.optionHeight;

if (this.offsetSum == 0) {
// offsetSum为0, 相当于点击事件
// 0 1 [2] 3 4
// offsetSum为0, 相当于点击事件 点击了中间的选项
const clickOffetNum = Math.floor(
(window.innerHeight - this.moveEndY) / 40
);
Expand Down Expand Up @@ -751,26 +764,13 @@ export default class MobileSelect {
// 修正位置
this.updateCurDistance(theSlider, index);
this.curDistance[index] = this.fixPosition(this.curDistance[index]);
this.movePosition(theSlider, this.curDistance[index]);

// 反弹
if (
this.curDistance[index] + this.offsetSum >
2 * this.optionHeight
) {
if (this.curDistance[index] > 2 * this.optionHeight) {
this.curDistance[index] = 2 * this.optionHeight;
setTimeout(() => {
this.movePosition(theSlider, this.curDistance[index]);
}, 100);
} else if (
this.curDistance[index] + this.offsetSum <
this.oversizeBorder
) {
} else if (this.curDistance[index] < this.oversizeBorder) {
this.curDistance[index] = this.oversizeBorder;
setTimeout(() => {
this.movePosition(theSlider, this.curDistance[index]);
}, 100);
}
this.movePosition(theSlider, this.curDistance[index]);

this.config.transitionEnd?.(
this.getIndexArr(),
this.getCurValue(),
Expand All @@ -791,20 +791,6 @@ export default class MobileSelect {
}

break;

case "touchmove":
case "mousemove":
event.preventDefault();
if (event.type === "mousemove" && !this.enableClickStatus) break;
this.moveY = Math.floor(
event instanceof TouchEvent ? event.touches[0].clientY : event.clientY
);
this.offsetY = (this.moveY - this.preMoveY) * this.config.scrollSpeed;
this.updateCurDistance(theSlider, index);
this.curDistance[index] = this.curDistance[index] + this.offsetY;
this.movePosition(theSlider, this.curDistance[index]);
this.preMoveY = this.moveY;
break;
}
}
}

0 comments on commit 488c6dd

Please sign in to comment.