leongersen / noUiSlider

noUiSlider is a lightweight, ARIA-accessible JavaScript range slider with multi-touch and keyboard support. It is fully GPU animated: no reflows, so it is fast; even on older devices. It also fits wonderfully in responsive designs and has no dependencies.
https://refreshless.com/nouislider/
MIT License
5.64k stars 658 forks source link

Question: onchange, is it possible to check the previous values? #1194

Closed stratboy closed 2 years ago

stratboy commented 2 years ago

Hi, for an app I would need to know, onchange, if the user has broadened or restricted the range, so I need to know if passed values are larger or smaller than the previous ones. Is it some way possible?

Thank you

leongersen commented 2 years ago

Yeah, you can keep track of the previous values and compare:

noUiSlider.create(slider, { ... });

// Initial values
var previousValues = slider.noUiSlider.get();

slider.noUiSlider.on('change', function(values, handle) {
    // compare "values" and "previousValues"

    previousValues = values;
});
stratboy commented 2 years ago

Ok thank you. For reference, for future users, I paste here another way that I'm using right now:

      // code extracted from a Vue component

      this.start_values = null;

      this.slider.noUiSlider.on('start', function(values, handle, unencoded, tap, positions, noUiSlider){
        this.start_values = unencoded;
      }.bind(this));

      this.slider.noUiSlider.on('end', function(values, handle, unencoded, tap, positions, noUiSlider){
        this.start_values = null; // reset
      }.bind(this));

      this.slider.noUiSlider.on('change', function(values, handle, unencoded, tap, positions, noUiSlider){
          // do something with this.start_values
      }.bind(this));
github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.