question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Touch Support to RangeSlider

See original GitHub issue

Range 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:closed
  • Created 7 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cristiantxcommented, Nov 2, 2016

Thanks @etpinard! I will give it a try

0reactions
archmojcommented, Sep 9, 2020

Closed via #5025.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found