How to load multiple sliders with values and tooltips with noUISlider?

时间:2015-10-29 15:46:10

标签: nouislider

I'm trying to load multiple sliders using noUISlider and it's not working. When using the first slider, the values changes for the second and third. How do I go about refactoring the code for one slider so I can easily add additional sliders with different options?

Here's a fiddle and below is my code.

// noUISlider
        var actSlider = document.getElementById('act-slider');

        noUiSlider.create(actSlider, {
            start: [0, 50],
            connect: false,
            step: 1,
            range: {
                'min': 0,
                'max': 50
            },
            format: {
                to: function (value) {
                    return value + '';
                },
                from: function (value) {
                    return value.replace('', '');
                }
            }
        });

        // Connect bar
        var connectBar = document.createElement('div'),
            connectBase = actSlider.getElementsByClassName('noUi-base')[0],
            connectHandles = actSlider.getElementsByClassName('noUi-origin');

        // Give the bar a class for styling and add it to the slider.
        connectBar.className += 'connect';
        connectBase.appendChild(connectBar);

        actSlider.noUiSlider.on('update', function (values, handle) {

            // Pick left for the first handle, right for the second.
            var side = handle ? 'right' : 'left',
            // Get the handle position and trim the '%' sign.
                offset = (connectHandles[handle].style.left).slice(0, -1);

            // Right offset is 100% - left offset
            if (handle === 1) {
                offset = 100 - offset;
            }

            connectBar.style[side] = offset + '%';
        });

        // Value tooltips
        var tipHandles = actSlider.getElementsByClassName('noUi-handle'),
            tooltips = [];

        // Add divs to the slider handles.
        for (var i = 0; i < tipHandles.length; i++) {
            tooltips[i] = document.createElement('div');
            tooltips[i].className += 'value-tooltip';
            tipHandles[i].appendChild(tooltips[i]);
        }

        // When the slider changes, write the value to the tooltips.
        actSlider.noUiSlider.on('update', function (values, handle) {
            tooltips[handle].innerHTML = values[handle];
        });

1 个答案:

答案 0 :(得分:4)

您可以将滑块创建包装在一个函数中,并为要创建滑块的所有元素调用它。

noUiSlider还支持tooltips(来自版本8.1.0)和connect选项,因此如果您不想制作非常具体的内容,则不必自行实施变化。

对于每个滑块的不同选项,有几种方法可以做到这一点。我已使用step选项的数据属性添加了一个示例。

以下代码使用.slider类初始化所有元素上的滑块。

function data ( element, key ) {
    return element.getAttribute('data-' + key);   
}

function createSlider ( slider ) {

        noUiSlider.create(slider, {
            start: [0, 50],
            connect: false,
            step: Number(data(slider, 'step')) || 1,
            range: {
                'min': 0,
                'max': 50
            },
            tooltips: true,
            connect: true,
            format: {
                to: function (value) {
                    return value + '';
                },
                from: function (value) {
                    return value.replace('', '');
                }
            }
        });
}

Array.prototype.forEach.call(document.querySelectorAll('.slider'), createSlider);

这里有jsFiddle演示此代码。