aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlbin Larsson <mail@albinlarsson.com>2018-06-19 17:16:30 +0200
committerAlbin Larsson <mail@albinlarsson.com>2018-06-19 17:22:12 +0200
commit004528a65c334603bfd0e79222687a7fec02abcb (patch)
tree43ee2e3847a25afa7a230da6a1940f5b70516d1e /src
parent39c7bd40c205700e7b1df677b8aaacc6cb8100db (diff)
downloadplyr-004528a65c334603bfd0e79222687a7fec02abcb.tar.lz
plyr-004528a65c334603bfd0e79222687a7fec02abcb.tar.xz
plyr-004528a65c334603bfd0e79222687a7fec02abcb.zip
Avoid conditions in volume scroll event listener
Diffstat (limited to 'src')
-rw-r--r--src/js/listeners.js38
1 files changed, 11 insertions, 27 deletions
diff --git a/src/js/listeners.js b/src/js/listeners.js
index 9d987508..d9811dd1 100644
--- a/src/js/listeners.js
+++ b/src/js/listeners.js
@@ -665,36 +665,20 @@ class Listeners {
// Detect "natural" scroll - suppored on OS X Safari only
// Other browsers on OS X will be inverted until support improves
const inverted = event.webkitDirectionInvertedFromDevice;
- const step = 1 / 50;
- let direction = 0;
-
- // Scroll down (or up on natural) to decrease
- if (event.deltaY < 0 || event.deltaX > 0) {
- if (inverted) {
- this.player.decreaseVolume(step);
- direction = -1;
- } else {
- this.player.increaseVolume(step);
- direction = 1;
- }
- }
- // Scroll up (or down on natural) to increase
- if (event.deltaY > 0 || event.deltaX < 0) {
- if (inverted) {
- this.player.increaseVolume(step);
- direction = 1;
- } else {
- this.player.decreaseVolume(step);
- direction = -1;
- }
- }
+ // Get delta from event. Invert if `inverted` is true
+ const [x, y] = [event.deltaX, -event.deltaY]
+ .map(value => inverted ? -value : value);
+
+ // Using the biggest delta, normalize to 1 or -1 (or 0 if no delta)
+ const direction = Math.sign(Math.abs(x) > Math.abs(y) ? x : y);
+
+ // Change the volume by 2%
+ this.player.increaseVolume(direction / 50);
// Don't break page scrolling at max and min
- if (
- (direction === 1 && this.player.media.volume < 1) ||
- (direction === -1 && this.player.media.volume > 0)
- ) {
+ const { volume } = this.player.media;
+ if ((direction === 1 && volume < 1) || (direction === -1 && volume > 0)) {
event.preventDefault();
}
},