简体   繁体   English

Angular 带固定拇指标签的材料垫滑块

[英]Angular Material mat-slider with fixed thumblabel

I would like to use a mat-slider with a thumblabel that doesn't move along with the slider when it is moved.我想使用带有拇指标签的垫子滑块,它在移动时不会与 slider 一起移动。 The way I see it the entire 'mat-slider-thumb-container' gets moved along the slider with the 'transform:translateX(xx%)' style So I would guess if I add a the same style but with -xx% to the 'mat-slider-thumb-label' it should always be in the center of the slider?我看到它的方式整个'mat-slider-thumb-container'沿着slider以'transform:translateX(xx%)'风格移动所以我猜我是否添加了相同的风格但使用-xx% 'mat-slider-thumb-label' 它应该始终位于 slider 的中心?

<div class="mat-slider-thumb-container" style="transform: translateX(-100%);" ng-reflect-ng-style="[object Object]"><div class="mat-slider-focus-ring"></div><div class="mat-slider-thumb"></div><div class="mat-slider-thumb-label"><span class="mat-slider-thumb-label-text">0</span></div></div>

Now, first of all how does angular get the xx% and how could I get this value?现在,首先,angular 是如何获得 xx% 的,我怎样才能获得这个值? A workaround would be to just calculate the percentage with the actual value and the min/max values of the slider but angular must store this percentage in some variable, so it feels to me as if it would be much more elegant to use that same variable angular must be using.一种解决方法是仅使用 slider 的实际值和最小/最大值计算百分比,但 angular 必须将此百分比存储在某个变量中,所以在我看来,使用同一个变量会更加优雅angular 必须使用。

I tried to use a directive to add the style of 'tranform:translateX(xx%)' to the element with 'mat-slider-thumb-label' class but it seems to ignore it.我尝试使用指令将 'transform:translateX(xx%)' 的样式添加到具有 'mat-slider-thumb-label' class 的元素中,但它似乎忽略了它。 It works perfectly fine, when I add other styles eg: background-color or width but transform seems to be totally ignored and I don't really understand why.它工作得很好,当我添加其他 styles 例如:背景颜色或宽度但变换似乎完全被忽略了,我真的不明白为什么。

export class FixThumbLabelDirective implements OnChanges {
    constructor(private renderer: Renderer2, private elRef: ElementRef) {}
    @Input() thumbLabelPos: number;

    ngOnChanges() {
        this.renderer.setStyle(
            this.elRef.nativeElement.children[0].children[2].children[2],
            'transform',
            'translateX(' + this.thumbLabelPos * 10 + 'px)'//this obviously needs a properly calculated % value , just put it that way to make changes definitely noticeable
        );
        console.log(this.thumbLabelPos);
    }
}

I also tried other values for transform like px instead of % or to rotate the element but nothing seems to work.我还尝试了其他转换值,例如 px 而不是 % 或旋转元素,但似乎没有任何效果。

my html:我的 html:

<mat-slider appFixThumbLabel [thumbLabelPos]='value' [(ngModel)]="value" 
  value="value" thumbLabel >
</mat-slider>

This is the first question that I actually post here, I hope I expressed my problem clearly enough, all suggestions to find a solution welcome这是我在这里实际发布的第一个问题,我希望我足够清楚地表达了我的问题,欢迎所有找到解决方案的建议

Alright.好吧。 I think I found the issue of my problem.我想我找到了我的问题的问题。 Somewhere in my css there was another transform with.important on the mat-slider-thumb-label class...在我的 css 的某个地方,mat-slider-thumb-label class 上有另一个带有.important 的变换...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM