Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverstasa authored Oct 31, 2023
1 parent b20f353 commit f884b7a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions advanced-demo/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
let lastPos;

// gets cursor position from top
const getCursorPos = e => e.pageY || e.touches[0].pageY;

// clamp number in between given boundaries
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);

Expand All @@ -8,16 +11,18 @@ const setRange = e => {
const input = document.querySelector('#inputs input[type="range"]');
const inputText = document.querySelector('#inputs input[type="number"]');
const bar = document.querySelector('#image div');
const newValue = clamp(inputText.value*1 + (lastPos - e.pageY), 0, 100);
lastPos = e.pageY || e.touches[0].pageY;
const currentPos = getCursorPos(e);
const newValue = clamp(inputText.value*1 + (lastPos - ), 0, 100);

lastPos = currentPos;
input.value = newValue;
inputText.value = newValue;
bar.style.height = newValue + '%';
};

// set listener to change value on mouseMove, and to unset on mouseUp
const hook = e => {
lastPos = e.pageY || e.touches[0].pageY;
lastPos = getCursorPos(e);
document.body.classList.add('touchy'); // cursor after leaving image
document.addEventListener('mousemove', setRange);
document.addEventListener('mouseup', unhook);
Expand Down

0 comments on commit f884b7a

Please sign in to comment.