noUiSlider - 以格式或通过更新事件获取当前滑块

时间:2015-10-29 10:26:16

标签: javascript nouislider

我有多个滑块,由document.querySelectorAll('。js-slider')定义; on tooltips.format我想在值上方插入一个标签,该标签是通过数据标签在当前幻灯片对象上定义的。

在init上运行正常。

但是在更新时它会获得最后一个初始化的滑块标签,这很明显:) 但是,我可以以某种方式获取tooltips.format内或更新事件内的当前滑块对象或id,或者获取当前滑块的设置对象吗?

var uiSlider = {
init : function() {
    var rangeInputs = document.querySelectorAll( '.js-slider' );
    uiSlider.setup( rangeInputs );
},
setup : function( sliders ) {
    var len = sliders.length,
        slider, min, max, step, value, range, start, postfix, pipValues, label, settings;

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

    while( len-- ) {            
        slider = sliders[len];                  
        min = data( slider,'min' );
        max = data( slider,'max' );
        step = data( slider,'step' );
        value = data( slider, 'value' );
        range = !isNaN( min ) && !isNaN( max ) ? { 'min': min, 'max': max } : { 'min': 0, 'max': 100 };
        start = !isNaN( value ) ? value : 0;
        postfix = slider.getAttribute( 'data-postfix' );
        pipValues = slider.getAttribute( 'data-pipvalues' ) ? slider.getAttribute( 'data-pipvalues' ).split( ',' ) : null;
        label =  slider.getAttribute( 'data-label' );
        settings = {
            start: start,
            step: !isNaN( step ) ? step : 1,
            connect: "lower",
            tooltips: {
                format: function( value, handle, settings ) {

                    var dformat = wNumb({
                        decimals: 0,
                        thousand: ',',
                        prefix: '<span class="noUi-tooltip__label">' + label + '</span>',
                        postfix: ( postfix ? ' ' + postfix : ' $' )
                    }); 

                    return dformat.to( parseFloat( value ) );
                }
            },
            range: range,
            pips: { // Show a scale with the slider
                mode: 'positions',
                values: pipValues ? pipValues : [ 0, 20, 40, 60, 80, 100 ],
                density: 2,
                format: wNumb({
                    decimals: 0,
                    thousand: ','
                })
            },
            format: wNumb({
                decimals: 0
            })
        };

        noUiSlider.create( slider, settings );

    }
}
};

uiSlider.init();

还没有弄清楚这一点。

可以在此处看到问题的正在运行的Fiddel:Fiddle

1 个答案:

答案 0 :(得分:0)

This happens because you are sharing a 'label' variable between all sliders.

If you refactor your code to run all slider initializations in a closure, you can have label variables unique to each slider.

The simplest way to do this is to forEach on your list of sliders, for example using Array.prototype.forEach.call(sliders, function (slider));

Your code refactored:

var uiSlider = {
    init : function() {
        var rangeInputs = document.querySelectorAll( '.js-slider' );
        uiSlider.setup( rangeInputs );
    },
    setup : function( sliders ) {

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

        Array.prototype.forEach.call(sliders, function ( slider ) {

            var min = data( slider,'min' ),
                max = data( slider,'max' ),
                step = data( slider,'step' ),
                value = data( slider, 'value' ),
                range = !isNaN( min ) && !isNaN( max ) ? { 'min': min, 'max': max } : { 'min': 0, 'max': 100 },
                start = !isNaN( value ) ? value : 0,
                postfix = slider.getAttribute( 'data-postfix' ),
                pipValues = slider.getAttribute( 'data-pipvalues' ) ? slider.getAttribute( 'data-pipvalues' ).split( ',' ) : null,
                label =  slider.getAttribute( 'data-label' );

            noUiSlider.create(slider, {
                start: start,
                step: !isNaN( step ) ? step : 1,
                connect: "lower",
                tooltips: {
                    format: function( value, handle, settings ) {

                        var dformat = wNumb({
                            decimals: 0,
                            thousand: ',',
                            prefix: '<span class="noUi-tooltip__label">' + label + '</span>',
                            postfix: ( postfix ? ' ' + postfix : ' $' )
                        }); 

                        return dformat.to( parseFloat( value ) );
                    }
                },
                range: range,
                pips: { // Show a scale with the slider
                    mode: 'positions',
                    values: pipValues ? pipValues : [ 0, 20, 40, 60, 80, 100 ],
                    density: 2,
                    format: wNumb({
                        decimals: 0,
                        thousand: ','
                    })
                },
                format: wNumb({
                    decimals: 0
                })
            });
        });
    }
};

uiSlider.init();

And an updated jsFiddle.