[英]Set the html slider background programmatically in angular2
I want to create a custom slider component, where the background color should be set with angular data binding.我想创建一个自定义 slider 组件,其中背景颜色应使用 angular 数据绑定设置。
Here is a simple stackblitz example where the default color green should be overridden/exchanged with the data-bound color red.这是一个简单的stackblitz 示例,其中默认颜色绿色应该被数据绑定颜色红色覆盖/交换。
What I tried:我尝试了什么:
Question问题
You can use the power of css variables.您可以使用 css 变量的强大功能。
CSS CSS
.slider{
--bg: green;
}
::-webkit-slider-runnable-track {
background:var(--bg);
}
::-ms-track {
background: var(--bg);
}
::-moz-range-track {
background: var(--bg);
}
HTML: HTML:
Set {{color}} as background for this slider:
<br />
<div class="slider" [attr.style]="sanitizer.bypassSecurityTrustStyle('--bg:' + color)">
<input id='slider' type="range">
</div>
Here is the implementation: https://stackblitz.com/edit/angular-oygzgp这是实现: https://stackblitz.com/edit/angular-oygzgp
You can use ngClass to set your color dynamically您可以使用 ngClass 动态设置颜色
in your css.在您的 css 中。
.test::-webkit-slider-runnable-track {
background: red ;
}
.test::-ms-track {
background:red ;
}
.test::-moz-range-track {
background: red ;
}
in your html:在您的 html 中:
<input id='slider' type="range" [ngClass]="isRed ? 'red' : 'green'">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.