The Slider component for Angular represents a flexible Slider that lets the user select from a range of values by moving a thumb along a track. The widget is completely customizable in terms of appearance and offers numerous configuration options like mouse wheel and keyboard support, smooth or step-based slider and support for range sliders.
npm install -g @angular/cli ng new jqwidgets-project cd jqwidgets-project
ng add jqwidgets-ng
jQWidgets UI for Angular is distributed as jqwidgets-ng NPM package
npm install jqwidgets-ng
@import 'jqwidgets-ng/jqwidgets/styles/jqx.base.css';
"styles": [ "node_modules/jqwidgets-ng/jqwidgets/styles/jqx.base.css" ]
<div style="width: 550px">
<div style="height: 180px;">
<div style="float: left">
<span style="font-style: italic;">Red</span>
<jqxSlider [theme]="'fluent'" #redSlider (onChange)='redSliderOnChange()'
[height]='60' [min]='0' [max]='255' [tickLabelFormatFunction]='tickLabelFormatFunction'
[value]='0' [step]='25.5' [mode]='"fixed"' [tooltipFormatFunction]='tooltipFormatFunction'
[showTickLabels]='true' [tooltip]='true' [ticksFrequency]='25.5'>
</jqxSlider>
<span style="font-style: italic;">Green</span>
<jqxSlider [theme]="'fluent'" #greenSlider (onChange)='greenSliderOnChange()'
[height]='60' [min]='0' [max]='255' [tickLabelFormatFunction]='tickLabelFormatFunction'
[value]='0' [step]='25.5' [mode]='"fixed"' [tooltipFormatFunction]='tooltipFormatFunction'
[showTickLabels]='true' [tooltip]='true' [ticksFrequency]='25.5'>
</jqxSlider>
<span style="font-style: italic;">Blue</span>
<jqxSlider [theme]="'fluent'" #blueSlider (onChange)='blueSliderOnChange()'
[height]='60' [min]='0' [max]='255' [tickLabelFormatFunction]='tickLabelFormatFunction'
[value]='255' [step]='25.5' [mode]='"fixed"' [tooltipFormatFunction]='tooltipFormatFunction'
[showTickLabels]='true' [tooltip]='true' [ticksFrequency]='25.5'>
</jqxSlider>
</div>
<div class="jqx-rc-all colorBlock" id="colorBlock">
<div id="colorLabel"></div>
</div>
</div>
<jqxCheckBox [theme]="'fluent'" (onChange)='checkBoxOnChange($event)'
[width]='200' [checked]='false'>
Smooth Thumb Drag
</jqxCheckBox>
</div>
import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
import { jqxSliderModule, jqxSliderComponent } from 'jqwidgets-ng/jqxslider';
@Component({
selector: 'app-root',
imports: [jqxSliderModule],
standalone: true,
templateUrl: './app.component.html',
styleUrls: ['app.component.css'],
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild('redSlider') redSlider: jqxSliderComponent;
@ViewChild('greenSlider') greenSlider: jqxSliderComponent;
@ViewChild('blueSlider') blueSlider: jqxSliderComponent;
ngAfterViewInit(): void {
this.setColor();
}
redSliderOnChange(): void {
this.setColor();
};
greenSliderOnChange(): void {
this.setColor();
};
blueSliderOnChange(): void {
this.setColor();
};
checkBoxOnChange(event: any): void {
let checked = event.args.checked;
let value = 'default';
if (!checked) {
value = 'fixed';
}
this.redSlider.mode(value);
this.greenSlider.mode(value);
this.blueSlider.mode(value);
};
setColor(): void {
let red = this.fixHex(Math.round(this.redSlider.value()).toString(16)),
green = this.fixHex(Math.round(this.greenSlider.value()).toString(16)),
blue = this.fixHex(Math.round(this.blueSlider.value()).toString(16));
document.getElementById('colorBlock').style.backgroundColor = '#' + red + green + blue;
document.getElementById('colorLabel').innerHTML = ('#' + red + green + blue).toUpperCase();
let color = this.getTextElement({ r: parseInt(red, 16), g: parseInt(green, 16), b: parseInt(blue, 16) });
document.getElementById('colorLabel').style.color = color;
};
fixHex(hex: any): any {
return (hex.length < 2) ? '0' + hex : hex;
};
getTextElement(color: any): any {
let nThreshold = 105;
let bgDelta = (color.r * 0.299) + (color.g * 0.587) + (color.b * 0.114);
let foreColor = (255 - bgDelta < nThreshold) ? 'Black' : 'White';
return foreColor;
};
tickLabelFormatFunction: any = (value) => {
if (value == 0) return value;
if (value == 255) return value;
return "";
};
tooltipFormatFunction: any = (value) => {
if (this.redSlider.mode() === 'default') {
if (value < 10) return new Number(value.toPrecision(3));
if (value > 10 && value < 100) return new Number(value.toPrecision(4));
else return new Number(value.toPrecision(5));
}
else {
if (value < 10) return new Number(value.toPrecision(3));
if (value > 10 && value < 256) return new Number(value.toPrecision(4));
}
};
}