Add Touch Support to RangeSlider
See original GitHub issueRange Slider should support touch events so users are able to interact with that component as expected. Otherwise it feels broken on mobile devices.
This is related with #480, but not the same. I think this one is more important since the expected behaviour of the component is not achieved with this issue.
I’ve managed to add the events to the method in charge of this (https://github.com/plotly/plotly.js/blob/master/src/components/rangeslider/draw.js#L158) but seems like I need to do two taps for it to work. I suspect that the touchmove
doesn’t trigger just after the touchstart
. Like mousemove
and mousedown
does.
My code:
function setupDragElement(rangeSlider, gd, axisOpts, opts) {
var slideBox = rangeSlider.select('rect.' + constants.slideBoxClassName).node(),
grabAreaMin = rangeSlider.select('rect.' + constants.grabAreaMinClassName).node(),
grabAreaMax = rangeSlider.select('rect.' + constants.grabAreaMaxClassName).node();
function mouseDownHandler() {
var event = d3.event,
target = event.target,
startX = event.clientX || event.touches[0].clientX,
offsetX = startX - rangeSlider.node().getBoundingClientRect().left,
minVal = opts.d2p(axisOpts.range[0]),
maxVal = opts.d2p(axisOpts.range[1]);
var dragCover = dragElement.coverSlip();
dragCover.addEventListener('mousemove', mouseMove);
dragCover.addEventListener('touchmove', mouseMove);
dragCover.addEventListener('mouseup', mouseUp);
dragCover.addEventListener('touchend', mouseUp);
function mouseMove(e) {
var clientX = e.clientX || e.touches[0].clientX;
var delta = +clientX - startX;
var pixelMin, pixelMax, cursor;
switch(target) {
case slideBox:
cursor = 'ew-resize';
pixelMin = minVal + delta;
pixelMax = maxVal + delta;
break;
case grabAreaMin:
cursor = 'col-resize';
pixelMin = minVal + delta;
pixelMax = maxVal;
break;
case grabAreaMax:
cursor = 'col-resize';
pixelMin = minVal;
pixelMax = maxVal + delta;
break;
default:
cursor = 'ew-resize';
pixelMin = offsetX;
pixelMax = offsetX + delta;
break;
}
if(pixelMax < pixelMin) {
var tmp = pixelMax;
pixelMax = pixelMin;
pixelMin = tmp;
}
opts._pixelMin = pixelMin;
opts._pixelMax = pixelMax;
setCursor(d3.select(dragCover), cursor);
setDataRange(rangeSlider, gd, axisOpts, opts);
}
function mouseUp() {
dragCover.removeEventListener('mousemove', mouseMove);
dragCover.removeEventListener('mouseup', mouseUp);
dragCover.removeEventListener('touchmove', mouseMove);
dragCover.removeEventListener('touchend', mouseUp);
Lib.removeElement(dragCover);
}
}
rangeSlider.on('mousedown', mouseDownHandler );
rangeSlider.on('touchstart', mouseDownHandler );
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Touch Support in WPF Range Slider control - Syncfusion
Learn here all about Touch Support in Syncfusion WPF Range Slider (SfRangeSlider) control, its elements and more.
Read more >Range Slider how Implement Touch Mobile? - Stack Overflow
You need to attach handlers for touch events to make the range sliders work in mobile. If you add the below lines to...
Read more >Touch-enabled Custom Range Slider Web Component
Supports both horizontal and vertical layouts. Local & Session storage. Supports ARIA attributes. Mobile friendly. Touch & Keyboard interactions ...
Read more >Responsive & Touch-Friendly jQuery Range Slider Plugin
A small and fast jQuery plugin to create a customizable, responsive, touch-enabled range slider control from the native HTML5 range input.
Read more >Touch Compatible Range Slider - CodePen
Uses rangeslider.js pollyfill script to replace input type=range with div based range slider, compatible with browsers and touch devices...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @etpinard! I will give it a try
Closed via #5025.