您正在改變value
,而你只需要改變的元素的style
:
connectBar.style[side] = offset + '%';
完整的示例:
var connectSlider = document.getElementById('connect');
noUiSlider.create(connectSlider, {
start: [20, 80],
connect: false,
range: {
'min': 0,
'max': 100
}
});
var connectBar = document.createElement('div'),
connectBase = connectSlider.getElementsByClassName('noUi-base')[0],
connectHandles = connectSlider.getElementsByClassName('noUi-origin');
// Give the bar a class for styling and add it to the slider.
connectBar.className += 'connect';
connectBase.appendChild(connectBar);
connectSlider.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 + '%';
});
在這裏看到的例子:https://refreshless.com/nouislider/examples/#section-custom-connect
Leon,謝謝你,第三個選擇肯定是最好的選擇。我嘗試在輸入後放置'%'符號,但美學上它不是最好的解決方案。偉大的滑塊和支持! –