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.66k stars 659 forks source link

Round number to the nearest multiple #1156

Closed Meth0d-S closed 3 years ago

Meth0d-S commented 3 years ago

I use noUiSlider with the 'step' parameter. Each step is equal to 100,000, 'range' = 1,000,000. When I enter a number in' stepSliderValueElement ' that is not a multiple of 100,000, it rounds the number to the nearest multiple. Is it possible to select a number that is not a multiple of 'step' with the 'step' parameter?

leongersen commented 3 years ago

Yes, you can set the exactInput argument to set or setHandle to true.

Meth0d-S commented 3 years ago

I'm writing this calc.noUiSlider.set(this.value, true, true);, but for some reason it still doesn't work

Meth0d-S commented 3 years ago

all my code:

var calc = document.querySelector('.calc-slider');

    noUiSlider.create(calc, {
      start: [100000],
      connect: [true, false],
      range: {
        'min': [100000, 100000],
        '50%': [1000000, 1000000],
        'max': [20000000],
      },
    });

    var calcValue = document.querySelector('.calc-input');

    calc.noUiSlider.on('update', function (values, handle) {
      calcValue.value = new Intl.NumberFormat().format(values[handle]);
      // calcValue.value = values[handle]
      var now = calc.noUiSlider.get();
      var connect = document.querySelector('.noUi-connect')
      var handler = document.querySelector('.noUi-handle')
      var red = '#D30000'
      var yellow = '#EFAD00'
      var green = '#19C241'
      if (now < 499999) {
        connect.style.backgroundColor = red
        handler.style.borderColor = red
      }
      if (now > 499999) {
        connect.style.backgroundColor = yellow
        handler.style.borderColor = yellow
      }
      if (now > 999999) {
        connect.style.backgroundColor = green
        handler.style.borderColor = green
      }
    });

    calcValue.addEventListener('change', function () {
      calc.noUiSlider.set(this.value, true, true);
    });
Meth0d-S commented 3 years ago

I changed the update event to slide and everything worked. The question can be closed. Thanks

leongersen commented 3 years ago

Glad you worked it out 👍

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.