Skip to content

Commit

Permalink
Merge pull request #178 from hyvor/slider-mobile
Browse files Browse the repository at this point in the history
fixes #171 Slider mobile
  • Loading branch information
supun-io authored Dec 11, 2024
2 parents 09f7a98 + da1fc42 commit f923878
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/lib/components/Slider/Slider.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,57 @@
function handleMouseup() {
dragging = false;
window.removeEventListener('mousemove', handleMousemove);
window.removeEventListener('mouseup', handleMouseup);
}
function handleMousemove(event: MouseEvent) {
if (!trackEl) return;
if (dragging) {
const rect = trackEl.getBoundingClientRect();
const x = event.clientX - rect.left;
const width = rect.width;
const newValue = min + (x / width) * (max - min);
value = toStep(newValue);
dispatch('change', value);
calcPosUpdateValue(trackEl, event.clientX);
}
}
// touch events
function handleTouchstart(event: TouchEvent) {
dragging = true;
handleTouchmove(event);
window.addEventListener('touchmove', handleTouchmove);
window.addEventListener('touchend', handleTouchend);
}
function handleTouchend() {
dragging = false;
window.removeEventListener('touchmove', handleTouchmove);
window.removeEventListener('touchend', handleTouchend);
}
function handleTouchmove(event: TouchEvent) {
if (!trackEl) return;
if (dragging) {
calcPosUpdateValue(trackEl, event.touches[0].clientX);
}
}
function calcPosUpdateValue(trackEl: HTMLDivElement, clientX: number) {
const rect = trackEl.getBoundingClientRect();
const x = clientX - rect.left;
const width = rect.width;
const newValue = min + (x / width) * (max - min);
value = toStep(newValue);
dispatch('change', value);
}
</script>

<div class="slider">
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div class="track" bind:this={trackEl} class:dragging onmousedown={handleMousedown}>
<div
class="track"
bind:this={trackEl}
class:dragging
onmousedown={handleMousedown}
ontouchstart={handleTouchstart}
>
<div class="progress" style="width: {progress}%"></div>
<button class="handle" style="left: {progress}%">
<span class="tip">
Expand Down

0 comments on commit f923878

Please sign in to comment.